Sample Assignment: The Dynamic Matrix Class
|
|
matrix.h:
The header file for the revised matrix
class.
matrix.cxx: The implementation file for the revised
matrix class.
matrixtest.cxx
A simple interactive test program. You may probably use the
same progam from last week. It may be written
on your own, or you may work together with one or two
other students (sharing your test code).
Following the prescription in Section 4.4 of the textbook, you will revise your matrix class from last week to use a dynamic two-dimensional array. Since this is your first time using dynamic memory, please follow these steps precisely. If you come for help, we will ask you which steps you have completed and which one you are working on now.
value_type** data;
size_type many_rows, many_columns;
void matrix::make_dynamic_array(size_type rows, size_type columns)
// Precondition: data does not yet point to dynamic memory.
// Postcondition: data points to a new 2-d array of size rows x columns
// containing all zeros, and num_rows and num_columns are correctly set.
The techniques for creating the 2-d array will be covered in
class. Remember: you must put the prototype for the helper function in
the .h file, but the implementation and the precondition/postcondition
contract go in the .cxx file.
void matrix::delete_dynamic_array( )
// Postcondition: All dynamic memory used by data has been released, and
// num_rows and num_columns are both zero.
Though this is
based on ideas for one-dimensional dynamic arrays from Chapter 4,
you'll also need new techniques for this from lecture topics.
matrix::matrix(const matrix& source)
{
1. Make this matrix's array the same size as source's.
2. Use loops to copy the data from the source to this matrix.
}
matrix& matrix::operator = (const matrix& source)
{
if (this != &source)
{ // This is not a self-assignment, so do the work:
1. Delete the old memory
2. Do the same work as the copy constructor:
}
return *this;
}