实现Matrix类,包括Matrix的加减乘运算。
![Matrix](https://upload.wikimedia.org/wikipedia/commons/thumb/b/bf/Matris.png/220px-
Matris.png)
Matrix Class
A matrix is rectangular array of items laid out in rows and columns. The
dimensions, or size, of a matrix can be expressed as m x n or m-by-n, where m
is the number of rows in the matrix and n is the number of columns in the
matrix.
For example, consider A, which is the following 2 x 4 matrix:
[5 1 2 3]
[3 4 4 1]
The individual elements in A can be expressed as ai,j, where i (the row) is a
number from 1 to m and j (the column) is a number from 1 to n. For example,
the value at element a1,3 is 2.
Write a program (called matrix.cpp) that does that following:
Implement a class called Matrix that:
- Contains private member fields:
- int rows
- int columns
- Either an int* data or int** data
- You can use either of the two methods we discussed in class to implement the dynamic 2D array of integers
- Contains a non-default constructor
- Constructor that accepts the row and column information, and dynamically creates the matrix (using new, assign this to data)
- Contains a destructor
- That properly handles discarding the dynamically created 2D array (using delete[])
- Contains a copy constructor
- The copy constructor should perform a deep copy of the Matrix’s member fields
- Overloads the following operators as public member functions
- operator+
- adds two same sized Matrix objects together and returns a Matrix object that contains the sum
- operator-
- subtracts two same sized Matrix objects together and returns a Matrix object with the difference
- operator*
- performs proper matrix multiplication on any two Matrix objects and returns a Matrix object with the product
- performs scalar multiplication between an integer value and a Matrix object, and returns a Matrix object with the product
- You will need to overload operator* a total of 3 times
- Matrix * Matrix
- int * Matrix
- Matrix * int
- operator+
- Contains the two public functions
- Getter function for rows
- Getter function for columns
- Both should be implemented as const functions
- Publicly declares the
operator<<
function as a friend of the class Matrix- Be certain that the return type of the function is ostream&
- Accepts a Matrix& as its second parameter
Outside for class definition, write the global function definition foroperator<<
function that:
- Outputs the contents of the matrix in tabular form that matches the dimensions of the matrix
Implement the main function such that: - Prompts the user for:
- The dimensions of a first matrix, which are passed to the constructor when creating the first Matrix object
- The contents of the first Matrix object, which is collected after the object is already created (simplest method is to complete the
operator>>
function provided in the matrix_template.cpp file) - The dimensions of a second matrix, which are passed to the constructor when creating the second Matrix object
- The contents of the second Matrix object, which is collected after the object is already created (simplest method is to complete the
operator>>
function provided in the matrix_template.cpp file)
Sample prompts with appropriate user responses:
Number of Rows in Matrix 1: 5
Number of Columns in Matrix 1: 2
Values of Matrix 1 (expecting 10): 6 7 10 3 5 31 0 9 2 7
Number of Rows in Matrix 2: 5
Number of Columns in Matrix 2: 2
Values of Matrix 2 (expecting 10): 13 1 7 41 9 8 12 3 4 0
Note: You must use the above format for entering the values of the matrix.
When entering values to fill a matrix, all values should be provided on one
line.
- Performs the following calculations and prints each result using the overloaded
<< operator
- Each of the five matrix mathematical methods should be called, each result stored in a new object
- If the dimensions of the two matrices involved do not allow for the operation to be performed, skip performing this calculation, and display a message stating that step has been skipped.
- For example, if I have a 3x4 matrix and a 4x2 matrix, I cannot add or subtract these together, but I can perform multiplication
- Each calculation should be printed with a full explanation
- For scalar multiplication, be sure print the value of the integer value as part of the output when performing this function.
- The calculation being performed should be explained; and the contents of each matrix or value involved should be printed and identified.
General Guidelines
- All functions that accept a Matrix object as a parameter, that parameter should be passed by reference
- Use the matrix_template.cpp file in Canvas as the starting point for your program
- When performing calculations such as addition/subtraction/multiplication, be certain to initial the object with the functions’ return value, do not declare the object beforehand and assign it afterwards
Do not do this:
Matrix sum;
sum = m1 + m2;
—|—
Do this:
Matrix sum = m1 + m2;
—|—
Compiling the Program
Use the following command to compile your program:
g++ <program_name.cpp> -Wall -std=c++03 -pedantic -o
Example:
g++ matrix.cpp -Wall -std=c++03 -pedantic -o matrix
Remember: In order to be considered for grading, your submitted programs must
compile successfully without any errors. If your programs fail to compile, an
initial grade of a zero will be given for that program.