Create a 'MATRIX' class of size m X n. Overload the ‘+’
operator to add two MATRIX objects. Write a main function to implement it.
#include<iostream.h>
#include<conio.h>
class mat
{
int m,n,a[20][20]; public:
mat(int x,int y); void
readmat(); mat operator +(mat); void display();
};
mat :: mat(int x,int
y)
{
m=x;n=y;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
a[i][j]=0;
}
}
void mat :: readmat()
{
cout<<"\nenter matrix elements\n"; for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
}
mat mat:: operator +(mat
obj)
{
mat temp(m,n); for(int
i=0;i<m;i++) for(int j=0;j<n;j++)
{
temp.a[i][j]=a[i][j]+obj.a[i][j];
}
return temp;
}
void mat:: display()
{
int i,j; for(i=0;i<m;i++)
{
cout<<"\n\n";
for(j=0;j<n;j++)
cout<<"\t"<<a[i][j];
}
}
int main()
{
int m1,n1; clrscr();
cout<<"\nEnter the size(m,n) of matrix: "; cin>>m1>>n1;
mat a(m1,n1),b(m1,n1),c(m1,n1);
cout<<"\nEnter martix 1: "; a.readmat();
cout<<"\nEnter matrix 2: "; b.readmat();
c=a.operator +(b); cout<<"\nFirst Matrix :\n"; a.display();
cout<<"\nSecond Matrix :\n"; b.display();
cout<<"\nmatrix 1+matrix 2: "; c.display();
getch(); return 0;
}
********************************OUTPUT**********************************
Enter the size(m,n) of
matrix: 2 2
Enter martix 1: enter matrix elements 3 3 3 3
Enter matrix 2: enter matrix elements 4 4 4 4
First Matrix :
3
|
3
|
3
|
3
|
Second Matrix :
|
|
4
|
4
|
4
|
4
|
matrix 1 + matrix 2:
7
|
7
|
7
|
7
|