Archived

This topic is now archived and is closed to further replies.

a few simple questions about class inheritance

This topic is 5134 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I've been reading the book C++ primer the section on class inheritance, and I can't quite understand what purpose the virtual and protected keywords are, even after re-reading the whole thing I can't figure it out could someone give an explanation on what those do? What's the difference between protected and private? I know my question is kind of silly, but I'm having problems understanding why you would need inheritance too so there make that a question too , Thanks [edited by - NullPointer33 on November 27, 2003 3:56:39 AM]

Share this post


Link to post
Share on other sites
Its been a while, but if I remember correctly, the protected keyword means that the variable can be seen by the inheriting class, but not altered. Where as the private keyword means any inheriting class cannot even see the variable.

As for why do we need inheritence, becuase is adds more completely useless functionality, that normal people don't use and people who try and use it don't really understand.





[edited by - Dazed and Confused on November 27, 2003 5:11:23 AM]

Share this post


Link to post
Share on other sites
The protected keyword works in a class just as the private keyword outwards. The only difference is that an inheriting class will get the protected variables tucked into its private section, so that the subclass can use it and alter it at will. (If you do public inheritance)

As for the virtual keyword. If a class (A) is virtual it means a subclass (D) can have one or more superclasses (B and C) that inherit from that baseclass (A). Normally class D would have a problem trying to access variables and methods in the base class A since it "owns" several instances of it. With the keyword virtual you are guaranteed that every subclass that may, or may not, have multiple instances of same base class will in fact end up with only one.

Actually, the topics you ask about are very large. It is much easier to look up tutorials or articles on them if you couldn''t understand it from C++ Primer. Have you skipped some chapters or something? Last time I read the book it was very easy to understand. (Later I realised it wasn''t such a good book at teaching C++ since it teaches a lot of C... but it is still a great programming primer. So it should have no problems teaching you classes and inheritance. Actually, those parts are the strength of the book.)

Share this post


Link to post
Share on other sites