Inheritance and polymorphism
Building classes on other classes, choosing the right function while the program runs with virtual functions, and describing abilities with abstract classes and interfaces.
About 10 hours of study
Learning objectives
- Derive classes and call base constructors
- Override virtual functions and use override
- Give polymorphic base classes virtual destructors
- Write abstract classes and interfaces
Lessons
- 1
Inheritance
A derived class is a base class plus more: shared code in one place, protected members, and the order constructors and destructors run.
- 2
Virtual functions and polymorphism
A virtual function call picks the version for the object's real class, so one loop over base pointers works for every kind of object.
- 3
Abstract classes and interfaces
A pure virtual function (= 0) makes a class abstract: a promise that every derived class keeps, and a way to write interfaces.
Module practice
- C++: A student is a personEasy
- C++: Employees and managersMedium
- C++: Sale prices that build on the base priceHard
- C++: Every animal speaks its own wayEasy
- C++: Shapes through a base pointerMedium
- C++: A payroll with three kinds of payHard
- C++: An abstract shape with two measurementsEasy
- C++: A payment interfaceMedium
- C++: A pipeline of number filtersHard
Module quiz
‹ Static members and operatorsTemplates and the standard library ›