Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

This is improved code after I some issue in pointed by @Edward in the last question: C++ operator overloading for matrix operationsC++ operator overloading for matrix operations

This is improved code after I some issue in pointed by @Edward in the last question: C++ operator overloading for matrix operations

This is improved code after I some issue in pointed by @Edward in the last question: C++ operator overloading for matrix operations

deleted 243 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

C++ operator overloading for matrix operations (improved code)- follow-up

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  ?

C++ operator overloading for matrix operations (improved code)

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 ;

matrix.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.cpp

main.cpp

I have been told to "Throw exceptions rather than asserts" and to "Make your base class destructor virtual" what is the right way to do it  ? I never used exception before and not familiar with the concept of virtual desteructor.

"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 ?

Another thing I didn't understand is what to do when the main is trying to use the function illegally for example add 2 matrix that not in the same size  . I created new object and gave him the same data as one the called the function and returned it. "m=m+s" in this example if m and s are not in the same size I just returned new object with the values of m is it the right way  ?

C++ operator overloading for matrix operations - follow-up

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.

matrix.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.cpp

main.cpp

I have been told to "throw exceptions rather than asserts" and to "make your base class destructor virtual". What is the right way to do it? I never used exception before and not familiar with the concept of virtual destructor.

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 what to do when main is trying to use the function illegally for example add 2 matrix that not in the same size. I created a new object and gave him the same data as one then called the function and returned it. m=m+s in this example, if m and s are not in the same size I just returned new object with the values of m. Is it the right way?

edited title
Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

C++ operator overloading for matrix operations improved(improved code)

Source Link
benz
  • 405
  • 2
  • 6
  • 11
Loading