c++ program for addition and multiplication of two matrices using classes and object using operator overloading:
addittion and multiplication of two matrixes involves for building a software wich can add or multiply two matrices and give the result: program is given below
solution
solution
#include <iostream>
using namespace std;
class function
{
public:
int a[10][10],i,j,n,m,k;
void input()
{
cout<<"enter order of matrix in m by n:";
cin>>m>>n;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
}
function operator +(function obj2)
{
function temp;
temp.n=n;
temp.m=m;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
temp.a[i][j]=a[i][j]+obj2.a[i][j];
}
}
return temp;
}
function operator *(function obj2)
{
function temp;
temp.n=n;
temp.m=m;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
temp.a[i][j]=0;
for(k=0;k<n;k++)
temp.a[i][j]+=a[i][k]*obj2.a[k][j];
}
return temp;
}
void show()
{
for(i=0;i<m;i++)
{
for( j=0;j<n;j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}
}
};
int main()
{
function obj,obj2,result;
obj.input();
obj2.input();
result=obj+obj2;
cout<<"matrix 1 is:\n";
obj.show();
cout<<"matrix 2 is:\n";
obj2.show();
cout<<"sum of matrix :\n";
result.show();
result=obj*obj2;
cout<<"product of matrixes is:\n";
result.show();
return 0;
}
output:
0 comments: