Please help with this code.Am trying to design a Matrix classwith the following operation Addition, Multiplication, Subtraction and division.The code compile , But my result is incorrect
.please if someone can to solve this incorrect result.
This is my code Below.
#include <iostream>
using namespace std;
//member function of matrix class
class matrix{
public:
matrix();
matrix(int m,int n);
int getRow();
int getCol();
double& operator()(int, int);
//the operator used in the matrix class which are (addition operator, multiplication operator,substraction operator, and divide operator).
friend ostream& operator<<(ostream& os, matrix& m);
matrix operator + (matrix&);
matrix operator * (matrix&);
matrix operator - (matrix&);
matrix operator / (matrix&);
private:
void init(int, int);
int nrows,ncols;
double *data;
};
matrix::matrix(){
init(1,1);
}
matrix::matrix(int m, int n){
init(m,n);
}
//destructor to delete the used dynamic memory.
void matrix::init(int m, int n){
nrows=m;
ncols=n;
data= new double[m*n];
for(int i=0; i<m*n; i++)
data[i]=0;
}
int matrix::getRow() { return nrows;}
int matrix::getCol() { return ncols;}
double& matrix::operator ()(int r, int c){
if (r <0 || r> nrows){
cout<<"Illegal row index";
return data[0];
}
else if (c <0 || c > ncols){
cout<<"Illegal Column Index:";
return data[0];
}
else return data[r*ncols+c];
}
(i,j) does not call operator()( int, int ) on "this"; it does something else.
To call operator()( int, int ) on this, you will need to do it explicitly. Any
of the following would work.