Question: C++ please, please shows the two programs separately and add some comments. Question ask creating a file matrix.hpp. Inside this file, start to write your

C++ please, please shows the two programs separately and add some comments. Question ask creating a file matrix.hpp. Inside this file, start to write your matrix template class. Heres a start for the class definition:

template

class Matrix {

private:

int _rows;

int _cols;

T** data;

public:

Matrix(int rows, int cols);

~Matrix();

int rows();

int cols();

T& at(int row, int col);

};

The constructor should allocate the data field so that it contains the specified number of rows and columns, and the destructor should free the memory allocated to the data field (in addition to anything else you need to do in the destructor). In addition to the constructor and destructor, you should write accessors to return the number of rows and the number of columns. You should also write an at() method that returns a reference to the data element at a specified location. By returning a reference, you can use the at() method to set values in a matrix as well as access them. The at() method should throw an std::out_of_range exception if either the specified row or the specified column is outside the bounds of the matrix. And all of the methods for a template class need to be implemented directly in the header file itself. Once you finish Matrix class create a new file main.cpp. In this file, write a small program to test your Matrix class. Create a few Matrix objects containing different data types, like int, float, and double. Fill those Matrix objects with values using loops and the at() method, and print out some values from the matrices to make sure they look like what you expect. Try to access some values outside the bounds of your matrices to make sure an exception is thrown.

Step 2, Implement an add() method to add two matrices

Add a new method add() to your Matrix template class to add two Matrix objects. The declaration of this method inside your class definition should look like this:

void add (const Matrix& other);

Importantly, note that the Matrix argument to you add () method will also be of the same type T. The add () method should simply take the value at each location in other and add it to the value at the corresponding location of the Matrix object represented by this, replacing the latter with the sum of the two values.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!