103k views
0 votes
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?

(A) member function of class b can access protected data of class a
(B) member function of class b can access public data of class a
(C) member function of class b cannot access private data of class a
(D) object of derived class b can access public base class data

User Nart
by
7.2k points

1 Answer

6 votes

All the statements listed (A, B, C, D) are true for the described class hierarchy

How to explain

In the context of class inheritance where class B is derived from class A with protected inheritance, it's essential to understand the access levels of inherited members.

In this scenario, class B inherits both protected and public members of class A. The false statement is not provided among the options.

However, classes with protected inheritance allow derived classes to access protected and public members of the base class but restrict access to private members.

Therefore, all the statements listed (A, B, C, D) are true for the described class hierarchy.

The Complete Question

Consider the following scenario:

#include <iostream>

using namespace std;

class A {

protected:

int protectedDataA = 10;

private:

int privateDataA = 20;

public:

int publicDataA = 30;

};

class B : protected A {

public:

void accessData() {

cout << "Protected data of class A accessed by class B: " << protectedDataA << endl; // Statement 1

cout << "Public data of class A accessed by class B: " << publicDataA << endl; // Statement 2

// cout << privateDataA; // This line gives an error due to private access

}

};

Which of the following statements is false for class B?

A. Member function of class B can access protected data of class A. (True, as shown in Statement 1)

B. Member function of class B can access public data of class A. (True, as shown in Statement 2)

C. Member function of class B cannot access private data of class A. (True, as privateDataA in class A is private and inaccessible to derived classes)

D. Object of derived class B can access public base class data. (True, derived class objects can access public members of the base class.)

User Yuantonito
by
8.1k points