Reference no: EM13164475
Implement a class to represent a matrix. While doing this exercise, you are allowed only to use C++ primitive types and arrays. You may not use STL in your Matrix definition. You must use dynamic memory.
class Matrix {
friend ostream &operator << (ostream &os, const Matrix &);
friend istream &operator >> (istream &is, Matrix &);
public:
Matrix(); // 1x1 matrix with its element = 0
Matrix(int r, int c); // rxc matrix with all elements = 0
Matrix(int r, int c, int *values[]) // initialize matrix with specific values
Matrix(const Matrix &other); // copy ctor
~Matrix(); // destructor
Matrix &operator(const Matrix &other); // assignment operator
int getElement(int r, int c) const;
void setElement (int r, int c, int val);
Matrix operator + (const Matrix &other) const;
Matrix &operator += (const Matrix &other);
Matrix transpose() const;
int getNumRows() const;
int getNumCols() const;
bool isSymmetric() const;
private:
int **data;
int rows, cols;
};
You should throw and exception under illegal conditions (e.g., negative rows or cols, adding matrices with different sizes, etc.). For input and output matrices will be formatted as:
5 3
1 2 3
0 0 0
4 5 3
9 0 9
1 1 1