Re: Just curious...

Discussion in 'C++' started by ctgqumgf@sharklasers.com, Jan 5, 2013.

  1. Guest

    Am Samstag, 5. Januar 2013 09:33:56 UTC+1 schrieb Giuliano Bertoletti:
    > Hello, is it correct that the compiler does not issue an error on the following code? ===================== class Shape { public: void myfunction(); }; class Polygon : public Shape { public: // does not define myfunction }; class Hexagon : public Polygon{ public: void myfunction() { Polygon::myfunction(); // why does it compile? } }; ===================== I mean, what is the benefit of implicitly calling Shape::myfunction and not issuing an error when I'm explicitly asking to call a non existing Polygon::myfunction? Giulio.


    Because that's exactly what public inheritance is for! A method defines
    a certain functionality. Inheriting and NOT overriding a method means
    the drived class has (wants to have) the same behaviour as the base class.
    Some people call this phenomenon "reuse". A good starting point is
    http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)
     
    , Jan 5, 2013
    #1
    1. Advertising

  2. On 1/5/2013 4:57 AM, wrote:
    > Am Samstag, 5. Januar 2013 09:33:56 UTC+1 schrieb Giuliano Bertoletti:
    >> Hello, is it correct that the compiler does not issue an error on
    >> the

    following code? ===================== class Shape { public: void
    myfunction(); }; class Polygon : public Shape { public: // does not
    define myfunction }; class Hexagon : public Polygon { public: void
    myfunction() { Polygon::myfunction(); // why does it compile? } };
    ===================== I mean, what is the benefit of implicitly calling
    Shape::myfunction and not issuing an error when I'm explicitly asking to
    call a non existing Polygon::myfunction? Giulio.
    >
    > Because that's exactly what public inheritance is for! A method defines
    > a certain functionality. Inheriting and NOT overriding a method means


    Just a nitpick: you should only use the term 'overriding' when talking
    about virtual functions. In the OP's example the function is not
    virtual so no "overriding" can occur. You can *redefine* the function
    in the derived class, but it will not *override* unless the function is
    virtual and has the same type.

    > the drived class has (wants to have) the same behaviour as the base class.
    > Some people call this phenomenon "reuse". A good starting point is
    > http://en.wikipedia.org/wiki/Inheritance_(object-oriented_programming)
    >


    V
    --
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Jan 5, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    401
    Robert Klemme
    Jan 6, 2006
  2. Elaine Jackson

    just curious

    Elaine Jackson, Sep 6, 2003, in forum: Python
    Replies:
    3
    Views:
    386
    News M Claveau /Hamster-P
    Sep 6, 2003
  3. Ken Parkes

    Just curious

    Ken Parkes, Sep 20, 2004, in forum: Python
    Replies:
    4
    Views:
    339
    Ken Parkes
    Sep 22, 2004
  4. lallous
    Replies:
    5
    Views:
    366
  5. =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=

    Gridview question - Just curious

    =?Utf-8?B?Qi4gQ2hlcm5pY2s=?=, Nov 12, 2007, in forum: ASP .Net
    Replies:
    1
    Views:
    321
    Eliyahu Goldin
    Nov 12, 2007
Loading...

Share This Page