In this homework, you are asked to implement a class for integer matrices and overload some operators. The details of these operators and the functions that you will implement are given below. Our focus in this homework is on the class design and implementation. The usage of this class in the main program will be given to you.
Matrix Class Design
You have to keep the rowNumber, columnNumber and elements of the matrix as private data members of the class. Elements of the matrix must be stored in a dynamic matrix of integers with rowNumber rows and columnNumber elements per row. Since the matrix is dynamic one, as elements, you will need to use a pointer to pointer. Dynamic matrices were covered in week 4 of the course. Please refer to lecture notes to recall.
Your class implementation must include the following basic class functions:
• Default constructor: creates a Matrix object with zero rows and zero columns This constructor does not allocate memory for the elements of the matrix.
• Constructor with rowNum, columnNum, init parameters: creates a Matrix object with rowNum rows and columnNum columns, and allocates enough memory. Initial values of all of the elements must be set to init. Please refer to week 4 material about creating a dynamic matrix via pointers. If rowNum and/or columnNum is/are non-positive, then an empty matrix with zero rows and zero columns must be created.
• Deep copy constructor: takes a Matrix object as const-reference parameter and creates a new Matrix object as the deep copy of the parameter.
• Destructor: By definition, destructor deletes all dynamically allocated memory of the object and returns them to the heap.
In this homework, you will overload several operators for the Matrix class. These operators and the details of overloading are given below. You can overload operators as member or free functions.
+ This operator will be overloaded such that it calculates and returns the summation of two Matrix operands. The mathematical definition of + operation on matrices and is given below. Here assume that the dimensions of the operands are the same; you do not need to check the dimension equality in the operator implementation.
- This operator will be overloaded such that it calculates and returns the difference of two Matrix operands. The mathematical definition of - operation on matrixes and is given below. Here assume that the dimensions of the operands are the same; you do not need to check the dimension equality in the operator implementation.
=
This operator will be overloaded such that it will assign the Matrix object on the right handside of the operator to the Matrix object on the left hand-side. This function must be implemented in a way to allow cascaded assignments. Due to C++ language rules, this operator must be a member function (cannot be free function). == This operator will be overloaded such that it will check two Matrix objects for equality. If the sizes of these two matrices are not the same, then this operator directly returns false. If the sizes are the same, then all of the corresponding elements of the matrices must be the same in order to return true. Otherwise, the operator returns false.
! This operator will be overloaded such that it will return the transpose of the matrix operand. In linear algebra, the transpose of a matrix is an operator which switches the rows with columns.
Note that this is a unary operator (i.e. it takes a single operand). That means, if you implement it as a member function, the function does not take any parameters and processes the object on which the operator function is called. Alternatively, you can implement it as a free function; in this case, the function takes only one const-reference Matrix parameter and processes it. In either case, the transposed value must be returned from the function as value (i.e. not reference) and you will not change the content of its operand.
The mathematical definition of transpose is given below.
Other than these operators, you will need to implement the following accessors (getters) and mutator (setter) as member functions.
getRowNumber returns the number of rows of the Matrix object. No parameters. getColumnNumber returns the number of columns of the Matrix object. No parameters. getElementAt takes row and column indices as parameters and returns the matrix element at this position setElementAt takes row and column indices, and an integer value as parameters. The function does not return anything but assigns the parameter value to the corresponding matrix element.
You also need to write member function named print that does not take any parameters, does not return anything, but displays the content of the matrix.
A life-saving advice: The getters above need to be const member functions so that they work fine with const-reference parameters. If you do not remember what "const member function" is, please refer to CS201 notes or simply Google it!
In order the see the usage and the effects of these operators, please see sample runs and the provided main.cpp.
In this homework, you are allowed to implement some other helper member functions, accessors and mutators, if needed. However, you are not allowed to use friend functions and friend classes.
You have to have separate header and implementation files, in addition to the main.cpp.
Please do not make use any standard or non-standard container or matrix classes in your homework; you will implement your own class from scratch.
main.cpp In this homework, main.cpp file is given to you within this homework package and we will test your codes with this main function with different inputs. You are not allowed to make any modifications in the main function (of course, you can edit the file to add #includes, etc. to the beginning of the file). Inputs are explained with appropriate prompts so that you do not get confused. We strongly recommend you to examine main.cpp file and sample runs before starting your homework.
Sample Runs
Some sample runs are given below, but these are not comprehensive, therefore you have to consider all possible cases to get full mark. Especially try different matrix values (other than the ones given in the sample runs) and extreme cases.
Sample Run 1:
Please enter the row number of Matrix m1:
2 Please enter the column number of Matrix m1:
3 Please enter the init value of Matrix m1:
1
Matrix m1:
1 2 3
2 3 4
Please enter the row number of Matrix m2:
2
Please enter the column number of Matrix m2:
3 Please enter the init value of Matrix m2: 1
Matrix m2: 1 2 3
2 3 4
Please enter the row number of Matrix m3:
3 Please enter the column number of Matrix m3:
3 Please enter the init value of Matrix m3: 1
Matrix m3: 1 2 3
2 3 4
3 4 5
Matrix m4:
1 2 3
2 3 4
3 4 5
m1 is equal to m2.
m3 = m1 + m2 + m1:
3 6 9
6 9 12 m4 = m2 - m1 - m2:
-1 -2 -3
-2 -3 -4
Transpose of m3:
3 6
6 9
9 12
Assigning m4 = m2 = m3.
Matrix m4:
3 6
6 9
9 12
Matrix m2:
3 6
6 9
9 12
Matrix m3:
3 6
6 9
9 12
Sample Run 2:
Please enter the row number of Matrix m1:
2
Please enter the column number of Matrix m1:
5
Please enter the init value of Matrix m1:
1
Matrix m1: 1 2 3 4 5
2 3 4 5 6
Please enter the row number of Matrix m2:
2
Please enter the column number of Matrix m2:
2
Please enter the init value of Matrix m2:
2
Matrix m2: 2 4
4 6
Please enter the row number of Matrix m3:
1
Please enter the column number of Matrix m3:
1
Please enter the init value of Matrix m3: 1
Matrix m3:
1
Matrix m4:
1
m1 is not equal to m2.
m3 = m1 + m2 + m1:
Matrix m1 and m2 do not have the same dimensions. Cannot be added.
m4 = m2 - m1 - m2:
Matrix m1 and m2 do not have the same dimensions. Cannot be subtracted.