I have an abstract class say Figure
and some derived classes: Circle
, Square
, ...
The class figure implements:
private:
virtual double varea()=0;
double multiplier;
public:
virtual Figure * clone()=0;
double area() { return varea()*multiplier; }
The figures, Square for instance, behave like this:
private:
double L;
public:
virtual Figure * clone() {return new Square(*this);}
virtual double varea() {return L*L;}
I'm having difficulties to assign the variable multiplier when calling the method clone. What is the best way to achieve this? Of course this is just a stupid example with a number of workarounds, but in reality, with multiple levels of derivation, they are not so obvious, so please stick to this pattern.
Should I go for a virtual interface also for the method clone? In this way I can assign the multiplier directly in the Figure class without any need to let each figure know its multiplier.