Search⌘ K
AI Features

Dynamic vs. Static Polymorphism

Learn the key differences between dynamic polymorphism using virtual functions for runtime flexibility and static polymorphism with templates and overloaded functions resolved at compile time. Understand how these forms impact C++ programming patterns and performance.

When we learn about object-oriented programming, we learn about its fundamental principles, which are abstraction, encapsulation, inheritance, and polymorphism. C++ is a multi-paradigm programming language that supports object-oriented programming too. Although a broader discussion on the principles of object-oriented programming is beyond the scope of this section and this course, it’s worth discussing at least some aspects related to polymorphism.

Polymorphism

So, what is polymorphism? The term is derived from the Greek words for “many forms.” In programming, it’s the ability of objects of different types to be treated as if they were of the same type. The C++ standard actually defines a polymorphic class as follows: “A class that declares or inherits a virtual function is called a polymorphic class.”

It also defines polymorphic objects based on this definition, as follows: “Some objects are polymorphic; the implementation generates information associated with each such object that makes it possible to determine that object’s type during program execution.”

However, this actually refers to what is called dynamic polymorphism ...