Home arrow C++ arrow Page 5 - Operator Overloading in C++
C++

Operator Overloading in C++


Operating overloading allows you to pass different variable types to the same function and produce different results. In this article Ben gives us the low-down on operator overloading in C++.

Author Info:
By: Ben Watson
Rating: 3 stars3 stars3 stars3 stars3 stars / 370
December 08, 2002
TABLE OF CONTENTS:
  1. · Operator Overloading in C++
  2. · Definition
  3. · Overloading =
  4. · Overloading
  5. · Matrix Multiplication - Overloading * Again
  6. · Putting It All Together
  7. · Conclusion

print this article
SEARCH DEVARTICLES

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.
blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- 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++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials