Hello all,
I have created a dynamic matrix class. I overloaded the multiplication operator as follows:
I wanted to do it in such a way that I could do this:Code:Matrix Matrix::operator*(const Matrix &another) const //'const' keyword after => doesn't { //modify data members if (col != another.row) { cout<<"Error in matrix multiplication. Matrix dimensions do not agree."<<endl; return *this; } Matrix prod(row,another.col); Complex sum(0,0); for(int i=0; i<row; i++) //i: 0 to 3 { sum = 0; for(int j=0; j<another.col; j++) //j: 0 to 2 { for(int k=0; k<row; k++) { sum = sum + A[k] * another(k,j); } //cout<<"prod("<<i<<","<<j<<") = "<<sum<<endl; prod(i,j) = sum; } } return prod; }
Just for the record, I created a complex number class, and I have defined a similar function:Code:Matrix A(5,5), B(5,1); //define the matrices cout<<A*B<<endl; //I overloaded the cout operator as well
When I doCode:Complex Complex::operator +(const Complex &num) const { Complex sum; sum.real = real + num.real; sum.imag = imag + num.imag; return sum; }
The destructor is called AFTER it is displayed. But this does not work for the Matrix class, and I just can't figure out why. I thought I would save you all the trouble of going through all the code by only displaying what is pertinent. If you need all the code, pls let me know. Thanks a million.Code:Complex a(1,1), b(2,-2); cout<<a+b<<endl;
Bill



LinkBack URL
About LinkBacks


