有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加,如:c=a+b,重载流插入运算符<< 和流提取运算符>>,使之能用于矩阵的输入输出。
- #include<iostream>
- using namespace std;
- class Arrary
- {
- public:
- Arrary();
- int a[2][3];
- };
- Arrary::Arrary()
- {
- int i,j;
- for(i=0;i<2;i++)
- for(j=0;j<3;j++)
- a[i][j]=0;
- }
- istream & operator >>(istream &input,Arrary &c1)
- {
- cout<<"请输入数组:";
- int i,j;
- for(i=0;i<2;i++)
- for(j=0;j<3;j++)
- input>>c1.a[i][j];
- return input;
- }
- ostream & operator <<(ostream &output,Arrary &c1)
- {
- int i,j;
- for(i=0;i<2;i++)
- { for(j=0;j<3;j++)
- output<<c1.a[i][j]<<" ";
- output<<endl;
- }
- return output;
- }
- Arrary operator +(Arrary c1,Arrary c2)
- {
- Arrary c3;
- int i,j;
- for(i=0;i<2;i++)
- for(j=0;j<3;j++)
- {
- c3.a[i][j]=c1.a[i][j]+c2.a[i][j];
- }
- return c3;
- }
- int main()
- {
- Arrary a,b,c;
- cin>>a;
- cin>>b;
- c=a+b;
- cout<<c;
- }