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++.

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.