I'm new in C++ and I have maybe easy for you question.
class circle {
protected:
int r;
public:
circle(int re) { r=re; }
double surface() {
return 3.14*r*r;
}
};
class sphere : public circle {
public:
sphere(int r) : circle(r) {}
double surface(){
return 4*3.14*r*r;
}
};
And now, my problem is how I can do something like that: create a sphere object and using it to get surface not of a sphere but of a circle. Can I use somehow the same methods names in two classes, when one is inherited by the second one?
.and your commas will compile, but do the wrong thing (most likely with a compiler warning).aandb, while square is required to have all borders equally long). Perhapscircleandspherederiving both fromRoundObjectwould be much more correct, but still pointless to me.class sphere : public circleis that a sphere is a generalisation of a circle into three dimensions, but derivation is actually specialisation. In an inheritance relationship the more general concept is represented by the base class, not by the derived class.