Write C++ program for adding two matrices by overloading ‘+’ operator. Make necessary assumptions wherever required.
Answers
#include<iostream>
using namespace std;
class Matrix
{
int a[3][3];
public:
void accept();
void display();
void operator +(Matrix x);
};
void Matrix::accept()
{
cout<<"\n Enter Matrix Element (3 X 3) : \n";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout<<" ";
cin>>a[i][j];
}
}
}
void Matrix::display()
{
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
void Matrix::operator +(Matrix x)
{
int mat[3][3];
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
mat[i][j]=a[i][j]+x.a[i][j];
}
}
cout<<"\n Addition of Matrix : \n\n";
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<mat[i][j]<<"\t";
}
cout<<"\n";
}
}
int main()
{
Matrix m,n;
m.accept(); // Accepting Rows
n.accept(); // Accepting Columns
cout<<"\n First Matrix : \n\n";
m.display(); // Displaying First Matrix
cout<<"\n Second Matrix : \n\n";
n.display(); // Displaying Second Matrix
m+n; // Addition of Two Matrices. Overloaded '+' Operator
return 0;
}
Let m1 and m2 are two matrices. Find out m3=m1+m2 (use operator overloading).
#include<iostream>
using namespace std;
class Matrix
{
int a[3][3];
public:
void accept();
void display();
void operator +(Matrix x);
};
void Matrix::accept()
{
cout<<""\n Enter Matrix Element (3 X 3) : \n"";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout<<"" "";
cin>>a[i][j];
}
}
}
void Matrix::display()
{
for(int i=0; i<3; i++)
{
cout<<"" "";
for(int j=0; j<3; j++)
{
cout<<a[i][j]<<""\t"";
}
cout<<""\n"";
}
}
void Matrix::operator +(Matrix x)
{
int mat[3][3];
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
mat[i][j]=a[i][j]+x.a[i][j];
}
}
cout<<""\n Addition of Matrix : \n\n"";
for(int i=0; i<3; i++)
{
cout<<"" "";
for(int j=0; j<3; j++)
{
cout<<mat[i][j]<<""\t"";
}
cout<<""\n"";
}
}
int main()
{
Matrix m,n;
m.accept(); // Accepting Rows
n.accept(); // Accepting Columns
cout<<""\n First Matrix : \n\n"";
m.display(); // Displaying First Matrix
cout<<""\n Second Matrix : \n\n"";
n.display(); // Displaying Second Matrix
m+n; // Addition of Two Matrices. Overloaded '+' Operator
return 0;
}