I am really confused about private inheritance and protected inheritance.
1) in protected inheritance, the public and protected members become protected members in the derived class. In the private inheritance, everything is private. However, the derived class can never access the private members of the base class, is that right? The derived class can access the public and protected members in both cases. Is that right?
2) I noticed that the private members of the base class will never be touched by the derived class. So why are the private members inherited?
Best Answer
You are correct on point #1. Specifying
private
,protected
orpublic
when inheriting from a base class does not change anything access-wise on the derived class itself. Those access specifiers tell the compiler how to treat the base-class members when instances of the derived class are used elsewhere, or if the derived class happens to be used as a base class for other classes.UPDATE: The following may help to illustrate the differences:
For classes derived from base:
For classes derived from the 3 derived classes:
External access to the first three derived classes: