Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overloads the following operators using friend + replicate the functionality of the add function from matrix.cpp - replicate the functionality of the subtraction function from

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

If the dimensions of the two matrices involved do not allow for the operation to be performed, skip performing the calculation, and display a message.

#include

using namespace std;

class Matrix{

private:

int row,col;

public:

int **arr;

// private and create a getter for it.

int getValue(int r, int c){

return arr[r][c];

}

Matrix(){}

Matrix (int rowSize, int colSize){ // non default contructor

row = rowSize;

col = colSize;

arr = new int*[row];

for(int i=0;i

arr[i] = new int[col];

}

}

~Matrix (){ //destructor

}

// declare accessor functions ("getter" functions)

Matrix operator + (Matrix & m){

Matrix *newMatrix = new Matrix(row,col);

for(int i=0;i

for(int j=0;j

newMatrix->arr[i][j] = this->arr[i][j] + m.arr[i][j];

}

return *newMatrix;

}

Matrix operator += (Matrix & m){

for(int i=0;i

for(int j=0;j

this->arr[i][j] += m.arr[i][j];

}

return *this;

}

Matrix operator += (const int &num){

for(int i=0;i

for(int j=0;j

this->arr[i][j] += num;

}

return *this;

}

Matrix operator * (Matrix & m){

Matrix *newMatrix = new Matrix(row,col);

for (int i = 0; i < row; i++)

{

for (int j = 0; j < m.col; j++)

{

int result = 0;

for (int k = 0; k < col; k++)

result += this->arr[i][k] * m.arr[k][j];

newMatrix->arr[i][j] = result;

}

}

return *newMatrix;

}

Matrix operator ++(){

for(int i=0;i

for(int j=0;j

this->arr[i][j] ++;

}

return *this;

}

friend Matrix operator +(const int &num, const Matrix &m){

Matrix *newMatrix = new Matrix(m.row,m.col);

for(int i=0;i

for(int j=0;j

newMatrix->arr[i][j] = num + m.arr[i][j];

}

return *newMatrix;

}

friend istream& operator>> (istream& in, const Matrix& m){ //cin input stream

for(int i=0;i

for(int j=0;j

in>>m.arr[i][j];

}

return in;

}

friend ostream &operator<<(ostream &os, const Matrix &m){ //cout output stream

for(int i=0;i

for(int j=0;j

os<

os<<" ";

}

return os;

}

};

int main(){

int column1, row1, column2,row2;

cout<<"Please enter the Rows and Columns in Matrix1."<

cin>>row1>>column1;

cout<<" Enter the values."<

Matrix matrix1 (row1,column1);

cin>>matrix1;

cout << " Matrix 1:" << endl;

cout<

cout<<"Please enter the Rows and Columns in Matrix2."<

cin>>row2>>column2;

cout<<" Enter the values."<

Matrix matrix2 (row2,column2);

cin>>matrix2;

cout << "Matrix 2:" << endl;

cout<

cout << " Result of matrix1 + matrix2: " << endl;

Matrix addResult ;

addResult = matrix1 + matrix2;

cout<

}

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

Students also viewed these Databases questions