C++
  Home arrow C++ arrow Page 6 - Operator Overloading in C++
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Operator Overloading in C++
By: Ben Watson
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 253
    2002-12-08

    Table of Contents:
  • Operator Overloading in C++
  • Definition
  • Overloading =
  • Overloading
  • Matrix Multiplication - Overloading * Again
  • Putting It All Together
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Operator Overloading in C++ - Putting It All Together


    (Page 6 of 7 )

    To demonstrate the Matrix class and its overloaded operators, I've written a sample main() as well as some helper functions that will run the class through its paces:

    //the functions fill in a matrix with random values--they are type-specific
    void init_matrix(Matrix<int>& m) {
    for (int r=0;r<m.GetRows();r++)
    for (int c=0;c<m.GetCols();c++)
    m[r][c]=rand()%5+1;
    }

    void init_matrix(Matrix<__int64>& m) {
    for (int r=0;r<m.GetRows();r++)
    for (int c=0;c<m.GetCols();c++)
    m[r][c]=rand()%100000+1;
    }

    void init_matrix(Matrix<float>& m, int precision) {
    for (int r=0;r<m.GetRows();r++)
    for (int c=0;c<m.GetCols();c++) {
    float dec=float(rand()%precision)/precision;
    m[r][c]=float(rand()%5)+1.0+dec;
    }
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
    srand((unsigned)time(NULL));

    //save/load from file7
    Matrix<int> a(5,5);
    init_matrix(a);
    a[0][0]=-13;
    a[1][1]=-13;
    cout << "Writing to file from Matrix a:" <<endl;
    cout << a<<endl;
    ofstream of;
    of.open("test.txt");
    of << a<<endl;
    of.close();
    ifstream iff("test.txt");
    if (!iff) {
    cout << "Error opening file"<<endl;
    return 1;
    }
    Matrix<int> b;
    cout << "Reading from file into Matrix b:"<<endl;
    iff >> b;
    iff.close();
    cout <<endl;
    cout << b<<endl;

    cout <<"Press any key to continue..."<<endl;
    getchar();

    //add two floating-point matrices
    Matrix<float> c(3,2);
    init_matrix(c,100);
    cout <<"Matrix c:"<<endl<< c<<endl;
    Matrix<float> d(3,2);
    init_matrix(d,100);
    cout << "Matrix d:"<<endl<<d<<endl;
    cout << "c+d:"<<endl<<c+d<<endl;

    cout <<"Press any key to continue..."<<endl;
    getchar();

    //scale a floating-point matrix
    Matrix<float> e(10,10);
    init_matrix(e,1);
    float scalar=-1.5;
    cout << "Matrix e:" << endl<<e<<endl;
    cout << "Scalar: "<<scalar<<endl;
    cout << "e * scalar:"<<endl<<e*scalar<<endl;

    cout <<"Press any key to continue..."<<endl;
    getchar();

    //matrix-product
    Matrix<__int64> f(3,5);
    Matrix<__int64> g(5,6);
    init_matrix(f);
    init_matrix(g);
    cout <<"Matrix f:"<<endl<<f<<endl;
    cout <<"Matrix g:"<<endl<<g<<endl;
    cout <<"f*g:"<<endl<<f*g<<endl;

    cout <<"Press any key to continue..."<<endl;
    getchar();

    return 0;
    }

    More C++ Articles
    More By Ben Watson


       · This example was very useful to me in both the operator over loading examples and...
       · opertor overloading is very important topic in c++i think it,s to understading...
       · Op overloading is a beginner's subject. Why use templates in a beginner level...
       · hi my name is shakil khan and i belong to swat pakistan.and i need full page about...
       · Operating overloading allows you to pass different variable types to the same...
     

    C++ ARTICLES

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek