Polymorphism
Polymorphism is an important concept in object-oriented programming. It means “more than one form” — the same entity (function or operator) can operate differently under different situations.
There are two types of polymorphism in C++:
- Compile-time polymorphism: This is also called static (or early) binding.
- Runtime polymorphism: This is also called dynamic (or late) binding.
Compile-time Polymorphism
Compile-time polymorphism is perfect for function overloading as well as operator overloading.
In the example below, there are two functions with the same name, sum()
, but a different number of arguments.
The number of parameters passed during function invocation (function calling) determines which function to be called. This is why it is considered as an example of polymorphism because the output is different in different conditions. Since the call is determined during compile time, it is called compile-time polymorphism.
#include <iostream>class Add {public:// sum() with 2 parametersint sum(int num1, int num2) {return num1 + num2;}// sum() with 3 parametersint sum(int num1, int num2, int num3) {return num1 + num2 + num3;}};int main() {// Create object of Add classAdd obj;// This will call the first variant of the sum() functionstd::cout << "Output: " << obj.sum(10, 20) << "\n";// This will call the second variant of the sum() functionstd::cout << "Output: " << obj.sum(11, 22, 33) << "\n";return 0;}
The output would look like:
Output: 30Output: 66
Runtime Polymorphism
Function overriding is an example of runtime polymorphism.
When a child class declares a method that is already present in the parent class, it is called function overriding because the child class overrides the parent class.
In the case of function overriding, there are two definitions of the same function, one in the parent class and one in the child class. Since the call to the function is determined at runtime to decide which definition of the function is to be called, it is called runtime polymorphism.
#include <iostream>class A {public:void print() {std::cout << "Output: Parent Class Function" << "\n";}};class B: public A {public:void print(){std::cout << "Output: Child Class Function" << "\n";}};int main() {// Parent class objectA obj1;obj1.print();// Child class objectB obj2;obj2.print();return 0;}
The output would look like:
Output: Parent Class FunctionOutput: Child Class Function
All contributors
- Anonymous contributor
- Christine_Yang
- garanews
- Anonymous contributor
- aashishKushwaha8818184398
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours