Answer the question
In order to leave comments, you need to log in
How to call an overridden function on a child object that is in the parent array?
There is a Moved class:
class Moved{
public:
virtual int getType();
virtual int getBS() ;
};
class Car : public Moved{
public:
int getBS() override;
int getType() override;
};
class Walker : public Moved{
public:
int getBS() override;
int getType() override;
};
int Moved::getBS() {
return -1;
}
int Moved::getType() {
return -1;
}
int Car::getBS() {
return 3;
}
int Walker::getBS() {
return 1;
}
int Car::getType() {
return 0;
}
int Walker::getType() {
return 1;
}
void Printer::addM(Moved obj) {
Moved* tmp = new Moved[mSize];
for (int i = 0; i < mSize; i++) tmp[i] = objs[i];
objs = new Moved[++mSize];
for (int i = 0; i < mSize - 1; i++)
objs[i] = tmp[i];
objs[mSize - 1] = obj;
Car mytmp; // для теста
cout << mytmp.getType() << endl; // для теста
cout << objs[mSize-1].getType() << endl;
Sleep(100);
}
Car M;
Car M1;
Car M2;
Car M3;
Car M4;
Walker W;
Walker W1;
Walker W2;
P.addM(M);
P.addM(M1);
P.addM(M2);
P.addM(W);
P.addM(W1);
P.addM(W2);
Answer the question
In order to leave comments, you need to log in
Reformatted the code for myself. I can't take Egyptian brackets.
void Printer::addM(Moved obj)
{
Moved* tmp = new Moved[mSize];
for (int i = 0; i < mSize; i++)
{
tmp[i] = objs[i];
}
objs = new Moved[++mSize];
for (int i = 0; i < mSize - 1; i++)
{
objs[i] = tmp[i];
}
objs[mSize - 1] = obj;
Car mytmp; // для теста
cout << mytmp.getType() << endl; // для теста
cout << objs[mSize-1].getType() << endl;
Sleep(100);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question