C++, End to End
Compile-Time and Runtime Polymorphism, Previewed
A short bridging page, worth reading once and then coming back to after you've seen both halves in full. C++ gets you "polymorphism", one interface, multiple behaviors, in two completely different ways, and conflating them is a common source of confusion for people learning the language.
Compile-time polymorphism: templates
The function templates chapter earlier in this course showed this already: a single generic function or class definition gets a distinct, fully concrete version generated for each type it's actually used with, decided entirely at compile time. There's no runtime cost, no virtual dispatch, no indirection, the compiler picks the right code to run before the program ever executes.
Runtime polymorphism: virtual functions
Inheritance and virtual functions (both covered in depth later in this course) solve a different problem: calling the *right* version of a function through a common base-class pointer or reference, when the actual concrete type isn't known until the program is running, maybe it depends on user input, a config file, or which subclass got constructed somewhere else entirely.
Why the distinction matters
Templates cost nothing at runtime but require the type to be knowable at compile time, and generate more code (one instantiation per type used). Virtual functions cost a small, real runtime overhead (an indirect call through a vtable) but let you genuinely decide behavior based on information only available while the program is running. Neither one is a strictly better version of the other, they answer different questions: "can I write one piece of code that works for many types" versus "can I call the correct behavior for an object whose exact type I don't know yet".
A rule of thumb worth internalizing early: if you know all the types you'll ever need at compile time and just want to avoid duplicating code, reach for templates. If the whole point is handling types (or subclasses) that don't exist yet, plugins, a base class other people will extend, a collection of heterogeneous objects processed through one shared interface, that's what virtual functions and inheritance are for.