using namespace std;
class matrix
{
int **sptr;
int rows;
int cols;
public:
matrix(int r,int c)
{
rows=r;
cols=c;
sptr=(int **)new int[rows];
for(int i=0;i<rows;i++)
{
*(sptr+i)=new int[cols];
}
}
void get_matrix();
void put_matrix();
matrix operator+(matrix&);
matrix operator-();
matrix operator+(int x);
friend matrix operator+(int x,matrix &m);
friend ostream& operator<<(ostream &,matrix);
friend istream& operator>>(istream &,matrix&);
void * operator new[](size_t size);
};
void * matrix::operator new[](size_t size)
{
cout<<"hello";
return(malloc(sizeof(int)*size));
}
istream& operator>>(istream &din,matrix &m)
{
for(int i=0;i<m.rows;i++)
{
for(int j=0;j<m.cols;j++)
{
din>>*(*(m.sptr+i)+j);
}
}
}
ostream& operator<<(ostream &dout,matrix m)
{
matrix temp(m.rows,m.cols);
for(int i=0;i<m.rows;i++)
{
for(int j=0;j<m.cols;j++)
{
dout<<*(*(m.sptr+i)+j);
}
}
}
matrix matrix::operator-()
{
matrix temp(rows,cols);
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
*(*(temp.sptr+i)+j)=-*(*(sptr+i)+j);
}
}
return temp;
}
matrix operator+(int x,matrix &m)
{
matrix temp(m.rows,m.cols);
for(int i=0;i<m.rows;i++)
{
for(int j=0;j<m.cols;j++)
{
*(*(temp.sptr+i)+j)=x+*(*(m.sptr+i)+j);
}
}
return temp;
}
matrix matrix::operator+(int x)
{
matrix temp(rows,cols);
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
*(*(temp.sptr+i)+j)=*(*(sptr+i)+j)+x;
}
}
return temp;
}
matrix matrix::operator+(matrix &m)
{
matrix temp(rows,cols);
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
*(*(temp.sptr+i)+j)=*(*(sptr+i)+j)+*(*(m.sptr+i)+j);
}
}
return temp;
}
void matrix::get_matrix()
{
int i,j;
cout<<"Enter the elements";
for(i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
cin>>*(*(sptr+i)+j);
}
cout<<endl;
}
}
void matrix::put_matrix()
{
cout<<endl;
for(int i=0;i<rows;i++)
{
for(int j=0;j<cols;j++)
{
cout<<*(*(sptr+i)+j);
}
cout<<endl;
}
}
int main()
{
int rows1,rows2,cols1,cols2;
cout<<"Enter the no of rows of ist matrix\n";
cin>>rows1;
cout<<"Enter the no of cols of 1st matrix\n";
cin>>cols1;
cout<<"Enter the no of rows of 2nd matrix\n";
cin>>rows2;
cout<<"Enter the no of cols of 2nd matrix\n";
cin>>cols2;
matrix m1(rows1,cols1),m2(rows2,cols2);
m1.get_matrix();
m2.get_matrix();
m1.put_matrix();
m2.put_matrix();
(m1+m2).put_matrix();
(m1+3).put_matrix();
(3+m1).put_matrix();
cout<<"After overloading - operator"<<endl;
(-m1).put_matrix();
cout<<"After overloading >> operator"<<endl;
cin>>m1;
cout<<"After overloading << operator"<<endl;
cout<<m1;
int *p;
p=new
return 0;
}
Comments