有两个矩阵a和b,均为2行3列。求两个矩阵之和

有两个矩阵a和b,均为2行3列。求两个矩阵之和。重载运算符“+”,使之能用于矩阵相加,如:c=a+bios

 

  
  
  
  
  1. #include<iostream>  
  2. using namespace std;  
  3. class Arrary  
  4. {  
  5. public:  
  6.     Arrary();  
  7.     void cin_();  
  8.       
  9.     int a[2][3];  
  10.     void display();  
  11. };  
  12.  
  13. Arrary::Arrary()  
  14. {  
  15.         int i,j;  
  16.     for(i=0;i<2;i++)  
  17.         for(j=0;j<3;j++)  
  18.         a[i][j]=0;  
  19.           
  20. }  
  21. void Arrary::cin_()  
  22. {  
  23.     cout<<"请输入数组:";  
  24.           int i,j;  
  25.     for(i=0;i<2;i++)  
  26.         for(j=0;j<3;j++)  
  27.         cin>>a[i][j];  
  28. }  
  29. void Arrary::display()  
  30. {  
  31.      
  32.         int i,j;  
  33.     for(i=0;i<2;i++)  
  34.     {   for(j=0;j<3;j++)  
  35.         cout<<a[i][j]<<" ";  
  36.     cout<<endl;  
  37.     }  
  38. }  
  39. Arrary operator +(Arrary c1,Arrary c2)  
  40. {  
  41.     Arrary c3;  
  42.     int i,j;  
  43.     for(i=0;i<2;i++)  
  44.         for(j=0;j<3;j++)  
  45.         {  
  46.             c3.a[i][j]=c1.a[i][j]+c2.a[i][j];  
  47.         }  
  48.         return c3;  
  49. }  
  50.  
  51.  
  52. int main()  
  53. {  
  54.     
  55.  
  56.     Arrary a,b,c;  
  57.     a.cin_();  
  58.     b.cin_();  
  59.     cout<<endl;  
  60.     a.display();  
  61.     cout<<endl;  
  62.     b.display();  
  63.     cout<<endl;  
  64.     c=a+b;  
  65.     c.display();  
  66.