Polymorphism in C++

In this tutorial we will study and understand the concept and working of Polymorphism in C++ and its types.

Polymorphism in C++

The word polymorphism means having many forms. In the case of Polymorphism in C++ one form represent original form or original method always resides in base class and multiple forms represents overridden method which resides in derived classes.
There are 2 types of polymorphism:

  • Compile time polymorphism
  • Run time polymorphism

 

Polymorphism in C++

Compile time / Static / Early Binding polymorphism

In C++ programming you can achieve compile time polymorphism in two way, which is given below;

  • Function Overloading
  • Operator Overloading (we will see this in detail in other tutorial)
  • Function Overriding
Method Overloading in C++

Whenever same method name is exiting multiple times in the same class with different number of parameter or different order of parameters or different types of parameters is known as method overloading.

Example of Method Overloading in C++

As you can see in this program we have 2 variations of the function named sum. depending on the number of arguments being passed during the call, the appropriate function is called by the program.

Run Online

#include<iostream>
using namespace std;
class Addition
{
	public:
		void sum(int a, int b)
		{
			cout<<a+b;
		}
		void sum(int a, int b, int c)
		{
			cout<<a+b+c;
		}
};
int main()
{
	Addition obj;
	obj.sum(10, 20);
	cout<<endl;
	obj.sum(10, 20, 30);
	return 0;
}
Output
Total Objects: 1
30
60
Method Overriding in C++

Function overriding, in object oriented programming, is a language feature that allows a subclass or child class to provide a specific implementation of a method that is already provided by one of its super classes or parent classes.

Example of Method Overloading in C++

Here we created a class named Base class and another class named Derived class which inherits all properties of base class publicly. Both classes have same function named show with same function prototype, however both print different message according to their respective class’s. Thus the functionality is different. In the main function we created 2 objects of each of these classes and called their respective show functions. when the function of the derived class show is called, it overrides the base function show and prints “Derived class” instead of Base class. This is Function overriding.

Run Online

#include<iostream>
using namespace std;
class Base
{
 	public:
 	void show()
 	{
  		cout << "Base class\t";
 	}
};
class Derived:public Base
{
 	public:
 	void show()
 	{
  		cout << "Derived Class";
 	}
};

int main()
{
   Base b;       //Base class object
   Derived d;     //Derived class object
   b.show();     //Early Binding Occurs
   d.show();   
}
Output
Total Objects: 1
Base class    Derived Class
Runtime time / Dynamic / Late binding polymorphism

Dynamic polymorphism can be achieved using Virtual Functions in C++.

Virtual Functions

Virtual Function is a function in base class, which is overrided in the derived class, and which tells the compiler to perform Late Binding / Dynamic Polymorphism on this function. Virtual Keyword is used to make a member function of the base class Virtual.

Late Binding

In Late Binding function call is resolved at runtime. Hence, now compiler determines the type of object at runtime, and then binds the function call. Late Binding is also called Dynamic Binding or Runtime Binding.

Problem without Virtual Keyword

In this program you can see that even when we use the base class object to call the function of child class, it calls the base class function. This results in early binding which is not what we want. This problem can be resolved using the virtual keyword

Run Online

#include<iostream>
using namespace std;
class Base
{
 public:
 void show()
 {
  cout << "Base class";
 }
};
class Derived:public Base
{
 public:
 void show()
 {
  cout << "Derived Class";
 }
};

int main()
{
 Base* b;       //Base class pointer
 Derived d;     //Derived class object
 b = &d;
 b->show();     //Early Binding Ocuurs
}
Output
Total Objects: 1
Base class
Using Virtual Keyword

We can make base class’s methods virtual by using virtual keyword while declaring them. Virtual keyword will lead to Late Binding of that method. In this program given below you can see that we have made the base class function show as virtual by using the virtual keyword. Now in the main function we are called the derived class function using the base class pointer object. this is known as late binding or dynamic polymorphism

Run Online

#include<iostream>
using namespace std;
class Base
{
 public:
 virtual void show()
 {
  cout << "Base class";
 }
};
class Derived:public Base
{
 public:
 void show()
 {
  cout << "Derived Class";
 }
};

int main()
{
 Base* b;       //Base class pointer
 Derived d;     //Derived class object
 b = &d;
 b->show();     //Late Binding Ocuurs
}
Pure Virtual Functions

It’s possible that you’d want to include a virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class. We can change the virtual function area() in the base class to the following:

class Shape {
protected:
int width, height;
public:
Shape( int a = 0, int b = 0) {
width = a;
height = b;
}

// pure virtual function
virtual int area() = 0;
};

The = 0 tells the compiler that the function has no body and above virtual function will be called pure virtual function.

Abstract base classes

Classes which have pure virtual functions(such as the one in the above example) are known as Abstract base classes or simply Abstract class. Abstract base classes cannot be used to instantiate objects. But an abstract base class is not totally useless. It can be used to create pointers to it, and take advantage of all its polymorphic abilities and perform dynamic polymorphism.

Watch it on YouTube

Leave a Reply

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