In case of private inheritance in C++, which of the following correctly…

2018

In case of private inheritance in C++, which of the following correctly represents the accessibility of the data variables from second derived class?

Answer: D. Private variables: No, Protected variables: No, Public variables: NoConcept: When a class inherits privately, every protected and public member of the base class becomes reachable only as a PRIVATE member when viewed through…

  1. A.

    Private variables: Yes, Protected variables: No, Public variables: Yes

  2. B.

    Private variables: Yes, Protected variables: No, Public variables: No

  3. C.

    Private variables: Yes, Protected variables: Yes, Public variables: Yes

  4. D.

    Private variables: No, Protected variables: No, Public variables: No

Attempted by 305 students.

Show answer & explanation

Correct answer: D

Concept: When a class inherits privately, every protected and public member of the base class becomes reachable only as a PRIVATE member when viewed through that immediate derived class -- the member itself is not re-declared, but its effective access level, as seen from outside the base class, becomes private. A private member -- whether originally declared private in the base class, or made private-only through a private-inheritance step -- is reachable only inside the class through which it is currently seen (and that class's friends); it is never reachable from a class that is further derived from that owner, no matter which access specifier is used for that later inheritance step.

Application: Trace a base class B that has a private data member p, a protected data member r, and a public data member u, through two levels of derivation. Step 1: class D1 : private B -- because this inheritance step is private, protected member r and public member u both become accessible only as private members when viewed through D1; private member p was never reachable outside B in the first place. Step 2: class D2 : (any access specifier) D1 -- D2 is the second derived class in the chain. Step 3: inside D2, look for r and u -- both are now private-only as seen through D1, so D2 cannot name them, no matter which access specifier D2 uses to inherit from D1. Step 4: inside D2, look for p -- it was already unreachable outside B, so it stays unreachable through every further level, including D2.

Cross-check: apply the general private-inheritance mapping (protected becomes private-only, public becomes private-only, private stays inaccessible) at the D1 step, then note that D2's own inheritance mode from D1 does not matter once r and u are already private-only as seen through D1 -- a private member can never be exposed further down a derivation chain. Both the step-by-step trace and this general mapping agree that none of the three categories survive into D2.

Result: from the second derived class, the private data is not reachable, the protected data is not reachable, and the public data is not reachable -- Private: No, Protected: No, Public: No.

Explore the full course: Uppsc Polytechnic Lecturer 2025 Cs

Loading lesson…