Polymorphism in c++

Card image cap

Polymorphism ?

Polymorphism is another important concept of object-oriented programming (OOPs). In terms of programming, this means you can have the same function in different contexts.


The word polymorphism means having many forms. Typically, polymorphism occurs when there is a hierarchy of classes and they are related by inheritance.

Polymorphism is a feature of OOPs that allows the object to behave differently in different conditions.

In C++ have two types of polymorphism:?

1) Compile time Polymorphism

2) Runtime Polymorphism


Card image cap

Compile time Polymorphism :

Function overloading and Operator overloading are perfect example of Compile time polymorphism.

we have two functions with same name but different number of arguments. Based on how many parameters we pass during function call determines which function is to be called, this is why it is considered as an example of polymorphism because in different conditions the output is different. Since, the call is determined during compile time thats why it is called compile time polymorphism.

Card image cap

Example: Compile time Polymorphism



Output :

Output: 30
Output: 66
Card image cap

Run time Polymorphism :

Function overriding is an example of Runtime polymorphism.

When child class declares a method, which is already present in the parent class then this is called function overriding, here child class overrides the parent class.

run time polymorphism in C++ object oriented programming is function overriding where functions get resolved at run time i.e. when we execute the application. This is different than compile time polymorphism where function get resolved at compile time itself .

Card image cap

Example: Run time Polymorphism



Output :

Super Class Function
Sub Class Function
Card image cap