C++ Polymorphism

Polymorphism refers to having multiple forms or shapes and in C++, it means that an object can take on several forms. There are two types of polymorphism in C++: Compile-time polymorphism and Run-time polymorphism.

Compile-time Polymorphism

In this type of polymorphism, the compiler knows ahead of time which function will be executed. This is also referred to as early binding. There are two forms of compile-time polymorphism: function overloading and operator overloading.

Function overloading is when multiple functions have the same name but have different parameters. The compiler knows which function to execute based on the parameters passed to the function.

Operator overloading allows operators to perform specific tasks. For example, the + operator can be used to concatenate strings or add numerical values.

Run-time Polymorphism

Run-time polymorphism, also known as late binding, is when the compiler doesn’t know which function will be executed until runtime. This type of polymorphism is slower because function calls are decided at runtime. It can be achieved through virtual functions.

Virtual Functions

A virtual function is a member function in a base class that is declared using the virtual keyword and can be redefined in a derived class. Virtual functions can only be declared in the parent class, not as static.

Shubhajna Rai
Shubhajna Rai

A Civil Engineering Graduate interested to share valuable information with the aspirants.

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in