C++ - SPLessons

C++ Polymorphism

Chapter 24

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

C++ Polymorphism

C++ Polymorphism

shape Description

As discussed in the previous chapter, Overloading is the part of polymorphism which has same name but varies in signature. The other part in Polymorphism is overriding.

Polymorphism in CPP can be defined as the capability of objects of different types responding to functions with same name in their own ways.

shape Conceptual
figure

Function overriding

shape Description

If the member function of parent class is given definition again in child class with same name,return-type and number of parameters, then the function is said to be overridden. The process is called Function overriding.

Function overriding involves Inheritance.Without inheritance, one cannot perform overriding on functions.

//Example for function overriding

class base_class
{
	public:
	void splessons()
	{
		cout << "Welcome to splessons.";
	}
};
class derived_class : public base
{
	public:
	void splessons()
	{
		cout << "You are reading splessons";
	}
}

Here, splessons in parent class is overridden by child class.

Concept of Binding

shape Description

Any function has to be called to make use of it. The connection of call function to the body of function is called as Binding.

If binding process is done before run-time, it is called as Compile-time binding or Static binding or Early binding.

//Example for early binding
#include<iostream>
using namespace std;
class parent_class
{
	public:
	void splessons()
	{
	cout << "Welcome to splessons.";
	}
};
class child_class:public parent_class
{
	public:
	void splessons()
	{
	cout << "You are reading c++";
	}
};

int main()
{
	parent_class p; //object of parent class
	child_class c; //object of child class
	p.splessons(); //Compile-time binding or early binding
	c.splessons();
}

Output

Welcome to splessons.You are reading c++

In compile-time binding, even if the child class is pointed and called, the compiler will point to parent class only.

Virtual functions

shape Description

To overcome the above difficulty of functions pointing to parent class irrespective of destination given, virtual functions are introduced. So,this process can be called as Run-time Binding or dynamic binding or late binding. The keyword used to represent virtual functions is “virtual“.

The keyword must be given to the parent class methods. When a parent class is declared as virtual, it is applicable to all the child classes derived from parent class.

shape Examples

Example without using virtual keyword

#include<iostream>
using namespace std;
class parent_class
{
	public:
	void splessons()
	{
		cout << "Welcome to splessons";
	}
};
class child_class:public parent_class
{
	public:
	void splessons()
	{
		cout << "You are reading c++"; 
	} 
}; 
int main() 
{ 
	parent_class* p; //object of parent class 
	child_class c; //object of child class 
	p= &c;
	p->splessons();   //Compile-time binding or early binding
}

Output

Welcome to splessons

Example with using virtual keyword

#include<iostream>
using namespace std;
class parent_class
{
	public:
	virtual void splessons()
	{
		cout << "Welcome to splessons";
	}
};
class child_class:public parent_class
{
	public:
	void splessons()
	{
		cout << "You are reading c++"; 
	} 
}; 
int main() 
{ 
	parent_class* p; //object of parent class 
	child_class c; //object of child class 
	p= &c; 
	p->splessons();   //Compile-time binding or early binding
}

Output

You are reading c++

The mechanism of run-time binding involves VTABLES and vpointers. vpointers stores the address of objects created in classes of virtual functions. When a virtual function is called the pointer binds to the correct address in the VTABLE.

Abstract classes

shape Description

Abstract classes contains minimum of one pure virtual functions. Pure virtual functions can be obtained by equating virtual functions to zero. It does not provide any definition to the function.

virtual void s()=0;

shape Example

#include<iostream>
using namespace std;
class parent_class         //Abstract parent class
{
	 public:
	 virtual void splessons() = 0;   //Pure Virtual Function
};

void parent_class :: splessons()         //Pure Virtual function definition
{
	cout << "Pure Virtual function definition\n";
}

class child_class:public parent_class
{
	 public:
	 void splessons()
	 { 
		cout << "Implementation of Virtual Function in child class"; 
	 } 
}; 
int main() 
{ 
	parent_class* p; 
	child_class c; 
	p = &c; 
	p->splessons();
}

Output

Implementation of Virtual Function in child class

Summary

shape Key Points

  • Changing the behavior of a form as per different function calls is known as Polymorphism in CPP.
  • Connecting function body and function call is called as binding.
  • Virtual functions follows run-time binding.
  • Abstract classes have atleast one pure-virtual function.

shape Programming
Tips

More generic code can be written using Polymorphism in CPP that works with object families, rather than writing code for a specific class.