Home >>C++ Tutorial >C++ Virtual Function

C++ Virtual Function

Virtual Function in C++

  • The member function that is present in the base class that is redefined by the user in a deprived class is known as a virtual function in C++. This function is generally declared by the use of the virtual keyword.
  • This function generally used to order the compiler to perform the dynamic linkage or the late binding on the function.
  • In order to refer to all the objects of the different classes, the use of the single pointer is meant to be necessary. Hence, the pointer that is created by the user to the base class will refer to all the derived objects. But the base class function is always executed whenever the base class pointer contains the address of the derived class object.
  • The keyword that generally precedes the normal declaration of the function is 'virtual'.
  • C++ programming decides that which function has to be invoked at the runtime and that is based on the type of the object that has been pointed by the base class pointer.

Late binding or Dynamic linkage

The process in which the function call is resolved during runtime is known as the late binding. Hence the type of object is determined by the compiler at the runtime and the function call is binded.

Rules of Virtual Function

Here are the rules of the virtual function in C++:

  • Virtual functions should be the members of some class.
  • Virtual functions are not allowed to be the static members.
  • These functions can be accessed through object pointers.
  • These functions might be a friend of another class.
  • A virtual function in C++ should be defined in the base class despite of the fact that it is not even used.
  • Virtual function’s prototypes are of the base class and the derived classes should be identical. C++ programming would consider the two functions with the same name but different prototypes, as the overloaded functions.
  • Programmers cannot have a virtual constructor instead they can have a virtual destructor.

Here is an example

#include <iostream>  
using namespace std;  
class Demo  
{  
    int a=10;  
    public:  
    void show()  
    {  
        std::cout << "Disp the value of a = " <<a<<std::endl;  
    }  
};  
class Demo1: public Demo  
{  
    int b = 15;  
    public:  
    void show()  
    {  
        std::cout <<"Disp the Value of b = "<<b<<std::endl;  
    }  
};  
int main()  
{  
    Demo *obj;  
    Demo1 obj1;  
    obj = &obj1;  
    obj->show();  
    return 0;  
}  
Output : Disp the value of a = 10

Pure Virtual Function

  • In order to perform any task the virtual function is not used instead, it only behaves as a placeholder.
  • Do-nothing function are the functions that don’t have a definition.
  • The "do-nothing" function is also called as a pure virtual function. Function that is declared in the base class and has no definition that is related to the base class is known as a pure virtual function.
  • A class that consists of the pure virtual function is forbidden from getting used to declare the objects of its own, and are called as abstract base classes.
  • In order to provide the traits to the derived classes and as a second task to create the base pointer that is used for achieving the runtime polymorphism, the base class is used in C++.

Here is an example

#include   
using namespace std;  
class Base  
{  
    public:  
    virtual void display() = 0;  
};  
class Child : public Base  
{  
    public:  
    void display()  
    {  
        std::cout << "Here Child class is derived from the Parent class." << std::endl;  
    }  
};  
int main()  
{  
    Base *bptr;  
    //Base obj;  
    Child obj1;  
    bptr = &obj1;  
    bptr->display();  
    return 0;  
}  
Output :Here Child class is derived from the Parent class.

No Sidebar ads