Private Inheritance question

Hi I very recently did the C++ online Quiz and was surprised to find that my answer (1) to the following question (below) was incorrect. Is there something I am missing here, or is the answer incorrect. I thought this was the point of Private. "Always inaccessible regardless or derivation access".
Apparently (3) in the correct answer! according to the quiz!

The private member in derived class
1) Cannot be inherited
2) Can be inherited at all instances
3) Can be inherited only if the derived class is inheriting from base class with private access level
4) None of the Above

Questions by BronzeBill

Showing Answers 1 - 16 of 16 Answers

I think you are correct, BronzeBill.  I just tried an experiment using MFC: ChildClass inheriting privately from ParentClass which has a private member - that private member is *not* accessible in a ChildClass method even though ChildClass : private BaseClass.

beharav

  • Sep 23rd, 2008
 

Your answer is right. Its 1. There is no way you can access a private member of a base class in the derived class by inheriting privately.

  Was this answer useful?  Yes

gpuchtel

  • Sep 26th, 2008
 

You are confusing inheritance with accessability. Private members are indeed inherited. That is, there is a slot for it in the ‘vbtl’ when the class is constructed; however, you don’t have access to it from a derived class, unless the base class has made you a ‘friend’. If ‘private’ members were not inherited, how would ‘friend(ship)’ work? So, inheritance means ‘what do I (physically) get’, not ‘what can I (logically) see’.

gpuchtel's answer makes sense to me. The fact that size of the derived class object is sum of size of all the public and private members of base class and derived class corroborates the argument.

  Was this answer useful?  Yes

j_l_larson

  • Oct 18th, 2009
 

You can if the base class provides public or protected methods that modify its private data.  Then derived class can expose this using access specifiers.



  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions