Matrix with Dynamic Size

  1. Modify your class CMatrix which can only store a 3*3 matrix. From now on, we want to accommodate a matrix with size m by n (m_Row = m and m_Column = n, and m is not necessarily equal to n). Therefore, you'll need the technique of dynamic memory allocation, as illustrated in P.438.
  2. Design the following member functions:
    1. A constructor CMatrix(int n = 0), which will dynamically allocate an integer array with n*n entries, let m_Row = m_Column = n, and initialize all entries to become 0.
    2. Another constructor CMatrix(int m, int n, int v = 0), which will dynamically allocate an integer array with m*n entries, let m_Row = m, m_Column = n, and initialize all entries to be v.
    3. A copy constructor CMatrix(CMatrix& B) which will allocate an integer array with the same size as matrix B, and duplicate the corresponding entries in matrix B.
    4. A destructor to release the integer array allocated for this object, and print out a message like "Destructor called for a m*n matrix", where m is the value of m_Row, and n is the value of m_Column.
    5. Print() - print out the matrix with m_Row rows and m_Column columns. Note that integers in a column should be right-justified.
    6. MaxEntry() - returns the maximum of absolute value among all entries. This is helpful to determine the maximum width required in printing entries when you want to keep them right-justfied.
    7. Because m_Row and m_Column are private data members, you need to design public member functions Row() and Column() to return the value of m_Row and m_Column, respectively.
    8. Get(i, j): returns the value of the (i*m_Column+j)th entry of the dynamically allocated array.
    9. Set(i, j, n): assigns n to the (i*m_Column+j)th entry of the dynamically allocated array.

    Test your class definition with this main program. Save your class definition in "matrix.h" and compile the program with "clang++ matrix-4.cpp". The running result should look like:

    
    
    Constructor called for a 3*4 matrix.
    Copy Constructor called for a 3*4 matrix.
     1 1 1 1
     1 1 1 1
     1 1 1 1
     88  1  1  1
      1  1  1  1
      1  1  1  1
    Destructor called for a 3*4 matrix.
    Destructor called for a 3*4 matrix.