Let 𝐴 be the base class in C++ and 𝐵 be the derived class from 𝐴 with…

2019

Let 𝐴 be the base class in C++ and 𝐵 be the derived class from 𝐴 with protected inheritance. Which of the following statement is false for class 𝐵?

  1. A.

    Member function of class 𝐵 can access protected data of class 𝐴

  2. B.

    Member function of class 𝐵 can access public data of class 𝐴

  3. C.

    Member function of class 𝐵 cannot access private data of class 𝐴

  4. D.

    Object of derived class 𝐵 can access public base class data

Attempted by 277 students.

Show answer & explanation

Correct answer: D

Answer: Object of derived class B can access public base class data is false.

  • Protected inheritance rule: public and protected members of the base class become protected members in the derived class.

  • Effect on member functions: member functions of the derived class can access the base class's public and protected members (because they are protected in the derived class).

  • Private members: private members of the base class remain inaccessible to the derived class.

  • Effect on objects of the derived class: because the base class public members become protected in the derived class, they cannot be accessed through a derived class object from outside. To allow object-level access to base public members, the inheritance must be public.

  • Example: class A { public: int x; }; class B : protected A { }; B b; b.x; // error: x is protected in B

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Mppsc Assistant Professor