Virtual Function in Object Oriented Programming (OOP)

Virtual means existing in appearance but not in reality. A virtual function is a special member function defined in the base class and overridden in one or more derived classes. It means that name of the function and its signature in base and derived classes remains the same.

Polymorphism is achieved through the use of virtual functions.

A virtual function is declared by using keyword “virtual” before function declaration in the base class. The functions with the same names are declared in the derived classes.

However, the use of keyword “virtual” is optional for the functions of the same name in derived classes. Once a function is declared as virtual in base class, it remains virtual in the inheritance hierarchy.

Early Binding 

The assignment of type to variables and expressions at compilation time is known as early binding. it is also known as static binding. The early binding occurs when everything required to call a function at compile time. Early binding enables the compiler to know exactly which function will be called when a certain statement executed.

When a Program is compiled, the compiler checks the function calls and decides which function is to be executed. This process takes place during compilation process in normal programs with functions.

Late Binding

The assignment of type to variables and expressions at execution time is known as early binding. It is also called dynamic binding.  In late binding compiler does not know at compile time which function will be called when a certain statement is executed.

Example-1

#include<iostream>
using namespace std;

class base1
{
	public:
		virtual void print()
		{
			cout<<"print() function of virtual base class";
		}
};
class derive1 : public base1
{
	public:
		void print()
		{
		cout<<"print() function of derive 1 class";
		}
};
class derive2 : public base1
{
	public:
		void print()
		{
			cout<<"print() function of derive 2 class";
		}
};
main()
{
	base1 b;
	base1 *pb;
	derive1 d1;
	derive2 d2;
	pb = &b;
	pb->print();
	pb = &d1;
	pb->print();
	pb = &d2;
	pb->print();
}

In above program, the function “print” is declared as virtual in the base class. In the derived classes, the same function is redefined with the same name and signature but the keyword “virtual” is not used.

Example-2

#include<iostream>
using namespace std;

class shape
{
	public:
		virtual void draw()
		{
			cout<<"draw() function of virtual base class";
		}
};
class circle : public shape
{
	public:
		void draw()
		{
			cout<<"draw() function of circle class";
		}
};
class pie : public shape
{
	public:
		void draw()
		{
			cout<<"draw() function of pie class";
		}
};
class square : public shape
{
	public:
		void draw()
		{
			cout<<"draw() function of square class";
		}
};
class triangle : public shape
{
	public:
		void draw()
		{
			cout<<"draw() function of triangle class";
		}
};
class rectangle : public shape
{
	public:
		void draw()
		{
			cout<<"draw() function of rectangle class"<<endl;
		}
};
main()
{
	shape b;
	shape *pb[5];
	circle d1;
	pie d2;
	square d3;
	triangle d4;
	rectangle d5;
	pb[0] = &d1;
	pb[1] = &d2;
	pb[2] = &d3;
	pb[3] = &d4;
	pb[4] = &d5;
	for(int i = 0; i <= 4; i++)
     {
	    pb[i]->draw();
     }
}

In the above program, an array of pointer ‘pb’ to object of the base class “shape” is declared. Similarly, the objects of the derived classes are declared. The addresses of these objects are assigned to elements of ‘pb’. All the “draw” function of the derived classes are accessed through a single statement by using loop. 

Pure Virtual Function

A type of virtual function that has no body can be called as pure virtual function. A function may be declared as a pure virtual function by adding two things:

The keyword “virtual” at the start of function declaration.

The function is initialized with 0.

Pure virtual functions are used in Abstract classes. The user can inherit the class and then override pure virtual function in child class.

Syntax:

virtual return-type function-name() = 0 ;

Code Example

class Parent
{
 public:
 virtual void show() = 0;
};
class Child : public Parent
{
public:
void show()
{
   cout<<"Child Class";
}
};
main()
{
   Parent *p;
   p = new Child();
   p -> show();

}

Remember

A class that contains pure virtual functions is known as abstract class.

If there is an abstract class which contains a pure virtual function. Then all the inherited class from abstract must override pure virtual function.

Leave a Reply