You will implement the Matrix class class matrix public mat
You will implement the Matrix class:
class matrix
{ public:
matrix();
// Copy ctor, assignment operator, destructor
// friend operator+ adds two matrices, returns result by value
// operator>> and operator<<
private:
int numRows = 0; // number of rows
int numCols = 0; // number of cols
double **mArray = nullptr;
};
Only use libraries iostream, fstream, and cstring. The matrix object stores a two dimensional array (of doubles) with numRows rows and numCols columns. Space for the array should be dynamically allocated, for the exact amount needed.
Default ctor: Empty body.
Copy ctor: Makes an independent copy (deep copy).
Destructor: Releases all dynamically allocated space.
Assignment operator: Allows cascading (ex. m1 = m2 = m3).
friend operator+ adds two matrices, returns result by value
operator>>: Read a matrix from the input file, store it in a previously created object. Allow cascading (ex. cin >> m1 >> m2).
operator<<: Output the matrix. Allow cascading (ex. cout << m1 << m2).
The input file contains three matrices. For each matrix, one line gives the number of rows, followed by the number of columns. Immediately following that, the matrix entries are given row by row. See sample input file infile1.
Suggestion: Have private member functions void allocateSpace() and void deleteSpace(); they allocate and deallocate space for a matrix, respectively. allocateSpace() is called from copy ctor, operator=, and operator>>. deleteSpace() is called from dtor, operator=, and operator>>.
Further:
• For efficiency reasons, you should have friends and members directly access other data members.
• Declare const whenever appropriate. Use references (instead of values), for parameters and returned items, whenever appropriate.
• Remember to use #ifndef in your header file.
• No comments or other documentation is needed. Use meaningful names for all variables. You must use main.cpp; no changes are allowed.
main.cpp
#include <iostream>
using namespace std;
#include \"Matrix.h\"
int main()
{ Matrix m1, m2, m3;
cin >> m1 >> m2 >> m3;
cout << \"m3 is:\ \" << m3 << endl;
Matrix m4(m1);
cout << \"m4 is:\ \" << m4 << endl;
m4 = m4;
cout << \"m4 is:\ \" << m4 << endl;
Matrix m5;
m4 = m5 = m2;
cout << \"m4 is:\ \" << m4 << endl;
Matrix m6;
m6 = m1+m3;
cout << \"m6 is:\ \" << m6 << endl;
}
infile example
4 5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
6 7
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
4 5
20 19 18 17 16
15 14 13 12 11
10 9 8 7 6
5 4 3 2 1
outfile example
m3 is:
This is a 4 by 5 Matrix
20 19 18 17 16
15 14 13 12 11
10 9 8 7 6
5 4 3 2 1
m4 is:
This is a 4 by 5 Matrix
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
m4 is:
This is a 4 by 5 Matrix
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
m4 is:
This is a 6 by 7 Matrix
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35
36 37 38 39 40 41 42
m6 is:
This is a 4 by 5 Matrix
21 21 21 21 21
21 21 21 21 21
21 21 21 21 21
21 21 21 21 21
Solution
#include <iostream>
using namespace std;
class matrix
{
public:
int numRows , numCols;
int **mArray;
matrix(int row, int col)
{
numRows = row;
numCols = col;
mArray = new int*[numRows];
for (int i = 0; i < numRows; i++)
mArray[i] = new int[numCols];
}
matrix(const matrix &c)
{
numRows = c.numRows;
numCols = c.numCols;
mArray = c.mArray;
}
~matrix()
{
for (int i = 0; i < numRows; i++)
delete mArray[i];
delete mArray;
}
friend matrix operator + (matrix m2);
friend istream& operator>>(istream& os, matrix c);
friend ostream& operator<<(ostream& os, const matrix &c);
};
matrix operator+ (matrix m, matrix m2)
{
matrix T(m.numRows, m.numCols);
for(int i = 0; i < m.numRows; i++)
{
for(int j = 0; j < m.numCols; j++)
{
T.mArray[i][j] = m.mArray[i][j] + m2.mArray[i][j];
}
}
return T;
}
istream &operator >> (istream &in, matrix a){
for(int i=0; i<a.numRows; i++)
{
for(int j=0; j<a.numCols;j++)
{
in >> a.mArray[i][j];
}
}
return in;
}
ostream &operator <<(std::ostream& out, const matrix& a){
for(int i=0; i<a.numRows; i++)
{
for(int j=0; j<a.numCols;j++)
{
out << a.mArray[i][j]<<\" \\t\";
}
out <<endl;
}
return out;
}
int main()
{
matrix rf(3,3);
cout << \"Enter Elements: \";
cin >> rf;
cout << rf;
}



