矩阵相加
#include<iostream.h>class Matrix
{
public:
Matrix();
void input();
void display();
friend Matrix operator +(Matrix &,Matrix &);
protected:
int mat[2][3];
};
Matrix::Matrix()
{
for(int i=0;i<2;i++)
for(int j=0;i<3;j++)
mat[i][j]=0;
}
void Matrix::input()
{
cout<<"input value of matrix:"<<endl;
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
cin>>mat[i][j];
}
void Matrix::display()
{
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
{
cout<<mat[i][j]<<" ";
cout<<endl;
}
}
Matrix operator +(Matrix &a,Matrix &b)
{
Matrix c;
for(int i=0;i<2;i++)
for(int j=0;j<3;j++)
c.mat[i][j]=a.mat[i][j]+b.mat[i][j];
return c;
}
int main()
{
Matrix a,b,c;
cout<<"input a Matrix:"<<endl;
a.input();
b.input();
cout<<endl<<"intput a Matrix:"<<endl;
a.display();
cout<<endl<<"intput b Matrix:"<<endl;
b.display();
c=a+b;
cout<<endl<<"Matrix c=Matrix a+Matrix b:"<<endl;
c.display;
return 0;
}