i have following class hierarchy:
#include <iostream>
using namespace std;
class Figure
{
public:
Figure() {};
virtual void print() { cout << "Figure" << endl; };
};
class Circle : public Figure
{
public:
Circle() {};
virtual void print() { cout << "Circle" << endl; };
};
class Rectangle : public Figure
{
public:
Rectangle() {};
virtual void print() { cout << "Rectangle" << endl; };
};
class FigureContainer
{
public:
int index = 0;
Figure* figures = new Figure[20];
void add(const Figure& figure) { figures[index] = figure; index++; };
};
Main
int main()
{
FigureContainer* figures = new FigureContainer();
(*figures).add(*new Circle());
(*figures).add(*new Rectangle());
for (int i = 0; i < 2; i++)
{
(*figures).figures[i].print();
}
system("pause");
return 0;
}
and im expecting when interate over the figures when i call print function, to print the overrided one. What im doing wrong? Im pretty sure its somethink about the pointers and references, but not sure whats actaully.
Aucun commentaire:
Enregistrer un commentaire