0

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?

6
  • 4
    Careful with those decimal commas. C++ uses . and your commas will compile, but do the wrong thing (most likely with a compiler warning). Commented Apr 10, 2015 at 13:56
  • ok, ok, that's has been written quickly, my fault, sorry. Any ideas for my question? Commented Apr 10, 2015 at 13:58
  • Perhaps this question helps. Commented Apr 10, 2015 at 14:00
  • You're looking for something, that is impossible to model using classic OOP approach. It's the same problem like square and rectangle. Square is a rectange and yet it wouldn't have much profit in deriving from rectangle (rectangle needs to hold a and b, while square is required to have all borders equally long). Perhaps circle and sphere deriving both from RoundObject would be much more correct, but still pointless to me. Commented Apr 10, 2015 at 14:07
  • 2
    An obvious problem with class sphere : public circle is 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. Commented Apr 10, 2015 at 14:08

2 Answers 2

5

You can have access to the base class' surface method by appending circle:: before its name :

sphere sph(1);
double s = sph.circle::surface();
Sign up to request clarification or add additional context in comments.

Comments

3

Your design is initially wrong. Public inheritance in C++ means that the child is-a specific kind of the parent. A sphere is not a circle!

Besides, if you do want to get the surface area of the sphere, you should make your surface function virtual:

class Circle {
    public:
    virtual double surface();
};

That way, when you override it in Sphere, the Sphere version will be called.

2 Comments

Wrong. He only needs to make surface() virtual if he needs the Sphere version to be called when using a pointer to Circle. If he uses a pointer to Sphere, or a regular non-pointer object, he's fine.
I agree; I assumed that since he was using inheritance that he would want this kind of behavior, and not that he wants to hide the base class implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.