0Pricing
C++ Academy · Lesson

Virtual Functions and the vtable

Implement runtime polymorphism with virtual functions and inspect the vtable concept.

Virtual Functions and the vtable is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Static vs Dynamic Binding

By default, member calls are statically bound — the compiler picks the function based on the declared type. With virtual functions, the call is dynamically bound based on the actual type.

Declaring a virtual Function

Mark the function virtual in the base class. Derived classes can override it.

class Shape {
public:
    virtual double area() const = 0;        // pure virtual
    virtual ~Shape() = default;
};

class Circle : public Shape {
    double r_;
public:
    Circle(double r) : r_(r) {}
    double area() const override { return 3.14159 * r_ * r_; }
};

The override Specifier

Add override to derived methods. The compiler verifies the function actually overrides a virtual — catches typos in the signature.

The final Specifier

final prevents further overriding. Use on classes that should not be inherited from, or on methods that should not be overridden again.

class Concrete final : public Base { /* ... */ };
class Base {
    virtual void f() final;   // cannot be overridden
};

Calling Through a Base Pointer

The magic of polymorphism: a base pointer can dispatch to the correct derived method.

Shape* s = new Circle(3.0);
std::cout << s->area();   // 28.27... (Circle::area)
delete s;

The vtable Concept

Each polymorphic class has a hidden vtable — a table of function pointers. Each object holds a hidden pointer (vptr) to its class s vtable.

Cost of Virtual Calls

A virtual call is one extra indirection through the vtable. Negligible in most code. Avoid in inner loops if measured to be a bottleneck.

Pure Virtual Functions

Append = 0 to make a function pure virtual. Derived classes must override it to be instantiable. The class becomes abstract.

Virtual Destructors

If a class has any virtual function or is meant to be deleted through a base pointer, give it a virtual destructor. Otherwise the derived destructor never runs.

class Base {
public:
    virtual ~Base() = default;   // safe to delete through Base*
};

Polymorphism with Smart Pointers

Hold polymorphic objects through smart pointers to base. The smart pointer handles destruction correctly.

std::unique_ptr<Shape> s = std::make_unique<Circle>(3.0);

Slicing Revisited

Passing a derived object by value to a function expecting the base slices off the derived part. Use references or pointers for polymorphic parameters.

Performance Trade-off

Virtual functions add flexibility at a small runtime cost. Templates offer compile-time polymorphism with zero runtime cost — choose based on whether polymorphism is needed at runtime.

Quick Check

Why should a polymorphic base class have a virtual destructor?

Recap

Virtual functions enable runtime polymorphism through a per-class vtable. Mark overrides with override and prevent further inheritance with final. Always give polymorphic base classes a virtual destructor.

Frequently asked questions

Is the “Virtual Functions and the vtable” lesson free?

Yes — the full text of “Virtual Functions and the vtable” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “Virtual Functions and the vtable”?

Implement runtime polymorphism with virtual functions and inspect the vtable concept. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Virtual Functions and the vtable” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Single and Multiple Inheritance
  2. Virtual Functions and the vtable
  3. Abstract Classes and Pure Virtual Methods
  4. Inheritance vs Composition Trade-offs
← Back to C++ Academy