C++ inheritance syntax: which of the following is an invalid visibility label in a base-class list?

Difficulty: Easy

Correct Answer: friend

Explanation:


Introduction / Context:
When declaring inheritance in C++ (class Derived : access Base {}), you choose an access specifier that controls how the base’s public and protected members are treated in the derived class’s interface. Knowing which keywords are legal in the base-specifier list is fundamental C++ syntax knowledge.


Given Data / Assumptions:

  • We are selecting among: public, private, protected, friend.
  • The context is the base-specifier list after a colon in a class definition.
  • No virtual inheritance keyword is shown, but that is separate.


Concept / Approach:

Valid access specifiers in an inheritance list are exactly public, protected, and private. They adjust the accessibility of inherited members in the derived class. The keyword friend grants access privileges to another function or class and is not an access specifier for inheritance. Therefore it cannot appear as a visibility label in the base-specifier list.


Step-by-Step Solution:

Recall valid labels: public/protected/private. Note that friend is unrelated to inheritance access. Therefore, friend is invalid in this position. Choose “friend”.


Verification / Alternative check:

Try compiling class D : friend B {}; — compilers emit a syntax error. Replacing friend with public or protected compiles successfully (assuming B is declared).


Why Other Options Are Wrong:

public/private/protected are explicitly supported by the language for inheritance.


Common Pitfalls:

  • Confusing friend (access grant) with inheritance visibility.
  • Assuming friend affects transitive visibility; it does not.


Final Answer:

friend

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion