Module 6 · Inheritance and Polymorphism — topic 2 of 5
Polymorphism
One interface, many forms. Compare compile-time polymorphism (overloading, templates) with run-time polymorphism (virtual functions through base references).
Learning objectives
- Distinguish static and dynamic polymorphism
- Call derived behaviour through base references and pointers
- Store polymorphic objects in containers safely
Definition
Polymorphism is the ability of a single interface to work with different types. C++ provides compile-time (static) polymorphism through overloading and templates, and run-time (dynamic) polymorphism through virtual functions called via base-class pointers or references.
Introduction
area(shape) should work for circles, squares and triangles. With polymorphism the calling code doesn't need an if-else chain over every shape type; each type supplies its own behaviour and the right one runs automatically.
Why it matters
Polymorphism makes programs open for extension: adding a new shape requires no change to code that processes shapes.
It is a core OOP exam topic and appears in every framework you will use.
Detailed explanation
Static polymorphism: the compiler picks the function — overloading (print(int) vs print(double)) and templates (max<T>). No run-time cost.
Dynamic polymorphism: a virtual function called through a Base& or Base* runs the most-derived override, decided at run time.
Only references and pointers dispatch dynamically. Copying a derived object into a base value slices it, losing the derived behaviour.
Containers of polymorphic objects store pointers, typically std::vector<std::unique_ptr<Base>>.
Syntax
- The base class must declare the function
virtual. - Use
unique_ptrto own polymorphic objects. - Give the base a virtual destructor.
Code examples
Static polymorphism
42 2.5 abab
Dynamic dispatch through a reference
meow moo
More examples
A shape collection and object slicing
rect 12 circle 3.14159 total 15.1416 shape 0
Step-by-step explanation
- The vector owns objects of different derived types through
unique_ptr<Shape>. s->area()dispatches to Rect or Circle at run time.Shape sliced = r;copies only the base part, so it behaves as a plain Shape.- Pass and store polymorphic objects by pointer or reference to avoid slicing.
Expected output
Running “A shape collection and object slicing” prints exactly:
Practical applications
- Renderers draw any object implementing a Drawable interface.
- Payment systems process any PaymentMethod.
- Plugin architectures load classes that implement a known interface.
Best practices
- Use virtual functions for behaviour that varies by type.
- Own polymorphic objects with smart pointers.
- Prefer templates when the type is known at compile time and speed matters.
Common mistakes
Storing derived objects by value in a base container
Avoid
Prefer
By-value storage slices every object to a Shape.
Type-switching instead of polymorphism
Avoid
Prefer
Every new type would require editing every switch.
Important notes
- Dynamic dispatch costs one indirect call; it prevents some inlining.
dynamic_cast<Derived*>(base)checks the real type at run time and returns nullptr if it doesn't match.
Key takeaways
- Static polymorphism: overloading/templates, resolved at compile time.
- Dynamic polymorphism: virtual functions via pointers/references.
- Copying into a base value slices the object.
Try it yourself
- 1Add a
Triangleclass to the shape example without modifying the loop in main. - 2Write a function template
largestthat works for ints, doubles and strings.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
5 questions test this exact topic. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerFunction overloading and templates are examples of…
- ModerateWhat is printed?