fork download
  1. #include <iostream>
  2. #include <sstream>
  3. using namespace std;
  4.  
  5. template <class T>
  6. class Matrix
  7. {
  8. private:
  9. T arr[2][2];
  10.  
  11. public:
  12. Matrix();
  13. void display();
  14. void seter(T _var[2][2]);
  15.  
  16. Matrix operator + (Matrix tmp)
  17. {
  18. for(int i=0;i<2;i++)
  19. for(int j=0;j<2;j++)
  20. arr[i][j] += tmp.arr[i][j];
  21.  
  22. return *this;
  23. }
  24. };
  25.  
  26. template<class T>
  27. Matrix<T>::Matrix()
  28. {
  29. }
  30.  
  31.  
  32. template<class T>
  33. void Matrix<T>::display()
  34. {
  35. for(int i=0;i<2;i++)
  36. for(int j=0;j<2;j++)
  37. cout<<endl<<arr[i][j];
  38. }
  39.  
  40. template<class T>
  41. void Matrix<T>::seter(T _var[2][2])
  42. {
  43. for(int i=0;i<2;i++)
  44. for(int j=0;j<2;j++)
  45. arr[i][j]=_var[i][j];
  46. }
  47.  
  48. int main()
  49. {
  50. double arr1[2][2];
  51. double arr2[2][2];
  52.  
  53. double x=2.5,y=3.5;
  54.  
  55. for(int i=0;i<2;i++)
  56. for(int j=0;j<2;j++)
  57. arr1[i][j]=x++;
  58.  
  59. for(int i=0;i<2;i++)
  60. for(int j=0;j<2;j++)
  61. arr2[i][j]=y++;
  62.  
  63. Matrix<double> s1;
  64. Matrix<double> s2;
  65. Matrix<double> s3;
  66.  
  67. s1.seter(arr1);
  68. s2.seter(arr2);
  69.  
  70. s3=s1+s2;
  71.  
  72. s1.display();
  73. cout<<endl;
  74. s2.display();
  75. cout<<endl;
  76. s3.display();
  77.  
  78. return 0;
  79. }
  80.  
  81.  
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
6
8
10
12

3.5
4.5
5.5
6.5

6
8
10
12