Question: Please implement the following operators in class Matrix. Overload the following operators 1. < < // Matrix output 2. >> // Matrix input 3. =

Please implement the following operators in class Matrix.

Overload the following operators

1. << // Matrix output

2. >> // Matrix input

3. = // assignment operator

4. + // Matrix + Matrix

5. + // Matrix + double,

// all values plus the double value

6. += // m1 = m1 + m2

7. ++ // m5++, all values plus 1.0

8. * // Matrix * Matrix

9.() // implement the transpose function,

// return the transpose of itself

10.== // test if two matrices contain the same values

__________________________________Matrix.h_____________________

#ifndef Matrix_h

#define Matrix_h

#include

using namespace std;

class Matrix {

private:

double ** data;

int row;

int col;

void setUp(int, int);

public:

Matrix();// default constructor

Matrix(int, int);//constructor with input r row and c column

Matrix(int, int, double [], int);//populate matrix with a one dimensional array

Matrix(const Matrix &);//copy constructor

~Matrix();// destructor

void setData(int, int, double);//set matrix cell(r,c) to a double data value

int getRow() const;//get matrix row

int getCol() const;//get matrix column

double getData(int, int) const;//get matrix cell (r,c) data value

Matrix add(const Matrix &);//add an incoming matrix with actual class matrix

Matrix multiply(const Matrix &);//multiply an incoming matrix with actual class matrix

Matrix transpose();//return current class matrix transpose

void displayData() {

for (int i = 0;i

for (int j = 0;j

cout<

}

cout<<""<

}

}

};

#endif /* Matrix_h */

__________________________Matrix.cpp__________________

#include "Matrix.h"

#include

using namespace std;

void Matrix::setUp(int r, int c){

row = r;

col = c;

data = new double*[row];

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

data[i] = new double [col];

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

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

data[i][j] = 0;

}

Matrix:: Matrix(){//default constructor

setUp(2,2);

}

Matrix::Matrix(int r, int c){//constructor with input r row and c column

setUp(r,c);

}

Matrix::Matrix(int r, int c, double a[], int size){//populate matrix with a one dimensional array

setUp(r, c);

int k = 0;

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

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

data[i][j] = a[k++];

}

Matrix::Matrix(const Matrix &m)//copy constructor

{

setUp(m.row, m.col);

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

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

data[i][j] = m.data[i][j];

}

Matrix::~Matrix()// destructor

{

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

delete[] data[i];

delete[] data;

}

void Matrix::setData(int r , int c, double val) //set matrix cell(r,c) to a double data value

{

data[r][c] = val;

}

int Matrix::getRow() const//get matrix row

{

return row;

}

int Matrix::getCol() const//get matrix column

{

return col;

}

double Matrix::getData(int r , int c) const//get matrix cell (r,c) data value

{

return data[r][c];

}

Matrix Matrix::add(const Matrix &m)//add an incoming matrix with actual class matrix

{

Matrix result(row, col);

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

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

result.data[i][j] = data[i][j] + m.data[i][j];

return result;

}

Matrix Matrix::multiply(const Matrix &m)//multiply an incoming matrix with actual class matrix

{

Matrix result(row, m.col);

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

{

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

{

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

result.data[i][j] += data[i][k] * m.data[k][j];

}

}

return result;

}

Matrix Matrix::transpose(){//return current class matrix transpose

Matrix result(col, row);

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

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

result.data[j][i] = data[i][j];

return result;

}

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!