I want to pass the result object to another display function,but for some reason its not working. cmd stops working
I tried using different aproaches but none seems to be working.. Basically I ahbe to add two matrices using a function and return type should be of an object. I want to print the result of this addition, not in this function but using another function.
#include<iostream>
using namespace::std;
class Matrix{
private:
int row,column; //dimensions row x column
int **matrix; //pointer to a pointer to int
void allocarray(){ //method to llocate array matrix and the matrix[i] arrays
matrix=new int*[row];
for(int i=0;i<row;i++){
matrix[i]=new int[column];
}
}
public:
Matrix(int rowsize, int columnsize); //default constructor
Matrix(); //user defined constructor
~Matrix(); //destructor
void input();
Matrix Add(Matrix);
void display(Matrix);
};
Matrix Matrix::Add(Matrix m2)
{
Matrix result(3,3);
for(int i=0;i<row;i++)
{
for( int j=0;j<column;j++)
{
result.matrix[i][j]=this->matrix[i][j]+m2.matrix[i][j];
}
}
return *this;
}
void Matrix::display(Matrix m)
{
for(int i=0;i<row;i++)
{
for( int j=0;j<column;j++)
{
cout<<m.matrix[i][j];
}
cout<<endl;
}
}
Matrix::Matrix( int rowsize, int columnsize):row(rowsize),column(columnsize) //dynamivally allocate
{
allocarray();
for(int i=0;i<row;i++)
{
for( int j=0;j<column;j++)
{
matrix[i][j]=0; //initilze all values to 0
}
}
}
Matrix::~Matrix() //destructor
{
for( int i=0;i<row;i++)
{
delete [] matrix[i];
}
delete [] matrix;
}
void Matrix::input()
{
cout<<"enter the elements for the matrix"<<endl;
for(int i=0;i<row;i++)
{
for(int j=0;j<column;j++)
cin>>matrix[i][j];
cout<<"\n"; //check it after performing functions!
}
}
int main()
{
Matrix obj1(3,3),obj2(3,3),res(3,3);
cout<<"enter elements for matrix one";
obj1.input();
cout<<"enter elements for matrix two";
obj2.input();
cout<<"addition of two matrices";
res=obj1.Add(obj2);
obj1.display(res);
return 0;
}
so here's the code for copy constructor
Matrix::Matrix(const Matrix &m):row(m.row),column(m.column)
{
allocarray();
for(int i=0;i<row<i++)
{
for(int j=0;j<column;j++)
{
matrix[i][j]=m.matrix[i][j];
}
}
}