Link Search Menu Expand Document

Polymorphism in C++

Polymorphism is a term that refers to the presence of several forms. It may be defined as a message’s capacity to appear in several forms. As a result, the same method name may be used to operate on many data types. As a result, if you “iterate” through them, they’ll all cause the same effect. Even though we only have one function, it acts differently depending on the situation. We assume you are familiar with the term polymorphism by now, but we will now describe how polymorphism works in C++.

Types of Polymorphism in C++

Compile Time Polymorphism

It’s also known as early binding polymorphism or static polymorphism. You should be able to describe it by looking at the parameters you’re sending in. And since the compiler knows which function to perform before the program is created, it is a compile-time polymorphism.

Categories of Compile Type Polymorphism

  • Function Overloading

Function overloading occurs when numerous functions have the same name but different arguments. So, the compiler will discover two functions with the same name in this case.

void fun(int x)
void fun(double x)

In this scenario, the same “fun” works in two ways depending on the parameters ‘int’ and ‘double,’ resulting in two different outputs.

  • Operator Overloading

Operator overloading is a sort of polymorphism in which different operators are implemented differently depending on their parameters.

int main(){
int x1=20, y1=30;
string x2="20", y2="30"; cout<<x1+y1<<<<"\n";

The ‘+’ operator adds the two integer operands but concatenates the two string operands’ values to make things easier. As a result, we can see that the ‘+’ operator behaves differently in different situations.

Run Time Polymorphism

It’s utilized to achieve polymorphism at runtime. It concludes the object at run time and then specifies which function call to bind to it, as opposed to “compile-time” or static polymorphism. Moreover, we can also call it dynamic or late binding.

Categories of Run Time Polymorphism

  • Function Overriding

In C++, function overriding occurs when a derived class defines the same function as its base class. This allows you to give a more particular implementation of a method that its underlying class already provides.

class Animal
void makeSound()
cout<<"Animals sound"
class Dog: public Animal
void makeSound()
coutes<<"Dogs bark"

Because the derived class Dog contains the same function as its base class, Animal, this is the simplest illustration of how function overriding in C++ works. This basically follows the inheritance pattern.


You can generate a pointer to a derived class that is also compatible with the base class using polymorphism. Furthermore, it saves the programmer a lot of time by eliminating the need to recreate code. If you understand the various types of polymorphisms in C++, you may make your work much more manageable with very little code. We hope the notion of polymorphism is apparent to you, but if not, you may work your way through it by trying your hand at C++.

Other useful articles:

Back to top

© , C Sharp Online — All Rights Reserved - Terms of Use - Privacy Policy