This work assignment in operator overloading .I need to use operators "*" "[][]"
"=" "+" "-" "<<"*, [][], =, +, -, << on objects of type matrix for example add to matrix using this code m=m+s ;: m=m+s.
matrix.hmatrix.h
#ifndef Matrix_h
#define Matrix_h
#include <iostream>
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 &other );
Matrix operator -()const;
Matrix operator -(const Matrix &other)const;
Matrix operator +(const Matrix &other)const ;
Matrix operator *(const Matrix &other)const;
Matrix operator *(const int &num)const;
int getMatrixRows(const Matrix &other){return other.rows;}
int getMatrixCols(const Matrix &other){return other.cols;}
friend Matrix operator *(const int & num,const Matrix &m)
{
return (m*num);
}
friend Matrix operator +(const int &num,const Matrix &t)
{
return (num+t);
}
friend std::ostream &operator<<(std::ostream &os, const Matrix &m) {
for (int i=0; i < m.rows; ++i) {
for (int j=0; j < m.cols; ++j) {
os << m.Mat[i][j] << " " ;
}
os << '\n';
}
return os;
}
};
#endif
matrix.cppmatrix.cpp
main.cppmain.cpp
I have been told to "Throw"throw exceptions rather than asserts" and to "Make"make your base class destructor virtual" what. What is the right way to do it ? I never used exception before and not familiar with the concept of virtual desteructordestructor.
"Prefer a single allocation
Instead of doing multiple allocations in the constructor, it would be simpler to do only a single allocation. This is both faster and simpler"
@Edward wrote this but is it possible to allocate 2 dimensional array with on allocation ?
Prefer a single allocation instead of doing multiple allocations in the constructor, it would be simpler to do only a single allocation. This is both faster and simpler
@Edward wrote this, but is it possible to allocate 2 dimensional array with an allocation?
Another thing I didn't understand is whatwhat to do when the mainmain is trying to use the function illegally for example add 2 matrix that not in the same size .
I I created a new object and gave him the same data as one thethen called the function and returned it. "m=m+s"m=m+s in this example, if mm and ss are not in the same size I just returned new object with the values of m ism. Is it the right way ?