```
#include <iostream>
#include <iomanip>
using namespace std;
class Matrix
{
public:
Matrix (int =0, int =0);
~Matrix ();
Matrix (const Matrix&);
void operator = (const Matrix&);
Matrix operator + (const Matrix&);
Matrix operator * (const Matrix&);
Matrix operator - (const Matrix&);
int* getMember(int =0, int =0) const; //returns pointer to matrix member
void setMatrix (); //set matrix members one at a time
void displayMatrix ();
void nulify (); // set matrix members to 0's
private:
int * pointer; //pointer to matrix member
int * reset; //pointer to the begunning of the matrix array [m*n] in memory
int m,n; //matrix dimensions
};
void nulify (Matrix&);
int main ()
{
cout <<"MATRIX ALGEBRA PROGRAM : "<<endl;
int choice=0;
do
{
cout <<"1) ADDITION"<<endl;
cout <<"2) SUBSTRACTION"<<endl;
cout <<"3) MULTIPLICATION"<<endl;
cout <<"4) EXIT "<<endl;
cout <<"your choice : ";
cin >> choice;
switch (choice)
{
case 1:
{
cout <<"enter matrix sizes: "<<endl;
cout <<"row number : ";
int m;
cin >> m;
cout <<"column number : ";
int n;
cin >> n;
Matrix A (m,n), B (m,n), C(m,n);
cout <<"enter matrix A"<<endl;
A.setMatrix();
cout <<"enter matrix B"<<endl;
B.setMatrix();
cout <<"matrix A = "<<endl;
A.displayMatrix();
cout <<"matrix B = "<<endl;
B.displayMatrix();
C=A+B;
cout <<"C = A + B "<<endl;
C.displayMatrix();
} break;
case 2:
{
cout <<"enter matrix sizes: "<<endl;
cout <<"row number : ";
int m;
cin >> m;
cout <<"column number : ";
int n;
cin >> n;
Matrix A (m,n), B (m,n), C(m,n);
cout <<"enter matrix A "<<endl;
A.setMatrix();
cout <<"enter matrix B "<<endl;
B.setMatrix();
cout <<"matrix A = "<<endl;
A.displayMatrix();
cout <<"matrix B = "<<endl;
B.displayMatrix();
C=A-B;
cout <<"C = A - B "<<endl;
C.displayMatrix();
} break;
case 3:
{
cout <<"enter matrix A sizes: "<<endl;
cout <<"row number : ";
int mA;
cin >> mA;
cout <<"column number : ";
int nA;
cin >> nA;
cout <<"enter matrix B sizes: "<<endl;
cout <<"row number : ";
int mB;
cin >> mB;
cout <<"column number : ";
int nB;
cin >> nB;
Matrix A (mA,nA), B (mB,nB), C(mA,nB);
cout <<"enter matrix A"<<endl;
A.setMatrix();
cout <<"enter matrix B" <<endl;
B.setMatrix();
cout <<"matrix A = "<<endl;
A.displayMatrix();
cout <<"matrix B = "<<endl;
B.displayMatrix();
C=A*B;
cout <<"C = A * B "<<endl;
C.displayMatrix();
} break;
default:
break;
}
} while (choice !=4);
return 0;
}
Matrix::Matrix (int x, int y) : m(x), n(y) //matrix will be constructed initialy to hold only 0s
{
if (m*n <=0)
{
cout <<"error : user constructor - matrix dimensions must be positive integers"<<endl;
exit (0);
} else
{
pointer = new int [m*n];
reset = pointer;
for (int i=0; i<m*n; i++)
*(pointer+i) =0;
}
pointer = reset;
}
Matrix::~Matrix()
{
delete [] pointer;
}
Matrix::Matrix(const Matrix& M)
{
pointer = new int [M.m*M.n];
m=M.m;
n=M.n;
for (int i=0; i<m*n; i++)
*(pointer+i)=*(M.pointer+i);
}
void Matrix::displayMatrix ()
{
int newLine=n-1;
for (int i=0; i<m*n; i++)
{
cout <<setw(4)<<*(pointer+i);
if (i == newLine)
{
cout <<endl;
newLine+=n;
}
}
}
int* Matrix::getMember (int i, int j) const
{
if (i*j <= 0)
{
cout <<"error : getMember - row and column index are positive integers"<<endl;
exit (0);
}
else
return (pointer + (i-1)*(n)+(j-1));
}
void Matrix::setMatrix ()
{
int row=1;
int column=1;
int newLine = n-1;
for (int i=0; i<m*n; i++)
{
cout <<"matrix["<<row<<"]["<<column<<"]= ";
column++;
if (i==newLine)
{
column=1;
++row;
newLine+=n;
}
cin >>*(pointer +i);
}
}
void Matrix::operator = (const Matrix& right)
{
if (m==right.m && n==right.n)
for (int i=0; i<m*n; i++)
*(pointer +i)=*(right.pointer+i);
else
{
cout <<" error : operator= - matrix dimensions must be equal. "<<endl;
}
}
Matrix Matrix::operator +(const Matrix& right)
{
Matrix temp (right.m, right.n);
for (int i=0; i<m*n; i++)
*(temp.pointer+i)=*(pointer+i)+*(right.pointer+i);
return temp;
}
Matrix Matrix::operator *(const Matrix& right)
{
if (n==right.m)
{
Matrix temp(m,right.n);
Matrix current = *this;
for (int i=0; i<temp.m; i++)
for (int j=0; j<temp.n; j++)
{
int sum =0;
for (int k=0; k<right.m; k++)
{
sum+=*current.getMember(i+1,k+1)* *right.getMember(k+1,j+1);
}
*temp.getMember(i+1,j+1)=sum;
}
return temp;
} else
{
cout <<"error : operator * - matrix sizes bad : A[m,k]*B[k,n]=C[m,n] , m,n,k"
<<"- number of rows and columns."<<endl;
exit (0);
}
}
Matrix Matrix::operator -(const Matrix& right)
{
Matrix temp (right.m, right.n);
for (int i=0; i<m*n; i++)
*(temp.pointer+i)=*(pointer+i)-*(right.pointer+i);
return temp;
}
void Matrix::nulify ()
{
for (int i=0; i<m*n; i++)
*(pointer +i)=0;
}
```

//tell me if this is any good (or bad :) - be gentle