Polymorphism in c++

C++ 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


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.

Example: Compile time Polymorphism



Output :

Output: 30
Output: 66

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 .

Example: Run time Polymorphism



Output :

Super Class Function
Sub Class Function