Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Just curious...

Reply
Thread Tools

Re: Just curious...

 
 
ctgqumgf@sharklasers.com
Guest
Posts: n/a
 
      01-05-2013
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/Inherit...ed_programming)
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-05-2013
On 1/5/2013 4:57 AM, http://www.velocityreviews.com/forums/(E-Mail Removed) 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/Inherit...ed_programming)
>


V
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is it just me or just Microsoft? Ark Khasin C++ 24 07-16-2007 11:11 AM
How do I clean a virus within an inbox or just clean only that infectedattachment or LOCATE AND delete just that attachment ? Vinayak Firefox 1 08-14-2006 06:19 PM
Is there a Python MVC that works just as well with just CGI, or FCGI? walterbyrd Python 1 04-10-2006 07:57 PM
allow you to move just about any music to your iPud, MP3 player , or just burn it to disk sbcmynews Computer Support 4 05-01-2005 03:53 PM
Just bought a Digital Rebel just 1 question RacerX Digital Photography 7 11-21-2003 01:51 PM



Advertisments