Interview Q&A · C++ · 9 questions in this topic
Inheritance In Depth — C++ Interview Q&A
Inheritance mechanics — access modes, construction order, multiple inheritance, diamond problem and virtual base classes. Part of Inheritance & Polymorphism · 213 questions in the C++ bank.
In what order are base classes and members constructed and destroyed?
Answer
Construction runs bases first (in base-specifier order), then members (in declaration order), then the derived constructor body. Destruction is the exact mirror: body, members reversed, bases reversed.
Virtual bases are constructed before all non-virtual bases by the most-derived class, which is how the diamond shares one instance.
Example
struct A { A(); }; struct B : A { std::string m; B() : A(), m("x") {} // A first, then m, then body };
💡 Declaration order — not initializer-list order — governs members; base-specifier order governs bases.
What is multiple inheritance and when is it reasonable?
Answer
Multiple inheritance derives from more than one base. It is reasonable for mixin-style capabilities (e.g. a class that is both Serializable and Printable) but risky for stateful bases with overlapping members — ambiguity must then be qualified explicitly.
Prefer single inheritance plus interfaces/mixins, and use virtual inheritance only to solve a genuine diamond.
Example
struct Printable { virtual void print() const = 0; }; struct Serializable { virtual std::string save() const = 0; }; struct Doc : Printable, Serializable { void print() const override {} std::string save() const override { return {}; } };
💡 Say mixins yes, stateful multi-parent hierarchies no — that is the balanced interview answer.
What is the diamond problem and how does virtual inheritance fix it?
Answer
The diamond (D inherits B and C, both inherit A) gives D two A subobjects without virtual inheritance — ambiguous access and duplicated state. Declaring the B/C-to-A inheritance virtual makes all paths share a single A subobject, constructed by the most-derived class.
The cost is extra indirection and construction complexity, so virtual bases should stay small and stateless where possible.
Example
struct A { int x = 0; }; struct B : virtual A {}; struct C : virtual A {}; struct D : B, C {}; // one shared A::x, not two D d; d.x = 1; // unambiguous
Can constructors, destructors, static functions and friends be virtual?
Answer
Constructors cannot be virtual (the dynamic type is still being built, so no vtable dispatch exists yet — virtual dispatch during construction resolves to the class under construction). Destructors can and often should be virtual. Static member functions have no object and cannot be virtual. Friends are not members at all, so virtual does not apply.
The constructor limitation is why factory functions and virtual clone() exist.
Example
struct Base { virtual ~Base() = default; virtual Base* clone() const = 0; // virtual factory instead of virtual ctor };
💡 Constructor no (no vtable yet), destructor yes, static no, friend N/A — recite all four for full marks.
What happens when you call a virtual function from a constructor or destructor?
Answer
The call resolves to the class currently under construction/destruction, not the most-derived override — the vtable still points at the in-progress class because derived parts do not exist yet (construction) or no longer exist (destruction).
Calling down to a not-yet-constructed derived override would touch uninitialized state, so the language deliberately binds statically to the current class. Never depend on polymorphic behavior in ctors/dtors.
Example
struct Base { Base() { init(); } // calls Base::init, NOT Derived::init virtual void init() {} };
What is function hiding in derived classes and how do you fix it?
Answer
Any derived declaration of a name hides all base declarations of that name — overloads included. So Derived::f(int) hides Base::f(double) even though the signatures differ, and calls like d.f(1.5) fail or convert unexpectedly.
Fix: add `using Base::f;` in the derived class to bring the hidden overloads back, or qualify explicitly (d.Base::f(1.5)).
Example
struct Base { void f(int); void f(double); }; struct D : Base { using Base::f; // un-hide both overloads void f(int); // override-style, others still visible };
💡 Name the fix (`using Base::f;`) — interviewers ask this question to hear exactly that line.
What is the difference between private and protected inheritance?
Answer
Protected inheritance makes public/protected base members protected in the derived class (usable by further-derived classes, invisible to the public). Private inheritance makes everything private — pure implementation reuse with no substitutability.
Public models IS-A; protected is rare (restricted reuse down the hierarchy); private models implemented-in-terms-of and is usually better written as composition unless you need empty-base optimization or override access.
Example
class Impl {}; class Widget : private Impl { // reuse, not substitutable }; // Widget* is NOT an Impl* — no conversion allowed outside
What is an interface in C++ (there is no `interface` keyword)?
Answer
The convention is an abstract class with only pure virtual functions (plus a virtual destructor) — no data members. Callers depend on the interface pointer/reference and concrete classes implement it.
Modern C++20 complements this with concepts, which constrain templates at compile time without any runtime indirection.
Example
struct Logger { virtual void write(std::string_view msg) = 0; virtual ~Logger() = default; };
What is covariant return type?
Answer
An override may return a pointer/reference to a derived type where the base returns a pointer/reference to the base. This lets clone-style factories preserve precise static types down the hierarchy without casts.
It only works for pointers/references (never by value — that would slice) and the derived return must genuinely derive from the base return.
Example
struct Base { virtual Base* clone() const = 0; }; struct Derived : Base { Derived* clone() const override { return new Derived(*this); } };
Recommended resourcesAffiliate links · may earn us a commission