0

I have a class

class A
{
    int *const e;
    const int row, column;
public:
    A(int r, int c); // create a empty matrix with r rows and c columns passed in method
}

int main(int argc, char* argv[])
{
    A test(2,2); 
    return 0;
}

Question is how can I write a constructor that create a matrix which I can use ?

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
mskuratowski
  • 4,014
  • 15
  • 58
  • 109
  • if you want any prebuilt matrix functionality look into libraries like Eigen – chris Dec 04 '14 at 01:48
  • But in this project I dont want to use any library – mskuratowski Dec 04 '14 at 01:49
  • `memset` is your friend, assuming you really just want an "empty" (which, BTW, has no real meaning in software development) matrix. Also, since the word `empty` appears in your question title, but not in the body, I have no idea what you're really asking for. – 3Dave Dec 04 '14 at 02:08
  • Also, stop using parameter names like `r` and `c`. I would either a) not hire you fresh out of school, or b) fire your ass after a few successive code reviews revealing that variable naming was not your forte'. – 3Dave Dec 04 '14 at 02:12

4 Answers4

1

Your constructor would just be

A::A(int r, int c) : row{r}, column{c}
{
    e = new int[r * c];
}

Then your destructor should be

A::~A()
{
    delete[] e;
}

And you can access the elements as

for (int i = 0; i < row; ++i)
{
    for (int j = 0; j < column; ++j)
    {
        std::cout << e[i * row + j] << " ";  // Using array arithmetic
    }
    std::cout << std::endl;
}
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
  • Error 1 error C2758: 'A::e' : a member of reference type must be initialized So should I first initialized variable first ? – mskuratowski Dec 04 '14 at 02:49
0

Your question sound like: How to save parameters into the constant row/column properties.

A::A(int r, int c): row(r), column(c), e(new int[r*c])
{
    for (int i=0; i<r*c; i++) e[i]=0;
}

Also do not forgot the destructor:

virtual ~A(){ delete[] e; };

In the parameter-list of the constructor, parameters are initialized in declaration order, so you cannot use row/column (they are not already initialized).

Adrian Maire
  • 14,354
  • 9
  • 45
  • 85
0

You need a pointer to a pointer to create a [normal] 2-dimensional array so

int ** const e;

Then the constructor can work like:

e = (int**) malloc(sizeof(int**) * a);
for(int i=0;i<a;++i) {
  e[i] = (int*) malloc(sizeof(int*) * b);
}

Your next problem is "how to initialize the constants". For this, you can refer to the answer here: how to initialize const member variable in a class C++

to implement this, put the inittializer code in a function:

initConstArray(const int** const a, int r, int  c) {
    e = (int**) malloc(sizeof(int**) * a);
    for(int i=0;i<r;++i) {
      e[i] = (int*) malloc(sizeof(int*) * b);
      for(int j = 0;j < c; ++j) {
        e[i][j] = a[i][j];
    }

and call this function from the constructor initializer list:

A(int **a, int r, int c) : initConstArray(a, r, c) {
}
Community
  • 1
  • 1
gk_2000
  • 1
  • 3
0

Here is another way to implement the matrix with templates:

#include <array>
#include <iostream>

template <std::size_t ROW, std::size_t COLUMN>
class A
{
    std::array<int, ROW*COLUMN> a;
public:
    A():a(){};
};


int main(int argc, char* argv[])
{
    A<3,3> test; 
    //test.printSize();
    //std::cout << test;
    return 0;
}

Much shorter and cleaner.

To make commented lines to work, you must add to the class the following 2 functions:

void printSize(){ std::cout << ROW << "x" << COLUMN << std::endl; };

friend std::ostream& operator<<( std::ostream& os, A<R,C> mat){
    int col_count = 0;
    for (auto id=mat.a.begin(); id!=mat.a.end(); id++){
        os << *id << (++col_count%C==0?"\n":" ");
    }

    return os;
};
Adrian Maire
  • 14,354
  • 9
  • 45
  • 85