All Exams Test series for 1 year @ ₹349 only
Question

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

The correct answer is

Object of derived class B can access public base class data

Understanding C++ Protected Inheritance

In C++, inheritance allows a new class (derived class) to inherit properties and behaviors from an existing class (base class). The type of inheritance (public, protected, or private) determines the accessibility of the base class members within the derived class and for objects of the derived class.

This question specifically asks about protected inheritance. When a derived class inherits from a base class using the protected keyword, the accessibility of the base class members changes as follows in the derived class:

  • public members of the base class become protected members in the derived class.
  • protected members of the base class remain protected members in the derived class.
  • private members of the base class remain private and are not accessible to the derived class at all.

Let's analyze the given statements for class B, derived from base class A with protected inheritance:

Analyzing Accessibility in C++ Protected Inheritance

We will examine each statement to determine its truthfulness based on the rules of protected inheritance:

  1. Member function of class B can access protected data of class A

    In protected inheritance, protected members of the base class A remain protected in the derived class B. Member functions of a class can access its own protected members and its base class's protected members (depending on inheritance type, here protected). Since A's protected data becomes B's protected data, member functions of B can access it. This statement is True.

  2. Member function of class B can access public data of class A

    In protected inheritance, public members of the base class A become protected members in the derived class B. Member functions of a class can access its own protected members. Since A's public data becomes B's protected data, member functions of B can access it. This statement is True.

  3. Member function of class B cannot access private data of class A

    Private members of a class are only accessible within that class itself. They are not accessible to derived classes, regardless of the type of inheritance (public, protected, or private). Therefore, member functions of derived class B cannot access private data of base class A. This statement is True.

  4. Object of derived class B can access public base class data

    In protected inheritance, public members of the base class A become protected members in the derived class B. Protected members of a class are accessible within the class itself and by its derived classes, but they are not accessible from outside the class using an object of that class. Since A's public data became B's protected data, an object of class B from outside the class definition cannot directly access this data. This statement is False.

The statement that is false for class B with protected inheritance is that an object of derived class B can access public base class data.

C++ Inheritance Accessibility Summary
Base Class Member Accessibility in Derived Class (Protected Inheritance) Accessibility via Derived Class Object (from outside)
public protected Not Accessible
protected protected Not Accessible
private private (Not inherited/accessible) Not Accessible

False Statement Identification

Based on our analysis of protected inheritance in C++, the statement that claims an object of derived class B can access public base class data is incorrect. Public members of the base class become protected in the derived class, making them inaccessible through an external object of the derived class.

Revision Table: C++ Inheritance

C++ Inheritance Type Impact on Member Accessibility
Base Class Member Public Inheritance Protected Inheritance Private Inheritance
public public protected private
protected protected protected private
private Not Accessible Not Accessible Not Accessible

Additional Information: C++ Access Specifiers

C++ provides three main access specifiers:

  • public: Members declared as public are accessible from anywhere, both within the class and outside the class, including by derived classes and objects of the class.
  • protected: Members declared as protected are accessible within the class itself and by its derived classes. They are not accessible from outside the class using an object of the class (unless through specific member functions).
  • private: Members declared as private are only accessible within the class itself. They are not accessible from outside the class, not even by derived classes.

Understanding how these specifiers interact with different types of inheritance is crucial for designing object-oriented programs in C++.

Was this answer helpful?

Important Questions from OOPs concept

  1. Which of the following statements are true regarding C++?

    (a) Overloading a function gives the capability to an existing operator to operate on other data types.

    (b) Inheritance in object-oriented programming provides support to reusability.

    (c) When object of a derived class is defined, first the constructor of derived class is executed then constructor of a base class is executed.

    (d) Overloading is a type of polymorphism.

    Choose the correct option from those given below:
  2. Match List I with List II

    List IList II
    (Programming Paradigm)(Characteristic)
    A.ImperativeI.Declarative, clausal representation, theorem proving
    B.Object‐orientedII.Side‐effect free, declarative, expression evaluation
    C.LogicIII. Imperative, abstract data type
    D.FunctionalIV.Command‐based, procedural

    Choose the correct answer from the options given below :

  3. A member function can always access the data in _______, (in C++).

  4. Which of the following is not correct for virtual function in C++?

  5. Which of the following is correct (in C++)?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App