Difficulty: Medium
Correct Answer: Multiple inheritance is a feature that allows a derived class to inherit from more than one base class, combining interfaces and behaviour from all of them
Explanation:
Introduction / Context:
Multiple inheritance is a notable feature of C++ that allows a class to inherit from more than one base class. While powerful, it can also introduce complexity, such as the diamond problem, and so interviewers often ask candidates to define multiple inheritance and describe its basic purpose. This question focuses on the core definition rather than advanced corner cases.
Given Data / Assumptions:
Concept / Approach:
Multiple inheritance means that a single derived class can have more than one direct base class. For example, class Derived : public Base1, public Base2 { }; allows Derived to inherit data members and member functions from both Base1 and Base2. This can be used to combine capabilities from different base classes. The correct option must clearly state that multiple inheritance involves inheriting from more than one base class, not simply having multiple constructors or overloaded functions.
Step-by-Step Solution:
Step 1: Recall the basic syntax: class Derived : public Base1, public Base2 { /* members */ };
Step 2: Understand that Derived objects can be treated as objects of Base1 and Base2 where appropriate, and they may override virtual functions coming from either base.
Step 3: Recognise that the main goal is to combine interfaces and behaviours from multiple base classes into one derived class.
Step 4: Examine option (a), which explicitly states that multiple inheritance allows a derived class to inherit from more than one base class and combine interfaces and behaviour.
Step 5: Discard other options that refer instead to overloaded functions, multiple constructors, or definition across multiple source files, which are unrelated to inheritance structure.
Verification / Alternative check:
To verify, imagine designing a class that should act both as a graphical widget and as a serialisable object. You might have base classes Widget and Serializable and derive class Button : public Widget, public Serializable. This is a practical example of multiple inheritance, where Button inherits members from both base classes. The description in option (a) matches this scenario and confirms your answer.
Why Other Options Are Wrong:
Option (b) is wrong because having multiple constructors is constructor overloading, not multiple inheritance, and does not involve several base classes. Option (c) is incorrect because overloading functions is a different feature unrelated to inheritance. Option (d) is wrong because defining a class in multiple source files would cause redefinition errors and is not what multiple inheritance means.
Common Pitfalls:
A common pitfall when working with multiple inheritance is ignoring the possibility of the diamond problem, where a class inherits from two base classes that themselves share a common base. This can lead to duplicate base class subobjects unless virtual inheritance is used. Another mistake is to overuse multiple inheritance where simpler designs using composition or interfaces might be clearer. However, for the purpose of this question, it is enough to understand that multiple inheritance allows a class to inherit from more than one base class.
Final Answer:
Multiple inheritance is a feature that allows a derived class to inherit from more than one base class, combining interfaces and behaviour from all of them.
Discussion & Comments