Interview Questions
Inheritance · Study material
C++ Inheritance – Interview Questions
1. What is inheritance?
A mechanism for creating a new class from an existing one, reusing and extending its members; it models an IS-A relationship.
2. What are the types of inheritance in C++?
Single, multilevel, hierarchical, multiple and hybrid.
3. What are the inheritance modes and their effect?
public keeps public/protected as they are; protected turns both into protected; private turns both into private. Private base members are never accessible in the derived class.
4. What is the default inheritance mode?
private for class, public for struct.
5. In what order are constructors and destructors called?
Constructors: virtual bases, then direct bases in declaration order, then members in declaration order, then the constructor body. Destructors: the exact reverse.
6. How does a derived class call a specific base constructor?
In its member initializer list: Derived(int x) : Base(x) {}.
7. Are constructors inherited?
Not automatically; using Base::Base; inherits them (C++11).
8. What is name hiding?
A derived-class member with the same name as a base member hides all base members of that name, regardless of parameters. using Base::name; brings the base overloads back.
9. What is upcasting and downcasting?
Upcasting converts a derived pointer/reference to a base one – implicit and safe. Downcasting converts base to derived – explicit, with dynamic_cast (checked) or static_cast (unchecked).
10. What is object slicing?
Copying a derived object into a base object by value, which discards the derived part.
11. What is multiple inheritance and what problems can it cause?
A class with several direct bases. It can cause name ambiguity and, with a common base, the diamond problem.
12. What is the diamond problem?
When two classes derive from the same base and a fourth class derives from both, it contains two copies of the common base, causing duplication and ambiguity.
13. How does virtual inheritance solve it?
Classes that inherit virtually from the common base share a single base sub-object in the most derived class.
14. Who constructs a virtual base class?
The most derived class, before any non-virtual bases; constructor calls to the virtual base from intermediate classes are ignored.
15. Which members are not inherited?
Constructors (unless using-declared), destructors (but they are always called), friend relationships, and in effect the copy/move assignment operators, which are regenerated for the derived class.
16. Can a derived class access private members of its base?
No. They exist in the derived object but only the base's member functions and friends can access them.
17. What does final do?
On a class it prevents inheritance from it; on a virtual function it prevents further overriding.
18. When should you use inheritance versus composition?
Inheritance for genuine IS-A relationships where substitutability and polymorphism are needed; composition (HAS-A) for reuse otherwise.
19. What does the Liskov substitution principle say about inheritance?
A derived object must be usable wherever a base object is expected without breaking the program's correctness.
20. What is the size of a derived object?
At least the size of its base sub-objects plus its own members (plus padding, and pointers for virtual bases or virtual functions).