Question: Define a Matrix class to represent a dynamic matrix. Implement the following operations on the matrices using operator overloading. #ifndef Matrix_h #define Matrix_h #include class

Define a Matrix class to represent a dynamic matrix. Implement the following operations on the matrices using operator overloading.

#ifndef Matrix_h #define Matrix_h #include class Matrix { private: int rows; int cols; int **M;

public: Matrix(const int &rows, const int &cols); Matrix(const Matrix &other); ~Matrix(); int* & operator[](const int &index) const; void operator=(const Matrix &other); Matrix operator -()const; Matrix operator -(const Matrix &other)const; Matrix operator +(const Matrix &other)const; Matrix operator *(const Matrix &other)const; Matrix operator *(const int &num)const; Matrix operator++(); Matrix operator--(); int getMatrixRows(); int getMatrixCols(); friend Matrix operator *(const int & num, const Matrix &m) friend Matrix operator +(const int &num, const Matrix &t) friend ostream &operator<<(ostream &os, const Matrix &m) friend istream& operator>> (istream& in, const Matrix& m); }; #endif

Write a main function to test all overloading operators in the class Matrix.

Test driver

int main() { Matrix m1(2, 2); Matrix m2(2, 2); Matrix m3(2, 2); Matrix m4(2, 3); Matrix m5(3, 2); m1[0][0] = 2; m1[1][1] = 2; m2[0][0] = 1; m2[1][1] = 2; const Matrix s = -m1; //a. cout << m1 << endl << s << endl; m3 = m1 + m2; //b. cout << m3 << endl; m3 = m1 - m2; //c. cout << m3 << endl; m3 = m1 * m2; //d. cout << m3 << endl; m3 = 1 + m3; //e. cout << m3 << endl; m2 = m3; //f. cout << m2 << endl; m3 = 5*m3; //g. cout << m3 << endl; //h cout << m3[1][1] << endl; m2=++m3; //k cout << m2 << endl; //l cout << m3 << endl; cin >> m4; cin >> m5; m1 = m4*m5; //m cout << m1; return 0; }

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!