I have the following class hierarchy:
#include <iostream>
using namespace std;
class Figure
{
public:
Figure() {};
virtual void print() const { cout << "Figure" << endl; };
Figure& operator=(const Figure& rhs)
{
if (this != &rhs)
{
cout << "Called figure equals" << endl;
}
return *this;
};
};
class Circle : public Figure
{
public:
Circle() {};
virtual void print() const { cout << "Circle" << endl; };
Circle& operator=(const Circle& rhs)
{
if (this != &rhs)
{
Figure::operator=(rhs);
cout << "Called circle equals" << endl;
}
return *this;
};
};
And when im adding derived objects to the FigureContainer, objects are not using its own operator=, but they're using the base class oprerator and enters in the array as base class instaces, so i cant call "print" function from the derived class.
class FigureContainer
{
public:
FigureContainer() { };
int size = 0;
Figure figures[20];
void add(const Figure& figure)
{
figure.print(); // Called circle print !
figures[size] = figure; // Used base = operator instead of the derived figure class
size++;
};
};
int main()
{
FigureContainer* figures = new FigureContainer();
(*figures).add(*new Circle());
(*figures).figures[0].print(); // called abstract print !
system("pause");
return 0;
}
.
Output :
Circle
Called figure equals
Figure
What am I doing wrong?