S
S
seroukhovigor2014-05-02 14:31:33
C++ / C#
seroukhovigor, 2014-05-02 14:31:33

Implement class in C++, matrices

There are matrix programs (each separate) in C++, such as - addition / subtraction, multiplication, transposition, inverse matrix. We need to get them into the classroom.
With OOP, just started. But the finished class must be handed over in 3 days. Help to implement the class, at least a few lines on the design of the code (methods and their arguments (I can write the implementation of the method myself), etc.)
Please help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Vershinin, 2014-05-02
@seroukhovigor

#include <stdexcept>

using namespace std;

class Matrix
{
protected:
  double *matrix;
  unsigned size_x, size_y;
  
public:
  Matrix()
  {
    matrix = NULL;
    size_x = size_y = 0;
  }
  
  Matrix(unsigned sz_x, unsigned sz_y)
  {
    matrix = new double[sz_x * sz_y];
                            size_x = sz_x; size_y = sz_y;
  }
  
  /** Во всех операциях, где участвуют две матрицы нужно проверять их совместимость по размеру **/
  
  Matrix& operator+(Matrix &b)
  {
    
  }
  
  Matrix& operator-(Matrix &b)
  {
    
  }
  
  Matrix& operator*(Matrix &b)
  {
    
  }
  
  double* operator[](unsigned index)
  {
    if(index > sz_y)
      throw out_of_range("Index is out of range");
    
    return &matrix[index * sz_x];
  }
  
  Matrix& transpose()
  {
  
  }
  
  Matrix& reverse()
  {
  
  }
};

Regarding access to elements - the operator [] returns a row of the matrix, i.e. when accessing a matrix element - my_matrix[<row_number>][<column_number>], the first brackets return a piece of the array that starts with the required row (here the index is controlled), and the second - the element itself (here the index is not controlled). If you need full control - then a double array and a function double get(unsigned i, unsigned j) {}. If this is not so important, then you can do it as indicated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question