Interview Q&A · C++ · 8 questions in this topic
The Four OOP Pillars in C++ — C++ Interview Q&A
Encapsulation, abstraction, inheritance and polymorphism — what each one concretely means in C++ code. Part of Classes & OOP Pillars · 213 questions in the C++ bank.
What is encapsulation and how does C++ implement it?
Answer
Encapsulation bundles data with the operations on it and hides internals behind an interface. C++ implements it with classes plus access specifiers (private/protected/public), const-correct interfaces, and invariants enforced by constructors and mutators.
Good encapsulation means callers depend on behavior, not representation — the member layout can change without breaking users.
Example
class BankAccount { public: void deposit(double a) { if (a > 0) balance_ += a; } double balance() const { return balance_; } private: double balance_ = 0; // invariant: never negative via interface };
What is abstraction in C++?
Answer
Abstraction exposes what an object does while hiding how it does it. In C++ it is expressed through public interfaces, abstract base classes / pure virtual functions, and templates that program to concepts rather than concrete types.
A std::sort caller, for example, depends only on iterator requirements — not on the introsort implementation underneath.
Example
struct Shape { virtual double area() const = 0; virtual ~Shape() = default; }; struct Circle : Shape { explicit Circle(double r) : r_(r) {} double area() const override { return 3.14159 * r_ * r_; } private: double r_; };
What is inheritance and what are its access modes?
Answer
Inheritance derives a new class from a base, reusing and extending its interface. C++ has public (IS-A: base interface stays public), protected (public/protected base members become protected) and private (IS-implemented-in-terms-of: everything becomes private) inheritance.
Public inheritance models substitutability; private inheritance models reuse without substitutability (often better expressed as composition).
Example
class Animal { public: virtual void speak() const {} }; class Dog : public Animal { // IS-A: Dog is an Animal public: void speak() const override {} };
💡 Say IS-A for public, implemented-in-terms-of for private — that phrasing is exactly what interviewers listen for.
What is polymorphism? Compare compile-time vs runtime polymorphism.
Answer
Polymorphism means one interface, many implementations. Compile-time forms — overloading and templates — resolve at compile time with zero dispatch cost. Runtime polymorphism resolves virtual calls through the vtable at runtime, costing an indirection but allowing behavior to vary by dynamic type.
Modern C++ often prefers templates/concepts for performance and virtuals for open-ended runtime extension (plugins, heterogeneous collections).
Key points
- 1Static — overloads, templates: resolved by the compiler, fully inlineable.
- 2Dynamic — virtual functions: resolved via vptr/vtable at runtime.
- 3Choose static for speed and type lists; dynamic for runtime-varying behavior.
Example
template<class T> void twice(T& x) { x += x; } // static poly struct Base { virtual void f() const {} }; // dynamic poly
What is the difference between overloading, overriding and hiding?
Answer
Overloading is same name, different parameters in the same scope (compile time). Overriding redefines a virtual base function in a derived class with a compatible signature (runtime). Hiding is a non-virtual same-name declaration that shadows all base overloads of that name in the derived scope.
Hiding surprises: a derived f(int) hides every Base::f overload, even f(double) — fixed with `using Base::f;`.
Example
struct Base { virtual void f(int); void f(double); }; struct D : Base { void f(int) override; // overrides f(int), hides f(double)! // using Base::f; // uncomment to un-hide f(double) };
What is the difference between IS-A and HAS-A? Which is preferred?
Answer
IS-A is inheritance (a Dog is an Animal) with substitutability. HAS-A is composition (a Car has an Engine) with ownership of parts. Prefer composition by default — it is looser coupling, easier to change at runtime, and avoids inheritance's fragile-base-class problems.
Reach for public inheritance only when substitutable polymorphism is genuinely needed.
Example
struct Engine { void start(); }; struct Car { Engine engine_; }; // HAS-A: flexible, no polymorphism needed
💡 Prefer composition is the expected one-liner; justify it with coupling and runtime flexibility.
What is an abstract class and a pure virtual function?
Answer
An abstract class has at least one pure virtual function (virtual T f() = 0) and cannot be instantiated — it exists to be derived from. Derived classes become concrete by overriding every pure virtual. A pure virtual may still have a definition that derived overrides can explicitly call.
Destructors can be pure virtual too, but must be defined (the derived destructor chain always calls them).
Example
struct Drawable { virtual void draw() const = 0; // pure virtual virtual ~Drawable() = default; }; // Drawable d; // error: abstract
What is the difference between `virtual`, `override` and `final`?
Answer
virtual declares a function overridable with dynamic dispatch. override on a derived declaration makes the compiler verify it actually overrides a base virtual (signature must match) — always use it. final either seals a virtual against further overriding or seals a class against inheritance.
Missing override lets a typo silently create a new hidden function instead of an override; override turns that into a compile error.
Example
struct Base { virtual void f(int); }; struct D : Base { void f(int) override; // checked override // void f(long) override; // error: no matching base virtual }; struct Leaf final : D {}; // cannot be derived from
💡 Always write override — it converts the worst virtual-function typo bugs into compile errors.
Recommended resourcesAffiliate links · may earn us a commission