Simply speaking, polymorphism is the ability of something to be displayed in multiple forms. Let’s take a real life scenario; a person at the same time can perform several duties as per demand, in the particular scenario. Such as, a man at a same time can serve as a father, as a husband, as a son, and as an employee. So, single person possess different behaviors in respective situations. This is the real life example of polymorphism. Polymorphism is one of the important features of OOP (Object Oriented Programming).

The word polymorphism is a Greek word, where poly means many or multiple and morph means forms, so the word polymorphism in literal means, having multiple forms.

In C++ Polymorphism is mainly divided into two types

1) Compile Time Polymorphism and

2) Runtime Polymorphism

Types of Polymorphism in C++

Types of Polymorphism in C++

Compile Time Polymorphism

This type of polymorphism is also known as static polymorphism and is achieved by overloading a function/method or an operator.

Compile Time Polymorphism in C++

Compile Time Polymorphism in C++

Function Overloading

When multiple functions are used at different places with same name but different parameters then these functions are known as overloaded function. Functions can be overloaded in two ways:

  • By change in number of arguments
  • By change in type of arguments.

Let’s consider an example.

Output:

value of a is 3

value of a is 6.456

value of a and b is 8, 71

The above example perfectly explains the concept of function overloading, a single function/method named function acts differently in 3 different situations which is the property of polymorphism.

Operator Overloading

The second method of compile time polymorphism is operator overloading. For example, we can make the operator (‘+’) for string class to concatenate two strings.  The general concept regarding “+” operator is that it is an addition operator whose task is to sum up two operands. But here in polymorphism, it will be used for a different purpose.

So, a single operator ‘+’ when placed between strings, concatenate them and when placed between integer operands, adds them. Let’s take an example of illustrating Operator Overloading using C++.

Output:

The result of adding two complex numbers by operator overloading is 13 + i21

In above mentioned example the “+” operator is overloaded. Here the operator is made to perform addition of two complex numbers.

Testop operator + (Testop const &obj)  is called automatically when the operator “+” is used in the main function.

Here it is important to mention that, operator overloading can be done on both unary as well as binary operators.

Runtime polymorphism

This is the second type of polymorphism. It can be achieved by Function Overriding.

Runtime polymorphism in C++

Runtime polymorphism in C++

Function overriding

Function overriding is giving another definition to an existing method with same parameters or we can say that a method has same prototype in base as well as derived class.

For example: when an inherited class in c++ has a different definition for one of functions of the base class then here the function of base class is said to be overridden.

Output:

Result method of Base class is called

The result method of derived class is called

In the above-mentioned example, the result () method of base class is overridden and redefined in the base class.

Runtime Polymorphism using virtual functions

Runtime polymorphism can also be achieved by virtual functions. A virtual Function is the member of base class and is overrided in the derived class. The syntax of a virtual function is to precede its declaration with keyword “Virtual”.

Here are some c programs to demonstrate how virtual pointers, virtual tables and virtual functions work in C++

Virtual function actually tells the compiler to perform Dynamic  Binding (resolves function call at runtime) on it.

Output:

275

137

In the above-amentioned example, there are three classes in all (Polygon, Rectangle and Triangle) and all have the same functions set_values and area and same members: w, h.

Function main declares three pointers, ppoly1, ppoly2 and ppoly3 to Polygon. The first two pointers are assigned the addresses of rec and trg that are objects of Rect and Tri. This type of assignment is valid, as both classes Rect and Tri are derived from Polygon.

*ppoly1 and *ppoly2 is dereferencing of both pointers and enables us to access the members of their pointed objects. To understand the concept, consider the following example,

ppoly1->set_values (11,25);      is equivalent to      rec.set_values (11,25);

In base class, the member function “area” is declared as virtual and is later redefined in both of the derived classes Rect and Tri. So, it allows us to access the non-virtual members of derived classes via a reference of the base class. But if the keyword “virtual” is removed from the declaration of area in the base class, all three calls to area would return zero.

The virtual keyword allows the members of a derived class to be called appropriately who have the same name as one of the member of the base class. More accurately when the type of the pointer is a pointer to the parent class, which is pointing to an instance of the derived class, as you can see in the above example.

 

0 Shares
Share
+1
Tweet
Share
Stumble