CurriculumPolymorphism

Polymorphism

<strong>Polymorphism</strong> means 'many forms'. Call the same function on different objects and get different behavior, decided at runtime.

01Runtime Polymorphism

A base class pointer pointing to a derived object with a virtual function: the DERIVED version runs, decided at runtime.
main.cpp
Loading...
Terminal
Waiting for execution...
Visualizer

Click Run to see memory state

02Polymorphism with Collections

The true power: iterate through base-class pointers and call virtual functions. The correct derived version runs automatically.
main.cpp
Loading...
Terminal
Waiting for execution...

03Compile-Time vs Runtime Polymorphism

Compile-time (overloading) is decided at compile. Runtime (virtual functions) is decided while the program runs.
main.cpp
Loading...
Terminal
Waiting for execution...

04override and final Keywords

'override' confirms you are actually overriding a virtual. 'final' prevents further overriding down the chain.
main.cpp
Loading...
Terminal
Waiting for execution...

05dynamic_cast (Pro Tip)

Safely downcast a base pointer to a derived pointer at runtime. Returns nullptr if the cast fails.
main.cpp
Loading...
Terminal
Waiting for execution...
← Back to Curriculum