C++
  Home arrow C++ arrow Page 5 - 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++ - Matrix Multiplication - Overloading * Again


    (Page 5 of 7 )

    We can actually overload the same operator more than once if we would like. As long as the function's signature (return type, name, and arguments) is different, we can define as many as we want. The * operator would be a likely candidate for implementing matrix multiplication as well as scaling. Both imply multiplication of some sort, so it should make sense.

    Matrix multiplication has a requirement: the number of columns in the first matrix must be equal to the number of rows in the second. Matrix multiplication is NOT commutative. I won't explain how to do matrix multiplcation--it's easy enough to look up this topic on-line or in a math textbook. Or you can deduce the "by-hand" algorithm from the code.

    const Matrix operator*(Matrix& m) {
    assert(numCols==m.numRows);
    Matrix theMatrix(numRows,m.numCols);
    for (int r=0;r<numRows;r++) {
    for (int c=0;c<m.numCols;c++) {
    for (int i=0;i<numCols;i++) {
    theMatrix[r][c]+=matrix[r][i]*m[i][c];
    }
    }
    }
    return theMatrix;
    }


    Overloading << and >>
    There are only two more important operators that I will cover here. These are perhaps the operators that should be implemented for each and every class you create. The streaming operators << and >> allow your object to be saved and restored from any stream, be it console, network, or file.

    There is a slight additional challenge with these operators because we must allow the stream access to our object's private data. Therefore, these functions must be declared as friends inside the Matrix class.

    Let's first look at outputting to a stream:

    friend ostream& operator<<(ostream& os,const Matrix& m) {
    os << m.numRows<<" "<<m.numCols<<" "<<endl;
    for (int r=0;r<m.numRows;r++) {
    for (int c=0;c<m.numCols;c++) {
    os << m.matrix[r][c] << " ";

    }
    os <<endl;
    }
    return os;
    }


    We include this function in our Matrix class and declare it to be friend. It does not have to be defined in our Matrix class, but that's where I left it. First we output the number of rows and columns we have, making sure to separate this data by a space to keep it intact. Then we just run through a loop, outputting each row on its own line. A very important thing to note about overloading streaming operators is that we always return a reference to the same stream we passed in. Why? Because that's what allows us to stack output and input:

    Matrix<float> a(2,2);

    cout <<"Matrix a:"<<endl<< a<<endl;

    ofstream ofile("output.dat");
    ofile << a << endl<<"End of File";


    It is a quite similar technique to read in values from a stream:

    friend istream& operator>>(istream& is, Matrix& m) {
    int rows,cols;
    is >> rows >> cols;
    m.SetSize(rows,cols);
    for (int r=0;r<rows;r++)
    for (int c=0;c<cols;c++)
    is >> m[r][c];
    return is;
    }


    Here we declare some local variables to hold our matrix dimensions, which we then pass to the referenced matrix object. Then it's just a matter of reading in the next number and putting it in the appropriate location. We then return a reference to the stream in case the calling function wanted to continue getting data from it in the same command.

    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 4 Hosted by Hostway
    Stay green...Green IT