> When you write class Cylinder : public Circle this only gives you access to
> the "public" section of the class and to use this with `height = volume / (PI * (radius * radius));'.
> The public member function of "Cylinder" has access to its private member
> variables, but not the private member of "Circle".
> "Cylinder" would need to call a public member function of "Circle" to get, or ste,
> the private member variables of "Circle".
> So the line of code height = volume / (PI * (radius * radius));
> should look something more like this height = volume / (PI * (getRadius() * getRadius()));.
class Cylinder: public Circle
Cylinder inherits all the members from Circle. It has access to the public members (like anyone else) and also to the protected members
it cannot access the private members, but those do form part of its state
the private members are responsibility of the base class, so the base class should manage them.
¿so how to compute the volume then? use the interface provided by the base class, no need to expose anything more
1 2 3 4 5 6 7
|
double Cylinder::volume() const{
//all these are equivalent
//double v = height * Circle::area();
//double v = height * this->area();
double v = height * area();
return v;
}
| |
one could argue that a cylinder is not a circle, it has two circles as base and top