Please check the code to see if there anything I can improve . Everyting is working as expected.
matrix.h
#ifndef Matrix_h
#define Matrix_h
using namespace std;
class Matrix
{
private:
int rows;
int cols;
int **Mat;
public:
Matrix (const int &rows,const int &cols);
Matrix(const Matrix &other);
~Matrix ();
int* & operator[](const int &index) const ;
void operator=(const Matrix &otherM )const;
Matrix operator -()const;
Matrix Matrix::operator -(const Matrix &t)const;
Matrix Matrix::operator +(const Matrix &t)const ;
Matrix Matrix::operator *(const Matrix &t)const;
Matrix Matrix::operator *(const int &num)const;
friend Matrix Matrix::operator *(const int & num,const Matrix &m)
{
return (m*num);
}
friend Matrix Matrix::operator +(const int &num,const Matrix &t)
{
return (num+t);
}
friend ostream & operator <<(ostream & os , const Matrix & m )
{
for(int i=0;i<m.rows;i++)
{
for(int j=0;j<m.cols;j++)
cout <<m.Mat[i][j]<<" " ;
cout <<endl ;
}
return os ;
}
};
#endif
matrix.cpp
#include <iostream>
#include<assert.h>
#include "Matrix.h"
using namespace std;
Matrix::Matrix(const int &rows,const int &cols )//constructor of class Matrix
{
this->rows=rows;
this->cols=cols;
Mat=new int* [cols];
assert(Mat);
for(int i =0;i<rows;i++)
{
Mat[i]=new int[cols];
assert(Mat[i]);
}
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
Mat[i][j]=0;
}
Matrix::Matrix(const Matrix &other) //copy constructor
{
this->cols=other.cols;
this->rows=other.rows;
this->Mat=new int* [other.rows];
assert(this->Mat);
for(int i =0;i<other.rows;i++)
{
this->Mat[i]=new int[other.cols];
assert(this->Mat[i]);
}
for(int i=0;i<other.rows;i++)
for(int j=0;j<other.cols;j++)
this->Mat[i][j]=other[i][j];
}
int* & Matrix::operator [](const int &index) const
{
return Mat [index];
}
void Matrix::operator=(const Matrix &otherM )const
{
if(this->Mat !=otherM.Mat && this->cols==otherM.cols && this->rows==otherM.rows)
{
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
this->Mat[i][j]=otherM.Mat[i][j];
}
}
Matrix Matrix::operator-()const
{
Matrix temp(this->rows,this->cols);
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
temp.Mat[i][j]=this->Mat[i][j]*-1;
return temp;
}
Matrix Matrix::operator +(const Matrix &t)const //add 2 matrix
{
Matrix temp(this->rows,this->cols);
if (this->rows!=t.rows ||this->cols!=t.cols)
{
for(int i=0;i<this->rows;i++)
for(int j=0;j<this->cols;j++)
temp.Mat[i][j]=this->Mat[i][j];
return temp;
}
else
{
for(int i=0;i<this->rows;i++)
for(int j=0;j<this->cols;j++)
temp.Mat[i][j]+=t.Mat[i][j]+this->Mat[i][j];
}
return temp;
}
Matrix Matrix::operator *(const Matrix &t)const//multiplay matrix on the right
{
if (this->cols!=t.rows)
{
Matrix temp(this->cols,this->rows);
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
temp.Mat[i][j]=this->Mat[i][j];
return temp;
}
else
{
Matrix temp(this->cols,t.rows);
for(int i=0;i<this->rows;i++)
for(int j=0;j<t.cols;j++)
for(int k =0;k<this->cols;k++)
temp[i][j]+=this->Mat[i][k]*t.Mat[i][j];
return temp;
}
}
Matrix Matrix::operator *(const int &num)const //multiplay with number
{
Matrix temp(this->rows,this->cols);
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
temp.Mat[i][j]=this->Mat[i][j]*num;
return temp;
}
Matrix Matrix::operator -(const Matrix &t)const //matrix subtraction
{
Matrix temp(this->rows,this->cols);
if (this->rows!=t.rows ||this->cols!=t.cols)
{
for(int i=0;i<rows;i++)
for(int j=0;j<cols;j++)
temp.Mat[i][j]=this->Mat[i][j];
return temp;
}
else
{
for(int i=0;i<this->rows;i++)
for(int j=0;j<this->cols;j++)
temp.Mat[i][j]+=this->Mat[i][j]-t.Mat[i][j];
}
return temp;
}
Matrix::~Matrix ()//destrucor
{
for(int i =0;i<this->rows;i++)
delete [] this->Mat[i];
delete [] this->Mat;
}
main
#include <iostream>
#include "Matrix.h"
using namespace std;
void main()
{
Matrix m(2, 2);
m[0][0] = 2;
m[1][1] = 2;
cout << m << endl;
m = m;
const Matrix s = -m;
cout << m << endl << s << endl;
m = s+2 * -m * m * 2 - s;
cout << m << endl << s << endl;
cout << s[1][1] << endl;
}
using namespace std
in your code at your previous question, yet you're still using it. Please elaborate both points. \$\endgroup\$