Question
c++ use those templates: template 1: #include using namespace std; class Matrix { private : // declare member fields for row, columns public : //
c++
use those templates:
template 1:
#include
using namespace std;
class Matrix
{
private:
// declare member fields for row, columns
public:
// declare member field for matrix data (int **), It would be better practice to put this under
// private and create a getter for it.
// declare non-default constructor, two parameters for row and column size
// declare destructor
// declare accessor functions ("getter" functions)
// declare functions to perform calculations and display output
Matrix* add(const Matrix&); // const is optional, but good practice. if you choose not to use it, make sure you update the definition below.
// as detailed in assignment
};
// Write function bodies for all public Matrix functions
//
// constructor dynamically allocates 2D array to hold matrix data based on row/col size
// destructor free up dynamically allocated memory using delete [], remember the "inside-out" rule
// add member function
Matrix* Matrix::add(const Matrix& m)
{
Matrix *sum = new Matrix( , ); // pass in correction information to invoke our constructor
// do calculations
return sum;
}
// remainder of calculations and member functions (like print)
int main()
{
// Prompt user for information for first matrix
// Collect input and create instance of Matrix
// Prompt user for information for second matrix
// Collect input and create second instance of Matrix
// Perform calculations as described in assignment using member functions
// Be sure to check matrices size for add/sub, and then mult
Matrix *sum = ; // invoke add function here
// Be sure to output details about calculation being performed and the results
return 0;
}
template 2:
#include
using namespace std;
class Matrix
{
private:
// declare member fields for row, columns
public:
// declare member field for matrix data (int **), It would be better practice to put this under
// private and create a getter for it.
// declare non-default constructor, two parameters for row and column size
// declare destructor
// declare accessor functions ("getter" functions)
// declare operator overloads using friend to perform calcuations as well as fill the matrix data and display output
friend Matrix* operator+(const Matrix&, const Matrix&);
// ...
friend ostream& operatorconst Matrix&);
// as detailed in assignment
};
// Write function bodies for all public Matrix functions
//
// constructor dynamically allocates 2D array to hold matrix data based on row/col size
// destructor free up dynamically allocated memory using delete [], remember the "inside-out" rule
// define operator overloads
Matrix* operator+(const Matrix& mLeft, const Matrix& mRight)
{
Matrix *sum = new Matrix( , ); // pass in correction information to invoke our constructor
// do calculations
return sum;
}
// remainder of operator overloads for calculations friended in the Matrix class
//
ostream& operatorconst Matrix& m)
{
// code
return os; // passing back the ostream reference we are given via os parameter
}
// define overloaded >>, receives and returns istream instead of ostream
int main()
{
// Remember you have to use >> and
// When you print Matrix pointers (object pointers), remember to dereference
// the pointer when you invoke
// Prompt user for information for first matrix
// Collect input and create instance of Matrix
// Prompt user for information for second matrix
// Collect input and create second instance of Matrix
// Perform calculations as described in assignment using member functions
// Be sure to check matrices size for add/sub, and then mult
Matrix *sum = ; // invoke + operator here
// Be sure to output details about calculation being performed and the results
// When you print Matrix pointers (object pointers), remember to dereference
// the pointer when you invoke
return 0;
}
1. (60 points) 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 mx 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: 15 1 (3 4 il 2 4 The individual elements in A can be expressed as dij, 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 01,2 is 2. Write a program (called matrix.cpp) that does that following: 1. Implement a class called Matrix that: . Contains private member fields for the number rows and columns of the matrix Contains a public member field to contain the matrix elements o This should be a 2D array of integers that is implemented dynamically Contains five public functions o add function, that adds two same sized matrices together and returns a new matrix with the result o subtract function, that subtracts two same sized matrices together and returns a new matrix with the result o multiply function, that performs proper matrix multiplication and returns a new matrix with the result scalar function, that performs scalar multiplication with an integer value and a matrix, and returns a new matrix with the result o print function, that outputs the contents of the matrix in tabular form that matches the dimensions of the matrix Contains a non-default constructor o Constructor that accepts size information, and dynamically creates the matrix Contains a destructor o That properly handles discarding the dynamically created 2D array (using delete and setting the member field to null O 2. Prompts the user for: The dimensions of a first matrix The contents of the first matrix, which is then filled into the newly created matrix object instance. The dimensions of a second matrix The contents of the second matrix, which is used to fill the newly created matrix instance . 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 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. 3. Performs the following calculations and prints each result using the print function . Each of the four 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. o 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, you can either use an integer literal or generate a random integer. Just be sure to 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. O O 2. (40 points) Matrix Class with Overloaded Operators Using the class from the first portion of the assignment, write a program (called matrix_ops.cpp) that does the following: 1. Overloads the following operators using friend: . Replicate the functionality of the add function from matrix.cpp . Replicate the functionality of the subtraction function from matrix.cpp Overload this to replicate the functionality of both the matrix multiplication and scalar multiplication functions from matrix.cpp This operator should be overloaded a total of 3 times. Replicate the functionality of the print function from matrix.cpp Used in the following way: cout > matrix_instancel; 2. Remove the five public functions of the Matrix class, and use only overloaded operators to perform the same functionality/output as in the matrix.cpp 3. Performs the following calculations and prints each result using the overloaded >> operator . O Each of the four matrix mathematical methods should be called, each result stored in a new object. Be sure you invoke scalar multiplication once for each overloaded operator int Matrix Matrix * int 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 o For scalar multiplication, you can either use an integer literal or generate a random integer. Just be sure to print the value of the integer value as part of the output when performing this function. o The calculation being performed should be explained; the contents of each matrix or value involved should be printed and identified. . Compiling the Program Use the following command to compile your classes: g++ -Wall -0 coutput_name>Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started