C++ Inheritance Interview Questions
C++ Types of Inheritance
Basic Level (15 Questions)
What is inheritance in C++?
Inheritance is an OOP concept where a new class (derived class) is created from an existing class (base class). The derived class inherits properties and behaviors from the base class, promoting code reusability and establishing an "is-a" relationship.
What are the main types of inheritance in C++?
C++ supports five main types of inheritance: Single Inheritance, Multiple Inheritance, Multilevel Inheritance, Hierarchical Inheritance, and Hybrid Inheritance.
What is single inheritance?
Single inheritance is when a derived class inherits from only one base class. It's the simplest form of inheritance. Syntax: class Derived : access-specifier Base { };
What is multiple inheritance?
Multiple inheritance is when a derived class inherits from two or more base classes. Syntax: class Derived : access-specifier Base1, access-specifier Base2 { }; C++ supports multiple inheritance, but Java and C# do not.
What are the problems with multiple inheritance?
Problems with multiple inheritance include: Diamond Problem (ambiguous inheritance), Name ambiguity (same method in multiple base classes), Increased complexity, and Constructor calling order issues.
What is multilevel inheritance?
Multilevel inheritance is when a class is derived from another derived class, creating a chain of inheritance. Example: Class C inherits from Class B, which inherits from Class A. Syntax: class A {}; class B : public A {}; class C : public B {};
What is hierarchical inheritance?
Hierarchical inheritance is when multiple derived classes inherit from a single base class. It creates a tree-like structure. Example: Classes B, C, and D all inherit from Class A.
What is hybrid inheritance?
Hybrid inheritance is a combination of two or more types of inheritance. Most commonly, it's a combination of multiple inheritance and hierarchical inheritance, which often leads to the diamond problem.
What is the diamond problem in inheritance?
The diamond problem occurs in hybrid/multiple inheritance when a class inherits from two classes that both inherit from the same base class. This creates ambiguity about which base class members to inherit. Solved in C++ using virtual inheritance.
How is the diamond problem solved in C++?
The diamond problem is solved using virtual inheritance. By making the common base class a virtual base class, only one copy of the base class is inherited. Syntax: class Derived : virtual public Base { };
What are access specifiers in inheritance?
C++ has three access specifiers in inheritance: public (base class public members remain public), protected (base class public and protected become protected), and private (base class public and protected become private).
What is the difference between "is-a" and "has-a" relationship?
"Is-a" relationship is implemented using inheritance (Car is a Vehicle). "Has-a" relationship is implemented using composition/aggregation (Car has an Engine). Inheritance should be used only for true "is-a" relationships.
What is virtual base class in C++?
A virtual base class is used in inheritance hierarchies to prevent multiple copies of a base class when multiple inheritance paths exist to that base class. Declared with the virtual keyword in inheritance.
What is constructor calling order in inheritance?
In inheritance, constructors are called from base to derived (top to bottom). Destructors are called in reverse order: derived to base (bottom to top). For multiple inheritance, base class constructors are called in the order they appear in inheritance list.
Can a derived class access private members of base class?
No, a derived class cannot access private members of the base class directly. Private members are accessible only within the class they are declared. To access base class private members, use public/protected getter/setter methods.
Tricky Level (10 Questions)
What is the difference between inheritance and friendship?
Inheritance establishes an "is-a" relationship and allows access to protected and public members. Friendship grants access to private and protected members but doesn't establish any relationship. Friend functions/classes are not inherited.
What are abstract classes and inheritance?
An abstract class is a class with at least one pure virtual function (virtual void func() = 0;). It cannot be instantiated and serves as a base for derived classes. Derived classes must implement all pure virtual functions to become concrete classes.
What is inheritance visibility mode?
Inheritance visibility mode (public, protected, private) determines the accessibility of base class members in the derived class. It affects how base class members are "seen" from the derived class and objects of derived class.
What are the advantages of using inheritance?
Advantages: Code reusability (reuse existing code), Extensibility (extend base class functionality), Polymorphism (enable runtime polymorphism), Organization (create hierarchical class structures).
When should inheritance be avoided?
Inheritance should be avoided when: 1) Relationship is "has-a" not "is-a", 2) Base class is not designed for inheritance, 3) Implementation inheritance is needed (use composition instead), 4) Class hierarchy becomes too deep/complex, 5) Multiple inheritance causes diamond problem.
What is multiple inheritance diamond problem?
Two base subobjects of a common base in a derived class—solved by virtual inheritance sharing one base subobject.
Difference between private and protected inheritance?
Private inheritance makes base members private in derived; protected keeps them accessible in further derived classes.
What is using-declaration in derived class?
using Base::func; brings overloaded base functions into derived scope for overload resolution.
Can derived class access base private members?
No, unless friend or public/protected interface—private members are not inherited for access.
What is covariant return type?
Overridden virtual function may return a derived pointer/reference type when base returns base pointer/reference.
Note: These questions cover different types of inheritance in C++ including their syntax, usage, advantages, and potential issues. Understanding inheritance types is crucial for effective object-oriented design and avoiding common pitfalls like the diamond problem. This page lists 15 basic and 10 tricky questions—use the tutorial and MCQ links above and below.