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
inMatrix.h
andMatrix.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
};
(3, 4); // Create a 3x4 matrix
Matrix a(3, 4); // Create another 3x4 matrix
Matrix b(b); // Use the copy constructor to create a hard copy of 'b'
Matrix c;
Matrix d= b; // Use copy assignment; both matrices share the same data d