class mymatrix{
private:
int** arr;
int m ,n;
public:
mymatrix()
{
std::cout<<"\nConstructor entered.";
std::cout<<"\nEnter no. of rows of the matrix:";
std::cin>>m;
std::cout<<"\nEnter no. of columns of the matrix:";
std::cin>>n;
arr = new int*[m];
for(int i=0;i<m;i++)
{
arr[i]=new int[n];
}
std::cout<<"\nConstructor exited.";
}
/****************************************************************************************/
mymatrix (mymatrix& t)
{
m=t.m;
n=t.n;
std::cout<<"\nCopy constructor has been called...";
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
arr[i][j]=t.arr[i][j];
}
}
}
/********************************************************************************************/
~mymatrix()
{
std::cout<<"\nDestuctoer entered.";
for(int i=0;i<m;i++)
{
delete [] arr[i];
}
delete arr;
std::cout<<"\nDestructor exited.";
}
void getmatrix();
mymatrix operator +(const mymatrix &);
mymatrix operator *(const mymatrix &);
bool operator ==(const mymatrix &);
void display();
};
mymatrix mymatrix :: operator +(const mymatrix & m1)
{
if(m1.m==m && m1.n==n)
{
std::cout<<"\nMatrix can be added togather";
}
else
{
std::cout<<"\nMatrix can't be add togather...!!!";
exit(0);
}
mymatrix t;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
t.arr[i][j] = m1.arr[i][j] + arr[i][j];
}
}
return t;
}
void mymatrix :: getmatrix()
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
std::cout<<"\nEnter value for arr["<<i<<"]["<<j<<"]:::";
std::cin>>arr[i][j];
std::cout<<"\n";
}
}
}
bool mymatrix :: operator ==(const mymatrix & m1)
{
if(m==m1.m && n==m1.n)
{
std::cout<<"\nMatrix can be compared togather";
}
else
{
std::cout<<"\nMatrix can't be compared togather...!!!";
exit(0);
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if( arr[i][j] != m1.arr[i][j])
{
return false;
}
}
}
return true;
}
void mymatrix :: display()
{
for(int i=0;i<m;i++)
{
std::cout<<"\n";
for(int j=0;j<n;j++)
{
std::cout<<arr[i][j]<<"\t";
}
}
}
mymatrix mymatrix :: operator *(const mymatrix & m1)
{
if(m1.m==n && m1.n==m)
{
std::cout<<"\nMatrix can be multiplied togather";
}
else
{
std::cout<<"\nMatrix can't be multiplied togather...!!!";
exit(0);
}
mymatrix t;
for(int i=0;i<m;i++)
{
for(int j=0;j<m1.n;j++)
{
for(int k=0;k<m1.m;k++)
{
t.arr[i][j] = arr[i][k] * m1.arr[k][j];
}
}
}
return t;
}
Hello all .
I'm having problem with my copy constructor. When I'm returning object from operator+ func. . No copy constructor is called. Whereas it should be.
#include<iostream>
#include<stdlib.h>
#include"mymatrix.h"
int main(void)
{
mymatrix c1;
c1.getmatrix();
c1.display();
mymatrix c2;
c2.getmatrix();
c2.display();
mymatrix c3;
c3 = c1 + c2;
c3.display();
mymatrix c4;
c4 = c1 * c2;
c4.display();
return 0;
}