C++ inheritance identification: what inheritance type does the following class use? class A : public X, public Y {}

Difficulty: Easy

Correct Answer: Multiple inheritance

Explanation:


Introduction / Context:
Recognizing the form of inheritance from the class header is a quick way to interpret C++ code. Here we see a derived class listing more than one base class separated by commas, which maps to a specific inheritance pattern known in object-oriented design and C++ syntax.


Given Data / Assumptions:

  • Code: class A : public X, public Y {};
  • Both bases X and Y are listed after a colon and separated by a comma.
  • No further details (virtual inheritance, etc.) are shown, but not needed.


Concept / Approach:

When a class inherits from more than one direct base at the same level, this is called multiple inheritance. Multilevel inheritance describes a chain (A derives from B, B derives from C). Hierarchical inheritance is one base with many derived classes. Hybrid inheritance mixes several patterns together (for example, multiple plus multilevel). The given declaration clearly shows two bases for A, so it is multiple inheritance.


Step-by-Step Solution:

Identify comma-separated bases: X and Y. Observe both are direct bases of A, not a chain. Match to the definition of multiple inheritance. Select “Multiple inheritance”.


Verification / Alternative check:

Replace the comma-separated list with a chain (class B : public A {}; class C : public B {};). That would be multilevel, unlike the given example. Therefore the classification here is unambiguously multiple.


Why Other Options Are Wrong:

Multilevel: requires a vertical chain, not siblings.

Hybrid: needs a mixture of models; not evident here.

Hierarchical: one base, many derived; opposite direction.


Common Pitfalls:

  • Confusing “two bases” (multiple) with “two levels” (multilevel).


Final Answer:

Multiple inheritance

Discussion & Comments

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