27. Consider a class Matrix Class Matrix { int a[3][3]; Public: //methods; }; Let m1 and m2 are two matrices. Find out m3=m1+m2 (use operator overloading).


#include<iostream.h>
#include<conio.h>
class matrix
{
int a[3][3];
public:
void get();
void disp();
void operator +(matrix z);
};
void matrix::get()
{
cout<<“Enter the matrix:”;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cin>>a[i][j];
}
}
}
void matrix::disp()
{
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout<<a[i][j]<<” “;
}
cout<<“\n”;
}
}
void matrix::operator +(matrix z)
{
int mat[3][3];
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
mat[i][j]=a[i][j]+z.a[i][j];
}
}
cout<<“\nAddition of matrix:\n”;
for( i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
cout<<mat[i][j]<<” “;
}
cout<<“\n”;
}
}
void main()
{
matrix m,n;
clrscr();
m.get();
n.get();
cout<<“\nMatrix 1st: \n”;
m.disp();
cout<<“\nMatrix 2nd: \n”;
n.disp();
m+n;
getch();
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s