Menu

What is Polymorphism and Types in C++ with Example

What is polymorphism and types in c++ with example

Sharing is caring!

Introduction to Polymorphism in C++

If you are already known to programming and ever heard about Object Oriented Programming (OOP) then you must have heard the word polymorphism which is one of the major pillar of OOP.

In simplest term polymorphism in C++ is using one thing in many forms. The most common example is a human that perform different tasks at different places such as in office he/she is an employee, whereas in market he/she is a customer and in college or school a student.

You will also read: Why Learn C++

Some Other Real World Examples Are:

1. Example : we use one word at different situation with different meanings in English language.

2. Example : many of us are fan of cartoon series such as ben ten who takes different characters according to the situation, which fits as an example of polymorphism in C++.

Polymorphism Types:

three types of polymorphism in C++ are used in programming languages:

1. Ad Hoc Polymorphism:

follows the early binding concept in which the attributes are bind to its entities at compile time. The scope is same in this type.
Overloading is achieved in this type of polymorphism, such as

Function overloading:

function with same name, but defined multiple time within same scope (class) and to make it easy for compiler to bind the function call with right function definition some differences are there such as number of arguments passed and types of arguments used in function.

Example: using C++

Function overloading C++

Constructor Overloading:

in OOP constructor is similar to function so passing arguments to the function is also possible.
One can overload it using parameterised constructor concept.

Example: using C++

Operator Overloading:

when one wants to use an operator to perform operation on user defined type of value such as addition between objects of the class then this term is used.

2. Parametric Polymorphism

it is an advantage over function overloading concept as in overloading same function is defined multiple time which means code is not reduced but only redefined with different number or types of parameter but in case a programmer wants to use only one function for all kind of operations he/she can find it useful for such operation.

Templates or generics are example of this type.

Templates are able to hold any type of value similar like one can get multiple things from a general store that is why these are also called generics.

3. Subtype Polymorphism

considered as run time polymorphism as the binding task is performed at runtime (late binding).

  • Early Binding: the task is executed at compile time and hence in statically typed language this binding is used.
  • Late Binding: the task is executed at run time and is followed in dynamically typed language.

Overriding is used to achieve this polymorphism.

Overriding:

in it the scope is different i.e., same function or method is overridden (defined) in different classes without changing its number and types of parameter.

  • Pure virtual function is the best example that also helps achieving Abstraction because the class in which pure virtual function declared does not allow to create its object. This is used along with Inheritance.
Overriding

Overriding

Output:

Error: cannot create the instance of abstract base class.

C++ Training

Leave a Reply

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

shares