Skip to the page

Inheritance and polymorphism · core

Inheritance, virtual functions and the vtable

Public inheritance means "is a". A virtual function called through a base pointer or reference runs the version of the real object, chosen at runtime, usually through a table of function pointers.

The notes

What you must know

  • class Car : public Vehicle means a Car is a Vehicle: public members stay public, protected members stay protected, and the base's private members are never reachable from Car.
  • Without virtual, Base* p = new Derived(); p->show(); runs Base::show, chosen from the pointer type at compile time. With virtual void show() in Base, it runs Derived::show, chosen at runtime (runtime polymorphism).
  • Runtime dispatch works only through a pointer or a reference. Passing a Derived by value to a function that takes a Base copies only the Base part (object slicing), so the Base version runs.
  • Write override on the derived function: if it does not really override anything (a typo, a different parameter type), the compiler gives an error. final stops further overriding, or stops a class from being derived from.
  • How it usually works: each class with virtual functions has a vtable, a table of function pointers, and each object has a hidden pointer (vptr) to its class's table. The standard does not require this; compilers do it this way.
  • Cost: one extra pointer in each object, and a couple of extra memory reads on each virtual call.
  • A class used through base pointers needs a virtual destructor. Rule of thumb: if a class has any virtual function, make its destructor virtual.
  • Polymorphism removes if-else chains on type: screens call role->dashboard(), and Student, Teacher and Admin each override it. A new role is one new class, created in one place (a factory function).

The red pen

Common traps: mistake → fix

  • Mistake: Overriding works without virtual in C++

    Fix: without virtual, the pointer type decides which version runs.

  • Mistake: Virtual calls work on objects passed by value

    Fix: slicing keeps only the base part.

  • Mistake: Each object holds its own vtable

    Fix: each object holds a pointer; there is one table per class.

  • Mistake: override is needed for overriding

    Fix: it is optional, but it catches mistakes at compile time.

Easy to hard

Questions interviewers ask, easy to hard

  1. What is a virtual function? What happens without the virtual keyword?
  2. How do virtual functions work inside? What are the vtable and the vptr?
  3. What is object slicing?
  4. Why do we need a virtual destructor?
  5. Every screen in your project has an if-else on the user's role. How would virtual functions remove it?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview