C++ inheritance syntax check: what happens when compiling the following class definitions? class Birds {}; class Peacock : protected Birds {};

Difficulty: Easy

Correct Answer: It will compile succesfully.

Explanation:


Introduction / Context:
C++ supports three access specifiers in inheritance: public, protected, and private. Understanding their legality and effects on member access and interface exposure is key. This item checks whether “protected inheritance” is valid and whether empty class definitions compile.



Given Data / Assumptions:

  • Base class Birds is defined with an empty body.
  • Derived class Peacock inherits using the protected access specifier.
  • No members or constructors are explicitly declared.



Concept / Approach:
Protected inheritance is legal C++ syntax. It changes how public and protected members of the base appear in the derived class’s interface (they become protected in the derived). Empty class bodies are also valid. Therefore, the code compiles successfully as-is.



Step-by-Step Solution:
1) Parse class Birds {}; → valid empty class.2) Parse class Peacock : protected Birds {}; → valid inheritance declaration.3) No rule requires bodies to contain members; empty is fine.4) Conclusion: compilation succeeds.



Verification / Alternative check:
Compile the snippet in any conforming compiler; it will succeed. Add members to observe how access transforms under protected inheritance.



Why Other Options Are Wrong:
A/B: Empty class bodies are fully legal.C: Protected inheritance is supported by the language.E: A virtual destructor is unrelated to the legality of inheritance syntax here.



Common Pitfalls:
Confusing inheritance access specifiers with member access specifiers. Inheritance access controls how base’s public/protected members are exposed by the derived, not their actual accessibility within member functions.



Final Answer:
It will compile succesfully.


Discussion & Comments

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