Write a program for four atrithmetic operations in matrix using operator overloading.

• Sep 2nd, 2011

#include
#include

class matrix{
private:
int mat;
int row;
int column;
public:
matrix(int,int);
matrix(matrix&);
void getdata();
matrix operator-(matrix);
matrix operator+(matrix);
matrix operator*(matrix);
void display();
};

matrix::matrix(int r,int c)
{
cout<<"constructor to initiliaze no of rows and columns"< row=r;column=c;
}

matrix::matrix(matrix& m)
{
row=m.row;
column=m.column;

cout<<"Copy constructor"< for(int i=0;i {
for(int j=0;j {
mat[i][j]=m.mat[i][j];
}
}
}

void matrix::getdata()
{
cout<<"Value input:"< for(int i=0;i {
for(int j=0;j {
cout<<"Value("< cin>>mat[i][j];
}
}

}

matrix matrix::operator+(matrix a)
{
matrix temp(row,column);

for(int i=0;i {
for(int j=0;j {
temp.mat[i][j]=mat[i][j]+a.mat[i][j];
}
}
return temp;
}

matrix matrix::operator-(matrix a)
{
cout<<"Subtraction operator"<
matrix temp(row,column);

for(int i=0;i {
for(int j=0;j {
temp.mat[i][j]=mat[i][j]-a.mat[i][j];
}
}
return temp;
}

matrix matrix::operator*(matrix a)
{
cout<<"Multiplaction operator"<
matrix temp(row,column);

for (int i=0;i {
for (int j=0;j {
temp.mat[i][j]=0;
for(int k=0;k {
temp.mat[i][j]=temp.mat[i][j]+(mat[i][k]*a.mat[k][j]);
}
}
}

return temp;
}

void matrix::display()
{
cout<<"The matrix is "< for(int i=0;i {
for(int j=0;j {
cout< }
cout< }
}

int main()
{
clrscr();
matrix m1(2,2),m2(2,2),m3(2,2);

m1.getdata();
m2.getdata();

m3=m1+m2;
m3.display();

m3=m1-m2;
m3.display();

m3=m1*m2;
m3.display();

getch();
return 0;
}

#### binoy

• Aug 23rd, 2012

```Code#include<iostream.h>
#include<conio.h>
class matrix
{
private:long m;
int row;int col;
public:void getdata();
int operator ==(matrix);
matrix operator+(matrix);
matrix operator-(matrix);
friend ostream & operator << (ostream &,matrix &);
};
/* function to check whether the order of matrix are same or not */
int matrix::operator==(matrix cm)
{
if(row==cm.row && col==cm.col)
{
return 1;
}
return 0;
}
/* function to read data for matrix*/
void matrix::getdata()
{
cout<<"enter the number of rows
";
cin>>row;
cout<<"enter the number of columns
";
cin>>col;
cout<<"enter the elements of the matrix
";
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
cin>>m[i][j];
}
}
}
/* function to add two matrix */
matrix matrix::operator+(matrix am)
{
matrix temp;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
temp.m[i][j]=m[i][j]+am.m[i][j];
}
temp.row=row;
temp.col=col;
}
return temp;
}
/* function to subtract two matrix */
matrix matrix::operator-(matrix sm)
{
matrix temp;
for(int i=0;i<row;i++)
{
for(int j=0;j<col;j++)
{
temp.m[i][j]=m[i][j]-sm.m[i][j];
}
temp.row=row;
temp.col=col;
}
return temp;
}
/* function to display the contents of the matrix */
ostream & operator <<(ostream &fout,matrix &d)
{
for(int i=0;i<d.col;i++)
{
for(int j=0;j<d.col;j++)
{
fout<<d.m[i][j];
cout<<" ";
}
cout<<endl;
}
return fout;
}
/* main function */
void main()
{
matrix m1,m2,m3,m4;
clrscr();
m1.getdata();
m2.getdata();
if(m1==m2)
{
m3=m1+m2;
m4=m1-m2;
";
cout<<"the result is
";
cout<<m3;
cout<<"subtraction of matrices
";
cout<<"The result is
";
cout<<m4;
}
else
{
cout<<"order of the input matrices is not identical
";
}
getch();
}```

#### JoolsL

• Nov 19th, 2012

In both the above answers the op overloads should be using a const ref as input. You should have a (private) ctor that makes the matrix from the data, and should use RVO in the return.  