The rule of three

Matrix Class in C++

Create a class Matrix to describe a matrix. The element type is double. One member of the class is shared_ptr<double[]> for the matrix data. The two matrices can share the same data through a copy assignment. The copy constructor constructs a hard copy of the object.

  • Create a class Matrix in Matrix.h and Matrix.cpp.
  • Create tests in main.cpp and write CMakeLists.txt to compile the code.
  • Zip and upload the code to Canvas.

The following code should run smoothly without memory problems.

Example Code

class Matrix {
    // Class implementation here
};

Matrix a(3, 4);   // Create a 3x4 matrix
Matrix b(3, 4);   // Create another 3x4 matrix
Matrix c(b);      // Use the copy constructor to create a hard copy of 'b'
Matrix d;
d = b;            // Use copy assignment; both matrices share the same data