Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Fill in the code needed for the operator * as well as the deconstructor ~Matrix. This is the code at the bottom included in the

Fill in the code needed for the operator* as well as the deconstructor ~Matrix. This is the code at the bottom included in the image. Please use the remaining code here as a foundation to finish the rest #include
#include
#include
#include
using namespace std;
//This forward declaration of Matrix class is necessary because the following operator>> and are using Matrix as a parameter
template
class Matrix;
//This forward declaration of operator>> is necessary Since operator is a template function.
template
ifstream& operator>>(ifstream& o, Matrix& m);
//This forward declaration of operator is necessary Since operator is a template function.
template
ostream& operator(ostream& o, const Matrix & m);
//Template class so we can have a matrix of any data types, int or double
template
class Matrix
{
//friend functions so they can access the private members
friend ostream& operator(ostream& o, const Matrix & m);
friend ifstream& operator>>(ifstream& o, Matrix& m);
private:
T** m; //2 dimensional dynamic array
const int R; //number of rows
const int C; //number of columns
public:
Matrix(int R, int C);
Matrix();
~Matrix();
Matrix operator*(const Matrix& other);// matrix multiplication
class size_error{};//exception class
}; //default constructor
template
Matrix::Matrix() : R(0), C(0)//setting const R and C to initial value, 0.
{
m = NULL;
}
//constructor to create a matrix, row x col
template
Matrix::Matrix(int row, int col) : R(row), C(col)//setting const R and C
{
if(row =0|| col =0)
{
m = NULL;
return;
}
//m = new T[R][C]; //this doesn't compile
m = new T*[R]; //create a single dimensional array of pointers of the T type
for (int i=0; i
ifstream& operator>>(ifstream& fin, Matrix& mrx)
{
for (int i =0; i mrx.R; i++){
for (int j =0; j mrx.C; j++){
fin >> mrx.m[i][j];
}
}
return fin;
}
/*output the matrix to screen in the following format. Allocate 10 spaces for each value.
111
222
333
444
*/
template
ostream& operator(ostream& o, const Matrix& mrx)
{
for (int i =0; i mrx.R; i++){
for (int j =0; j mrx.C; j++){
o setw(10) mrx.m[i][j];
}
o endl; // move to the next line after printing each row
}
return o;
}//matrix multiplication
template
Matrix Matrix::operator*(const Matrix& other)
{
/*
//check if the 2 matrices are comparable. If m1 is mxn, m2 has to be nxk.
if(??????????)//cannot compute because of incomparable sizes
?????????; //throw an exception
????//multiple lines of code
*/
}
//destructor
template
Matrix::~Matrix()
{
//destroy what is created in heap memory
//if m is not NULL do the following
//HINT: delete each dynamic array pointed to by each slot of m
//delete m (m contains all rows)
}
image text in transcribed

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

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

More Books

Students also viewed these Databases questions