fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. //write your code here
  5. class Matrix{
  6. private:
  7. int arr[3][3];
  8. public:
  9. friend istream& operator >> (istream& in,Matrix &matrix);
  10. friend ostream& operator << (ostream& out,const Matrix& matrix);
  11. friend Matrix operator + (Matrix &a,Matrix &b);
  12. void setArr(int i,int j,int data){
  13. arr[i][j] = data;
  14. }
  15. int getArr(int i,int j) const{
  16. // ^^^^^ // Added const
  17. return arr[i][j];
  18. }
  19. Matrix& operator = (const Matrix &b);
  20. // ^^^^^ // Added const
  21. };
  22. istream& operator >> (istream& in,Matrix &matrix)
  23. {
  24. for(int i=0;i<3;i++)
  25. for(int j=0;j<3;j++)
  26. in >> matrix.arr[i][j];
  27. return in;
  28. }
  29. ostream& operator << (ostream& out,const Matrix &matrix){
  30. for(int i=0;i<3;i++)
  31. {
  32. for(int j=0;j<3;j++)
  33. out << matrix.arr[i][j] << " ";
  34. out << endl;
  35. }
  36. return out;
  37. }
  38. Matrix operator + (Matrix &a,Matrix &b){
  39. Matrix temp;
  40. for(int i=0;i<3;i++)
  41. for(int j=0;j<3;j++)
  42. temp.setArr(i,j,(a.getArr(i,j)+b.getArr(i,j)));
  43. return temp;
  44.  
  45. }
  46. Matrix& Matrix::operator = (const Matrix &b){
  47. // ^^^^^ // Added const
  48. for(int i=0;i<3;i++)
  49. for(int j=0;j<3;j++)
  50. arr[i][j] = b.getArr(i,j);
  51. return *this;
  52. }
  53. int main()
  54. {
  55. Matrix a1,a2,a3,a4;
  56. cin >> a1;
  57. cin >> a2;
  58. a4 = a2;
  59. a3 = (a1+a4); // No errors now
  60. cout << a1 << endl;
  61. cout << a2 << endl;
  62. cout << a3 << endl;
  63. return 0;
  64. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
-1215588300 -1215598592 -1215414272 
-1215411920 1 -1079022288 
-1215488865 -1215411480 -1216923328 

1 1 0 
-1216237748 -1215593436 -1215594304 
-1079022316 -1218466669 134513424 

-1215588299 -1215598591 -1215414272 
1863317628 -1215593435 2000350704 
2000456115 1861089147 -1082409904