tags:

views:

317

answers:

5

Hi,

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?

A: 

1) You are correct. No type of inheritance allows access to private members (only friend declarations allow that)

2) They're "inherited" in the sense that an object of type Derived, when stored in memory, includes all of the data members of Derived and Base, including private members of Base. The private members can't just go away since when methods of Base run on that object, they will need to access Base's private members.

Also, the names of the private members of Base are technically in scope in Derived's methods, but of course you'll get a compile error if you try to access them.

Tyler McHenry
+2  A: 

1a) Protected inheritance means the "child" can access everything it could in public inheritance, but others using that object can only see the public interface to the child, anything in it's parent is hidden.

1b) Private inheritance results in all public functions of a class being inherited as private functions - meaning they can not be called from the child or accessed from a client of your object.

2) Private members are inherited because methods in the base class might need them to operate on.

EToreo
+2  A: 

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?

Yes.

The derived class can access the public and protected members in both cases. Is that right?

Yes.

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?

Because they are part of the base class, and you need the base class which is a part of your derived class. Note, you can still manipulate some state (if any) maintained in the base class using non-overriden public/protected member functions.

dirkgently
+1  A: 
  1. Yes, this is correct. Derived classes can access protected and public members of it's base class, and the derived class can not access the private members of it's base class.

  2. The private members are inherited for the following reason: The base class can define a protected or public function that modifies the base class's private member. The derived class can call this function, and therefore needs to know about the private variable that it's accessing.

Mike Sherov
+3  A: 
e.James
I like your last sentence.
skydoor