In a good way, you need to initialize the array in the constructor. You allocated memory, now there — in the array — completely random values ​​... This is for the Matrix (int n, int m) constructor Matrix (int n, int m) . In the Matrix() constructor, everything is generally fun - what values ​​do you assign n and m ? Yes, the same random that they were .
Even if you correct the constructor, when adding, you do not replace the non-initialized values ​​of the array, but add the sum of the elements to them , so there will still be garbage ...
Well, be sure to check the equality of the sizes of summable arrays! If not by generating an exception, then at least assert ...
So, throw away the default constructor, make one like this:
Matrix(int n = 1, int m = 1, int value = 0): n(n), m(m) { mas = new int*[n]; for (int i = 0; i < n; i++) { mas[i] = new int[m]; for(int j = 0; j < m; ++j) mas[i][j] = value; } }
B will have it both for you and for the reaper (and the usual constructor, and by default - create an 1x1 array), and even on the duda igrets - fill the array with default values ​​...
Well, in the operator + write not
result.mas[i][j] += right.mas[i][j]+ left.mas[i][j];
but
result.mas[i][j] = right.mas[i][j]+ left.mas[i][j];
and add a check at least assert(left.n == right.n && left.m == right.m); or
if (left.n != right.n || left.m != right.m) throw std::exception("Matrix sizes not matched in operator+()");
Well, and do not forget, as you have already written, the copy constructor. And the assignment operator does not hurt. About them already told here: Are the copy constructor and the assignment operator correct? How to improve?