Advertisement
Guest User

Matrix

a guest
Mar 21st, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1.  
  2.  
  3. class Matrix {
  4. private:
  5.     const int M_ROW;
  6.     const int M_COL;
  7.     double** p_matrix;
  8. public:
  9.     friend Matrix operator+(const Matrix& m1, const Matrix& m2);
  10.  
  11.     Matrix(const int row, const int col) : M_ROW{ row }, M_COL{ col }, p_matrix{ new double*[M_ROW]}{
  12.         for (int row{ 0 }; row < M_ROW; ++row) {
  13.             p_matrix[row] = new double[M_COL] {};
  14.         }
  15.     };
  16.  
  17. Matrix operator+(const Matrix& m1, const Matrix& m2)
  18. {
  19.     assert(m1.M_ROW == m2.M_ROW);
  20.     assert(m1.M_COL == m2.M_COL);
  21.  
  22.     Matrix temp{m1.M_ROW, m1.M_COL};
  23.  
  24.     for (int row{ 1 }; row <= m1.M_ROW; ++row) { // Indexing begins at 1 for irrelevant reasons i.e. ignore
  25.         for (int col{ 1 }; col <= m1.M_COL; ++col) {
  26.             temp(row, col) = m1(row, col) + m2(row, col);
  27.         }
  28.     }
  29.  
  30.     return temp;  // Function returns de-allocated object...
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement