Hello All,
I've been learning C++ recently and my question pertains to inheritance. What I have is code like this:
class shape {
public:
virtual void print_type()=0;
double delta() //acts the same on all shapes
};
class sphere : public shape {
public:
void print_type() {
cout << "this is a sphere" << endl;
}
};
class cylinder : public shape {
public:
void print_type() {
cout << "this is a cylinder" << endl;
}
};
I'd then like to take the shape as a datamember (or adding flowers to the shape as I use here :
class Shape_with_flowers {
protected:
shape *the_shape;
public:
// member functions
};
However, the shape with flowers will mean that the original member function delta() will act differently upon an instance of shape with flowers than on a pure shape on its own. I've thought that I can make Shape_with_flowers a derived class of shape and make delta virtual. However this seems inelegant to me, as the derived class uses the parent class as the data member, is there a better solution? A lot of the member functions in shape would apply fine to Shape_with_flowers, but some will be different.
Thank you.