Object-Oriented Programming in C++
Most languages hide their object model. C++ does not. Here you decide whether to use virtual functions or templates, whether to manage memory manually or through smart pointers, whether to build class hierarchies or prefer composition. That freedom is powerful — and it comes with real traps: object slicing, missing virtual destructors, the diamond problem. These are not theoretical concerns; they are bugs that compile silently and fail at runtime.
C++ supports the four classical OOP principles — encapsulation, inheritance, polymorphism, and abstraction — but unlike Java or Python, the implementation mechanism directly affects object size, performance, and ABI stability.
Topic map
- Classes and access control — class syntax,
public/protected/private,friend,this,static. - Type system — what an object is, size, aggregate vs class.
- Encapsulation — protecting the invariant, not just "private fields".
- Inheritance — type hierarchy, layout, EBO.
- Polymorphism — dynamic dispatch and object slicing.
- Virtual functions — vtable,
override,final, covariant returns. - Pure virtual functions — abstract classes and NVI.
- Multiple inheritance — diamond, virtual base, vptr count.
- Constructors — initializer list, delegating,
explicit. - Field initialization — declaration order, default member init, aggregates.
- Copy semantics — deep vs shallow, copy-and-swap.
- Special members — Rule of Five and Rule of Zero,
= default/= delete. - Object lifetime — ctor/dtor order, vptr stages, virtual destructor.
Common traps
| Mistake | Consequence |
|---|---|
| No virtual destructor in base class | UB on delete base_ptr — derived destructor never runs |
Object slicing (Animal a = Dog(...)) | Dog-specific data is gone, vptr reset to base |
Missing override | Wrong signature silently hides the base method instead of overriding it |
| Virtual call in constructor/destructor | Calls the base-class version, not the most-derived override |
Diamond inheritance without virtual | Two base subobjects, ambiguous member access |
| Defining only one of the Rule of Five | Compiler-generated copy/move is wrong — double free or shallow copy |
| Shallow copy of owning raw pointers | Double free, dangling pointers |
| Init-list order ≠ field declaration order | Declaration order wins silently; cross-field dependencies break |
One-argument constructor without explicit | Implicit conversions creeping into surprising places |
| Polymorphic delete through a non-virtual base destructor | Derived destructor never runs → resource leak |
Interview relevance
OOP in C++ is one of the most reliable signals of depth at the middle and senior level. Interviewers use it because it is easy to tell the difference between someone who knows the vocabulary and someone who understands the mechanics.
What the interviewer is actually checking:
- Do you know how vtable and vptr work, not just "virtual functions call the right method"?
- Do you understand object slicing, and why polymorphism only works through pointers or references?
- Do you know the cost of virtual dispatch — indirect call, no inlining, potential cache miss?
- Can you explain Rule of Five: why five, and what breaks if you only define the destructor?
- Can you walk through the diamond problem and explain what virtual inheritance actually changes?
- Do you know that virtual calls in constructors and destructors do not dispatch to the most-derived override?
- The field initialization order rule and why list-order ≠ declaration-order is UB.
explicitand implicit conversions.
Popular question directions:
- How does vtable work? How many vptrs does an object with multiple inheritance have?
- What is object slicing and how do you prevent it?
- Why must the base class destructor be virtual when using polymorphic delete?
- What does
overridedo? How is it different from just writingvirtual? - Explain the diamond problem. What does virtual inheritance actually solve, and what is its cost?
- Rule of Zero vs Rule of Five — when do you apply each?
- What order are fields and base classes initialized in?
Common wrong answer: "OOP means classes, inheritance, and polymorphism" — with no mention of dispatch mechanics, object layout, or ownership. That is the textbook answer, not the engineer's answer.