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?

rems4e
  • 3,112
  • 1
  • 17
  • 24
bingo157
  • 113
  • 1
  • 2
  • 12
  • 4
    Careful with those decimal commas. C++ uses `.` and your commas will compile, but do the wrong thing (most likely with a compiler warning). – chris Apr 10 '15 at 13:56
  • ok, ok, that's has been written quickly, my fault, sorry. Any ideas for my question? – bingo157 Apr 10 '15 at 13:58
  • Perhaps [this question](http://stackoverflow.com/questions/411103/function-with-same-name-but-different-signature-in-derived-class) helps. – chris Apr 10 '15 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. – Mateusz Grzejek Apr 10 '15 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. – R. Martinho Fernandes Apr 10 '15 at 14:08
  • @MateuszGrzejek this was only an example. I was looking for the thing which rems4e gave me. Like I said I'm new in c++ and I wasn't looking for answer on exactly this code. – bingo157 Apr 10 '15 at 14:15

2 Answers2

4

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

sphere sph(1);
double s = sph.circle::surface();
rems4e
  • 3,112
  • 1
  • 17
  • 24
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.

  • 1
    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. – Not a real meerkat Apr 10 '15 at 14:12
  • 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. –  Apr 10 '15 at 14:15