Operator overloading - c++ program:
Write a program in c to define operator overloading.
class matrix{
private :
int x[2][2];
public:
void show();
matrix(int a,int b,int c,int d);
matrix() //default constructor
{
for (int i= 0;i<2;i++)
for(int j=0;j<2;j++)
x[i][j]=0;
}
friend matrix operator + (matrix a,matrix b);
};
matrix operator +(matrix a,matrix b) //optr over loading +
{
matrix c;
for (int i= 0;i<2;i++)
for(int j=0;j<2;j++)
c.x[i][j]=a.x[i][j] + b.x[i][j];
return c;
}
matrix operator -(matrix a,matrix b) //optr over loading +
{
matrix c;
for (int i= 0;i<2;i++)
for(int j=0;j<2;j++)
c.x[i][j]=a.x[i][j] - b.x[i][j];
return c;
}
void matrix ::show()
{
for (int i= 0;i<2;i++)
for(int j=0;j<2;j++)
cout <<"\n"<
}
matrix:: matrix (int a,int b,int c,int d)//constructor definition
{
x[0][0]=a;
x[0][1]=b;
x[1][0]=c;
x[1][1]=d;
}
void main()
{
matrix a,b(2,2,2,2),c;
a=matrix(1,1,1,1);
clrscr();
c=a+b;
c.show();
c=a-b;
c.show();
getch();
}