#ifndef MATRIX_H#define MATRIX_H#define NULL 0#include <iostream>#include <sstream>#include <string>using std::cout; using std::cin;const char ERROR_MESSAGE[][80] = { "Assignment error. The dimensions do not match.\n", "Addition error. The dimensions do not match.\n", "Subtraction error. The dimensions do not match.\n", "Multiplication error. The dimensions do not match.\n"};enum ERROR_NUM {ASSIGNMENT, ADDITION, SUBTRACTION, MULTIPLICATION};float get_float();class Matrix{public: Matrix(int rows, int columns); // constructor Matrix(const Matrix &m); // copy constructor ~Matrix(); // destructor //overloaded operators Matrix& operator =(const Matrix &m); // assignment operator Matrix operator +(const Matrix &m); // increment operator Matrix operator -(const Matrix &m); Matrix operator *(const Matrix &m); float operator()(unsigned i, unsigned j); friend std::ostream& operator <<(std::ostream &os, const Matrix &m); friend std::istream& operator >>(std::istream &os, Matrix &m); void show() const; void set_to_0(); // set all elements to 0 void set_random(int range_start, int range_finish); //set random values void set_values(); int get_rows() const {return rows;} int get_columns() const {return columns;} static inline bool can_be_added(const Matrix &a, const Matrix &b) { return (a.rows == b.rows && a.columns == b.columns); } static inline bool can_be_subtracted(const Matrix &a, const Matrix &b) { return (a.rows == b.rows && a.columns == b.columns); } static inline bool can_be_multiplied(const Matrix &a, const Matrix &b) { return (a.columns == b.rows); }private: int rows, columns; float **ppMatrix;};#endif // MATRIX_H
#include "Matrix.h"Matrix::Matrix(int rows, int columns): rows(rows), columns(columns){ ppMatrix = new float*[rows]; for(int i = 0; i < rows; ++i) // Allocate memory for the matrix ppMatrix = new float[columns]; set_to_0();}Matrix::Matrix(const Matrix &m) // copy constructor{ // the copy constructor creates an independant object rows = m.rows; columns = m.columns; ppMatrix = new float*[rows]; for(int i = 0; i < rows; ++i) ppMatrix = new float[columns]; for(int i = 0; i < rows; ++i) // Set values for(int j = 0; j < columns; ++j) ppMatrix[j] = m.ppMatrix[j];}Matrix::~Matrix() // destructor{ // for every row, delete columns for(int i = 0; i < rows; ++i) delete []ppMatrix; delete ppMatrix;}Matrix& Matrix::operator =(const Matrix &m){ if(rows == m.rows && columns == m.columns) // if the matrices can be assigned { if(this != &m) // if we aren't assigning to itself { //the assignment operator is the destructor + the copy constructor //first, delete the currect matrix and free the memory for(int i = 0; i < rows; ++i) delete []ppMatrix; delete ppMatrix; //then, create the new matrix rows = m.rows; columns = m.columns; ppMatrix = new float*[rows]; for(int i = 0; i < rows; ++i) ppMatrix = new float[columns]; for(int i = 0; i < rows; ++i) // set values for(int j = 0; j < columns; ++j) ppMatrix[j] = m.ppMatrix[j]; } return *this; } else cout << ERROR_MESSAGE[ASSIGNMENT];}Matrix Matrix::operator +(const Matrix &m){ if(can_be_added(*this, m) ) { Matrix temp(rows, columns); for(int i = 0; i < rows; ++i) for(int j = 0; j < columns; ++j) temp.ppMatrix[j] = ppMatrix[j] + m.ppMatrix[j]; return temp; } else cout << ERROR_MESSAGE[ADDITION];}Matrix Matrix::operator -(const Matrix &m){ if(can_be_subtracted(*this, m) ) { Matrix temp(rows, columns); for(int i = 0; i < rows; ++i) for(int j = 0; j < columns; ++j) temp.ppMatrix[j] = ppMatrix[j] - m.ppMatrix[j]; return temp; } else cout << ERROR_MESSAGE[SUBTRACTION];}Matrix Matrix::operator *(const Matrix &m){ if(can_be_multiplied(*this, m) ) { Matrix temp(this->rows, m.columns); for(int i = 0; i < temp.rows; ++i) { for(int j = 0; j < temp.columns; ++j) { float s = 0.0f; for(int k = 0; k < this->columns; ++k) // this->columns = m.rows { s += ppMatrix[k] * m.ppMatrix[k][j]; } temp.ppMatrix[j] = s; } } return temp; } else cout << ERROR_MESSAGE[MULTIPLICATION];}void Matrix::show() const{ cout << *this;}std::ostream& operator <<(std::ostream &os, const Matrix &m){ for(int i = 0; i < m.rows; ++i) { for(int j = 0; j < m.columns; ++j) { os << m.ppMatrix[j] << " "; } os << "\n"; } os << "\n"; return os;}std::istream& operator >>(std::istream &os, Matrix &m){ for(int i = 0; i < m.rows; ++i) { for(int j = 0; j < m.columns; ++j) { cout << "Enter [" << i << "][" << j << "] of " << "[" << m.rows << "][" << m.columns << "]: "; m.ppMatrix[j] = get_float(); } } return os;}void Matrix::set_to_0(){ for(int i = 0; i < rows; ++i) for(int j = 0; j < columns; ++j) ppMatrix[j] = 0;}void Matrix::set_random(int range_start, int range_finish){ for(int i = 0; i < rows; ++i) for(int j = 0; j < columns; ++j) ppMatrix[j] = range_start + rand() % (range_finish - range_start);}void Matrix::set_values(){ cin >> *this;}float get_float(){ std::string temp; std::getline(std::cin, temp); std::stringstream ss(temp); float f; ss >> f; return f;}float Matrix::operator ()(unsigned i, unsigned j){ if(i < rows && j < columns) return ppMatrix[j];}
Is there a way to make operator() not return a garbage value? Should I use a pointer and return null?
Now I need to use that assert.