C++ Tutorial: Inheritance & Polymorphism
Extend classes with inheritance, virtual functions, pure virtual functions, abstract classes, and polymorphic dispatch.
Build on what exists
Inheritance creates specialised versions of existing classes. `virtual` functions enable polymorphism — the ability to call the right function based on the actual object type at runtime, not the variable type.
Virtual functions are what make game engines extensible: `Entity::update()` calls the right `Player::update()`, `Enemy::update()`, or `Bullet::update()` based on what the object actually is.
What you'll learn in this C++ inheritance & polymorphism tutorial
This interactive C++ tutorial has 6 hands-on exercises. Estimated time: 22 minutes.
- Inheritance basics — `class Child : public Parent` inherits all public members.
- virtual — enable polymorphism — Without `virtual`, the BASE class version is always called even on derived objects.
- Pure virtual — abstract classes — A **pure virtual** function has no implementation in the base class:
- Virtual destructor — essential! — If you delete a derived object through a base pointer without a virtual destructor, the derived destructor never runs — …
- dynamic_cast — safe downcasting — `dynamic_cast` safely converts a base pointer/reference to a derived type:
- Build a shapes calculator — No starter code.
C++ Inheritance & Polymorphism concepts covered
- Build on what exists