The short answer
A widely used classification, from a 1985 paper by Luca Cardelli and Peter Wegner, splits polymorphism into four kinds in two groups:
- Ad hoc polymorphism: overloading and coercion.
- Universal polymorphism: parametric polymorphism and inclusion polymorphism, also called subtype polymorphism.
Other sources group these ideas differently, for example as compile-time and runtime polymorphism. The examples below use the four-way split and show each one in C++.
1. Overloading
Overloading uses the same name for several functions with different parameter lists. Each version is a separate function, and the compiler picks one based on the arguments.
int twice(int x) { return x * 2; }
double twice(double x) { return x * 2.0; }
int main() {
twice(3); // calls the int version
twice(1.5); // calls the double version
}
Operators can be overloaded too, which is why readers ask, “Does C++ allow operator overloading?” It does. The expressions std::cout << 42 and std::cout << "text" already use different overloads of operator<<.
2. Coercion
Coercion is an implicit conversion that lets a value be used where a different type is expected. The compiler inserts the conversion for you.
#include <iostream>
double half(double x) { return x / 2.0; }
int main() {
int n = 7;
double result = half(n); // n is converted to double
std::cout << result << '\n'; // prints 3.5
}
Coercion counts as ad hoc because each conversion is specific to a pair of types, such as int and double.
3. Parametric polymorphism
Parametric polymorphism writes one definition that works for many types. In C++, this is what templates do. The compiler generates a version for each type you use.
template <typename T>
T largest(T a, T b) {
return (a > b) ? a : b;
}
int main() {
largest(3, 9); // T is int, result is 9
largest(2.5, 1.5); // T is double, result is 2.5
}
The same code handles both calls. Only the generated version changes.
4. Inclusion polymorphism
Inclusion polymorphism, also called subtype polymorphism, lets a value of a derived type be used where a base type is expected. Virtual functions then choose the behavior for the real object. This is usually what people mean by runtime polymorphism in C++.
#include <iostream>
class Animal {
public:
virtual ~Animal() = default;
virtual void speak() const { std::cout << "...\n"; }
};
class Dog : public Animal {
public:
void speak() const override { std::cout << "Woof\n"; }
};
void makeSpeak(const Animal& animal) {
animal.speak();
}
int main() {
Dog dog;
makeSpeak(dog); // prints "Woof"
}
The four kinds in C++ at a glance
| Kind | Group | C++ tool | When the choice is made |
|---|---|---|---|
| Overloading | Ad hoc | Same name, different parameter lists | Compile time |
| Coercion | Ad hoc | Implicit conversions such as int to double |
Compile time |
| Parametric | Universal | Templates | Compile time |
| Inclusion | Universal | Virtual functions and inheritance | Runtime |
For the first kind in particular, “What is overloading and its types?” is a useful companion question.
Why the split matters
Knowing which kind you are using tells you where errors show up. Overloading mistakes and template errors usually appear when the code is compiled. Inclusion-based mistakes, such as a missing virtual keyword, can run without any compiler error and only show up as the wrong output.