So i have an abstract class named Matrice
containing a matrix of complex numbers
class Matrice {
Complex** v; //matrix
public:
Matrice() {
v = new Complex * [//lin]; //use size here, without declaring it here
for (int i = 0; i < //lin; i++) {
v[i] = new Complex[//col];
for (int j = 0; j < //col; j++)
cin >> v[i][j];
}
}
virtual void afisare(int lin, int col) {
for (int i = 0; i < lin; i++) {
for (int j = 0; j < col; j++)
cout << v[i][j] << " ";
cout << endl;
}
}
};
and it's derived class
class Matrice_oarecare :Matrice {
int lin, col; //the size i want to use
public:
Matrice_oarecare();
~Matrice_oarecare() {
}
void afisare() { Matrice::afisare(lin, col); }
};
the question is how do i dynamically allocate my matrix using the size specified in my derived class Matrice_oarecare
, sorry for the dumb question but i'm new to this inheritance thing
If Matrice_oarecare inherits from Matrice, it has visibility on all protected attributes of Matrice class.
If you want to have access to Complex**v
from the derived class then
Complex**v
should protected in the base class.
and to allocate in Matrice_oarecare:
v = new Complex*[lin];
for(int i = 0; i < lin; ++i)
v[i] = new Complex[col];
Alternative
You can have a function called allocate
in the base class that allocates the Complex**
array like so :
class Matrice{
void allocate(int rows, int cols){
v = new Complex*[rows];
for(int i = 0; i < rows; ++i)
v[i] = new Complex[cols];
}
}
and call it from the constructor of the derived class. So you will have
class Matrice_oarecare :Matrice {
int lin, col;
public:
Matrice_oarecare(int rows, int cols):lin(rows),col(cols){
Matrice::allocate(lin, col);
}
~Matrice_oarecare() {
}
void afisare() { Matrice::afisare(lin, col); }
};
also you can have lin
and col
taken as parameters to the contructor of Matrice