TRT
Sign in

Core · Module 6

Inheritance and Runtime Polymorphism

Model is-a relationships with inheritance, dispatch work at run time with virtual functions, and close every hierarchy safely.

Start the modulePro6 lessons · about 63 minutes

Inheritance lets a Book reuse everything an Item already does, and virtual functions let one call do the right thing for every kind of item. The price is a hidden table of function pointers and strict rules about destruction and ownership. This module shows both: how to share code safely, and when composition beats inheritance.

  1. 1Inheritance: Reusing and Extending TypesPro — members onlyReuse base class state and behavior in derived types, recognize the five inheritance shapes, and predict construction order and member lookup in small hierarchies.9 min
  2. 2Virtual Functions and Dynamic DispatchPro — members onlyCall one interface and reach the right implementation at run time, using virtual functions, override and the vtable that makes it work.11 min
  3. 3Virtual Destructors: Closing a Hierarchy SafelyPro — members onlyDestroy derived objects through base pointers without leaks by declaring a virtual destructor, and trace the construction and destruction order it guarantees.10 min
  4. 4Abstract Classes and Pure InterfacesPro — members onlySeparate shared state from pure contracts with abstract bases and pure interfaces, and enforce implementation in derived types through pure virtual functions.11 min
  5. 5The Diamond and Virtual InheritancePro — members onlyResolve the diamond ambiguity with virtual inheritance so two paths share one base part, and trace how the most derived type builds that shared base.10 min
  6. 6Project: A Polymorphic Library CataloguePro — members onlyCombine inheritance, virtual dispatch, and safe teardown into a catalog that stores mixed items behind base handles and reports per-kind loan periods.12 min