Home C++ Programming Tutorials Virtual Function in C++

Virtual Function in C++

by adarshpal

In this article, you’ll learn about Virtual Function in C++, rules of virtual function ,late binding, Pure Virtual Function and more.

A virtual function is a member function that is declared within a base class and is re-defined(Overriden) by a derived class. It is declared using the virtual keyword.

When the function is made virtual, C++ determines which function is to be invoked at the runtime based on the type of the object pointed by the base class pointer rather than the type of function.

Rules of Virtual Function

  • Virtual functions must be members of some class.
  • Virtual functions cannot be static members.
  • A virtual function must be defined in the base class, even though it is not used.
  • They are accessed through object pointers.
  • They can be a friend of another class.
  • The prototypes of a virtual function of the base class and all the derived classes must be identical. If the two functions with the same name but different prototypes, C++ will consider them as overloaded functions.
  • We cannot have a virtual constructor, but we can have a virtual destructor
  • Consider the situation when we don’t use the virtual keyword.

Late binding or Dynamic linkage

In late binding function call is resolved during runtime. Therefore compiler determines the type of object at runtime, and then binds the function call.

Let’s see the simple example of C++ virtual function used to invoked the derived class in a program.

#include <iostream>  
using namespace std;  
class A  
{  
   int x=20;  
    public:  
    void display()  
    {  
        std::cout << "Value of x is : " << x<<std::endl;  
    }  
};  
class B: public A  
{  
    int y = 10;  
    public:  
    void display()  
    {  
        std::cout << "Value of y is : " <<y<< std::endl;  
    }  
};  
int main()  
{  
    A *a;    //pointer of base class    
    B b;     //object of derived class    
    a = &b;  
   a->display();  //Late Binding occurs    
    return 0;  
}  

Output

Value of x is : 20

In the above example, * a is the base class pointer. The pointer can only access the base class members but not the members of the derived class. Although C++ permits the base pointer to point to any object derived from the base class, it cannot directly access the members of the derived class. Therefore, there is a need for a virtual function which allows the base pointer to access the members of the derived class.

Let’s see the simple example of C++ virtual function used to invoked the derived class in a program.

#include <iostream>    
{    
 public:    
 virtual void display()    
 {    
  cout << "Base class is invoked"<<endl;    
 }    
};    
class B:public A    
{    
 public:    
 void display()    
 {    
  cout << "Derived Class is invoked"<<endl;    
 }    
};    
int main()    
{    
 A* a;    //pointer of base class    
 B b;     //object of derived class    
 a = &b;    
 a->display();   //Late Binding occurs    
}

Pure Virtual Function

A pure virtual function is a member function of base class whose only declaration is provided in base class and should be defined in derived class otherwise derived class also becomes abstract.

  • Base class containing pure virtual function becomes abstract.
  • When the function has no definition, such function is known as “do-nothing” function.
  • The “do-nothing” function is known as a pure virtual function. A pure virtual function is a function declared in the base class that has no definition relative to the base class.
  • The main objective of the base class is to provide the traits to the derived classes and to create the base pointer used for achieving the runtime polymorphism.

Pure virtual function can be defined as:

virtual void display() = 0;   

Let’s see a simple example:

#include <iostream>  
using namespace std;  
class Base  
{  
    public:  
    virtual void show() = 0;  
};  
class Derived : public Base  
{  
    public:  
    void show()  
    {  
        std::cout << "Derived class is derived from the base class." << std::endl;  
    }  
};  
int main()  
{  
    Base *bptr;  
    //Base b;  
    Derived d;  
    bptr = &d;  
    bptr->show();  
    return 0;  
}  

Output

Derived class is derived from the base class.

In the above example, the base class contains the pure virtual function. Therefore, the base class is an abstract base class. We cannot create the object of the base class.

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.