Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Step 2: Start a simple Matrix template class In this lab, youll be writing a template class to represent a matrix. Because its a template,

Step 2: Start a simple Matrix template class

In this lab, youll be writing a template class to represent a matrix. Because its a template, your matrix class will be able to work with different types of underlying data.

Start by creating a new 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); };

Here, 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.

Remember, all of the methods for a template class need to be implemented directly in the header file itself.

Once you have your Matrix class looking good, create a new file application.cpp. In this file, write a small application 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.

Make sure to add and commit your files to your git repository and push them to GitHub.

Step 3: 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 your 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.

Make sure to augment your application to test your add() method, commit your changes to your git repository, and push them to GitHub when everything is working.

Step 4: Write a size_mismatch exception and throw it in add()

One major catch about adding two matrices is that they must have the same dimensions. Otherwise, its not possible to add them. Your Matrix class should not allow the user to pass an argument other to the add() method that has different dimensions than the Matrix object represented by this. To do this, you should create a new file size_mismatch.hpp and, in it, write a new exception class called size_mismatch. This class should be derived from the std::exception class. Remember, at a minimum, a new class that derives from std::exception must implement the following method: virtual const char* what() const throw(); However, you may implement any additional methods that help you make an informative exception (e.g. a new parameterized constructor). You may also add new private data members if needed. You may place the implementations of the class methods for size_mismatch directly in the class definition in size_mismatch.hpp, or you may create a new file size_mismatch.cpp in which to place these implementations.

Once you have your size_mismatch class written, augment your Matrix classs add() method to throw a size_mismatch whenever the other argument has different dimensions than the Matrix object represented by this.

As always, make sure to add your new files and commit your changes to your git repository, and push them to GitHub.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Databases With PostgreSQL From Novice To Professional

Authors: Richard Stones, Neil Matthew

2nd Edition

1590594789, 978-1590594780

More Books

Students also viewed these Databases questions