Virtual Function in C++

As we know, the C++ programming language offers various benefits to programmers. It has numerous exciting and useful features and functionalities. In this tutorial, we are going to discuss the virtual functions in C++. And, Why should you make use of these functions in your program code?

C++ Virtual Function

In C++, it is a member of a function in a class that we declare in the base class and we also redefine it in a derived class. It tells the compiler to perform dynamic linkage or late binding on the function. The C++ programming language determines which function will be invoked at the runtime if the function is defined as virtual and it is based on the type of object that is pointed by the base class.

The ‘virtual’ keyword is used to precede the normal declaration of a function. There is a need to use the single pointer to refer to all the objects of the different classes.

Late Binding or Dynamic Linkage

In late binding, the compiler determines the type of the object at runtime. And after that, it binds the function call. Late binding is mainly used to resolve function calls during runtime.

Rules of Virtual Function in C++

  • It can’t be static.
  • It must be members of some other class.
  • It also can be a friend function of another class.
  • Virtual functions are always defined in the base class.
  • You can define a class as a virtual destructor but you can’t define it as a virtual constructor.
  • Always remember that the prototype of virtual functions should be the same in the base class as well as the derived class.

Example:- C++ Virtual Function

#include <iostream>
using namespace std;
 
class example{
public:
  virtual void print()
  {
    	cout << "Printing Base Class!" << endl;
  }
 
  void show()
  {
    	cout << "Showing Base class!" << endl;
  }
};
 
class Tech:public example{
public:
  void print()
  {
    	cout << "Printing Derived class!" << endl;
  }
 
  void show()
  {
    	cout << "Just showing derived class!" << endl;
  }
};
 
int main()
{
  example* ptr;
  Tech d;
  ptr = &d;
  ptr->print();
  ptr->show();
}

Output:-

Printing Derived class!
Showing Base class!

Pure Virtual Function in C++

You can declare a pure virtual function but you cannot implement it. It is a virtual function that exists but cannot be implemented. You cannot use virtual functions for performing any task, it is just used as a placeholder. If a class contains the pure virtual function then you cannot declare the objects of its own. These classes are known as abstract base classes.

Main purpose of the base class is to provide the characteristic of the derived classes. And also used to create the base pointer which is used for achieving the runtime polymorphism.

Defining a pure virtual function

virtual float fun() = 0;

Example of C++ Pure Virtual Function

#include <iostream>  
using namespace std;  
class Tech  
{  
  public:  
  virtual void display() = 0;  
};  
class Ex: public Tech
{  
  public:  
  void display()  
  {  
    	std::cout << "TechVidvan Tutorial: C++ Pure Virtual Function!" << std::endl;  
  }  
};  
int main()  
{  
  Tech *ptr;  
  Ex d;  
  ptr = &d;  
  ptr->display();  
  return 0;  
}

Output:-

TechVidvan Tutorial: C++ Pure Virtual Function!

Above, the base class contains the pure virtual function.

Summary

In this tutorial, we discussed the virtual function in C++ in detail. We also discussed the late binding and the pure virtual function in C++. Hope you liked the article.