double a, b; a + b |
|
double a, b; a + b |
is actually invoking the function:
double a, b; operator+(a, b) |
Matrix3x3 A, B; double x; B = A + x; |
We can do so by defining the operator function:
Matrix3x3 operator+(Matrix3x3 &m, double x) { ... } |
Because:
|
class Matrix3x3 { public: double A[3][3]; }; Matrix3x3 operator+(Matrix3x3 &m, double x) { Matrix3x3 out; int i, j; for (i = 0; i < 3; i = i+1) for (j = 0; j < 3; j = j+1) out.A[i][j] = m.A[i][j] + x; return(out); } |
int main(int argc, char *argv[]) { Matrix3x3 B, C; C = B + 40; } |
double a, b; a + b |
it will invoke the function:
double a, b; operator+(a, b) or a.operator+(b) // This fact help us write // operator functions using member function |
class Matrix3x3 { public: double A[3][3]; Matrix3x3 operator+(double x) { Matrix3x3 out; int i, j; for (i = 0; i < 3; i = i+1) for (j = 0; j < 3; j = j+1) out.A[i][j] = this->A[i][j] + x; return(out); } }; |
int main(int argc, char *argv[]) { Matrix3x3 B, C; C = B + 40; } |
The expression B + 40 is unchanged , but it will NOW map to the member function
class Matrix3x3 { public: double A[3][3]; // -------- Ambiguous.... Matrix3x3 operator+(double x) { Matrix3x3 out; int i, j; for (i = 0; i < 3; i = i+1) for (j = 0; j < 3; j = j+1) out.A[i][j] = this->A[i][j] + x; return(out); } }; // -------- Ambiguous.... Matrix3x3 operator+(Matrix3x3 &m, double x) { Matrix3x3 out; int i, j; for (i = 0; i < 3; i = i+1) for (j = 0; j < 3; j = j+1) out.A[i][j] = m.A[i][j] + x; return(out); } |
Error: Overloading ambiguity between "operator+(Matrix3x3&, double)" and "Matrix3x3::operator+(double)". |
Matrix3x3 A, B; double x; B = x + A; |
Matrix3x3 operator+(double x, Matrix3x3 &m) { ... } |
class Matrix3x3 { public: double A[3][3]; }; Matrix3x3 operator+(double x, Matrix3x3 &m) { Matrix3x3 out; int i, j; for (i = 0; i < 3; i = i+1) for (j = 0; j < 3; j = j+1) out.A[i][j] = m.A[i][j] + x; return(out); } |
int main(int argc, char *argv[]) { Matrix3x3 B, C; C = 40 + B; } |
Matrix3x3 A, B; double x; B = x + A; |
double x; Matrix3x3 A; x.operator+(A); |
We are not allowed to add new functions to C++ primitive types
Hence, this option is NOT possible
+- -+ +- -+ +- -+ | 1 0 0 | | 1 2 3 | | .. .. .. | | 1 1 0 | * | 4 5 6 | = | .. .. .. | | 1 2 5 | | 7 8 9 | | .. .. 1*3 + 2*6 + 5*9 | +- -+ +- -+ +- -+ A + B = C |
c[2][2] = a[2][0]*b[0][2] + a[2][1]*b[1][2] + a[2][2]*b[2][2] |
C[2][2] = 0; for (k = 0; k < 3; k = k + 1) C[2][2] = C[2][2] + A[2][k] * B[k][2]; |
C[i][j] = 0.0; for (k = 0; k < N; k = k + 1) C[i][j] = C[i][j] + A[i][k] * B[k][j]; |
for (i = 0; i < N; i = i + 1) { for (j = 0; j < N; j = j + 1) { C[i][j] = 0.0; for (k = 0; k < N; k = k + 1) C[i][j] = C[i][j] + A[i][k] * B[k][j]; } } |
int main(int argc, char *argv[]) { Matrix3x3 M1, M2, M3; M3 = M1 * M2; // Matrix multiplication !!! }; |
operator*(M1, M2) // Ordinary function or M1.operator*(M2) // Member function |
class Matrix3x3 { public: double A[3][3]; }; Matrix3x3 operator*(Matrix3x3 &m1, Matrix3x3 &m2) { Matrix3x3 out; for (i = 0; i < 3; i = i + 1) { for (j = 0; j < 3; j = j + 1) { out.A[i][j] = 0.0; for (k = 0; k < 3; k = k + 1) out.A[i][j] = out.A[i][j] + m1.A[i][k]*m2.A[k][j]; } } return(out); } |
int main(int argc, char *argv[]) { Matrix3x3 A, B, C; C = A * B; } |
class Matrix3x3 { public: double A[3][3]; Matrix3x3 operator*(Matrix3x3 &m2) { Matrix3x3 out; for (i = 0; i < 3; i = i + 1) { for (j = 0; j < 3; j = j + 1) { out.A[i][j] = 0.0; for (k = 0; k < 3; k = k + 1) out.A[i][j] = out.A[i][j] + A[i][k]*m2.A[k][j]; } } return(out); } }; |
int main(int argc, char *argv[]) { Matrix3x3 A, B, C; C = A * B; } |
cout << value; |
ostream |
Matrix3x3 m; cout << m; |
we must define the following operator function
operator<<( ostream & o, Matrix3x3 & x ) { ... (print Matrix3x3 x) } |
void operator<<( ostream & output, Matrix3x3 & x ) { int i, j; for (i = 0; i < 3; i = i + 1) { for (j = 0; j < 3; j = j + 1) output << x.A[i][j]; output << endl; } } |
#include <iostream.h> int main(int argc, char *argv[]) { Matrix3x3 A; cout << A; } |
Matrix A, B; cout << "Matrix A:" << A << endl << "Matrix B: << B << endl; |
( ( ( ( (cout << "Matrix A:") << A ) << endl ) << "Matrix B: ) << B ) << endl; |
But, here is a little secret:
The operator<<(ostream &, char &) function returns its first argument !!! |
( (cout << "Matrix A:") << A ) ^^^^^^^^^^^^^^^^^^^^^ ( cout << A ) |
Now we have a problem :
The operator<<(ostream &, Matrix3x3 &) function returns NOTHING !!! |
ostream & operator<<( ostream & output, Matrix3x3 & x ) { int i, j; for (i = 0; i < 3; i = i + 1) { for (j = 0; j < 3; j = j + 1) output << x.A[i][j]; output << endl; } return(output); } |
class Matrix3x3 { public: double A[3][3]; }; Matrix3x3 operator*(Matrix3x3 m1, Matrix3x3 m2) { Matrix3x3 out; for (i = 0; i < 3; i = i + 1) { for (j = 0; j < 3; j = j + 1) { out.A[i][j] = 0.0; for (k = 0; k < 3; k = k + 1) out.A[i][j] = out.A[i][j] + m1.A[i][k]*m2.A[k][j]; } } return(out); } |
int main(int argc, char *argv[]) { Matrix3x3 A, B, C; C = A * B; } |
But you gain a lot of speed )