Open In App

Virtual Function in C++

Last Updated : 01 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A virtual function (also known as virtual methods) is a member function that is declared within a base class and is re-defined (overridden) by a derived class. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class’s version of the method.

  • Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for the function call.
  • They are mainly used to achieve Runtime polymorphism.
  • Functions are declared with a virtual keyword in a base class.
  • The resolving of a function call is done at runtime.

Syntax

We can create a virtual function inside class using virtual keyword:

C++
class Base{
public:
    virtual return_type 
            functionName(){
        // Function body
    }
}

class Derived : public Base{
public:
    
    // Overriden virtual 
    // function of the base
    // class
    return_type functionName(){
        // Function body
    }
}

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

//Driver Code Ends }

class Base {
public:

    // Virtual function
    virtual void display() {
        cout << "Base class display" << endl;
    }
};

class Derived : public Base {
public:

    // Overriding the virtual 
    // function in the derived class
    void display() {
        cout << "Derived class display" << endl;
    }
};

int main() {
    Base* basePtr;
    Derived derivedObj;
    
    // Base class pointer pointing 
    // to derived class object
    basePtr = &derivedObj;
    
    // Calling the virtual function
    basePtr->display(); 

//Driver Code Starts{
    return 0;
}

//Driver Code Ends }

Output
Derived class display

Note: It is a recommended way to use override identifier to avoid mistakes while redefine virtual function inside the derived class.

Pure Virtual Function

A virtual function is called a pure virtual function if it does not have any implementation and is assigned = 0. A class that contains a pure virtual function is called an abstract class.

Example:

C++
//Driver Code Starts{
#include <iostream>
using namespace std;

//Driver Code Ends }

class Base {
public:

    // Pure virtual function
    virtual void display() = 0;
};

class Derived : public Base {
public:

    void display() override {
        cout << "Derived class display" << endl;
    }
};

int main() {
    Base* basePtr;
    Derived derivedObj;
    basePtr = &derivedObj;
    basePtr->display(); 

//Driver Code Starts{
    return 0;
}

//Driver Code Ends }

Output
Derived class display

Early Binding and Late Binding

In layman’s terms, when a function is called in the code, binding decides which function gets executed based on the context such as the type of object or the function signature. Binding happens at two levels:

  • Early Binding: It happens when a function call is resolved during the program’s compilation. This makes it faster because everything is decided early
  • Late Binding: It happens with virtual functions where the exact function to call is decided at runtime, depending on the actual object type. This is slower because the program has to figure it out while running.

Example:

C++
#include <iostream>
using namespace std;

class base {
public:
    virtual void print() {
        cout << "print base "
        "class\n";
    }

    void show() {
        cout << "show base class\n";
    }
};

class derived : public base {
public:
    void print() {
        cout << "print derived class\n";
    }

    void show() {
        cout << "show derived class\n";
    }
};

int main() {
    base* bptr;
    derived d;
    bptr = &d;

    // Virtual function, 
    // binded at runtime
    bptr->print();

    // Non-virtual function, 
    // binded at compile time
    bptr->show();
    return 0;
}

Output
print derived class
show base class

Explanation: In the above code, the print() function is declared with the virtual keyword so it will be bound at runtime and show() is non-virtual so it will be bound during compile time.

Note: If we have created a virtual function in the base class and it is being overridden in the derived class then we don’t need a virtual keyword in the derived class, functions are automatically considered virtual functions in the derived class.

Working of Virtual Functions (concept of VTABLE and VPTR)

As discussed here, if a class contains a virtual function then the compiler itself does two things.

  1. If an object of that class is created, then a virtual pointer (VPTR) is inserted as a data member of the class to point to the VTABLE of that class. For each new object created, a new virtual pointer is inserted as a data member of that class.
  2. Irrespective of whether the object is created or not, the class contains as a member a static array of function pointers called VTABLE. Cells of this table store the address of each virtual function contained in that class.

Consider the example below:

C++
#include <iostream>
using namespace std;

class base {
public:
    void fun_1() { cout << "base-1\n"; }
    virtual void fun_2() { cout << "base-2\n"; }
    virtual void fun_3() { cout << "base-3\n"; }
    virtual void fun_4() { cout << "base-4\n"; }
};

class derived : public base {
public:
    void fun_1() { cout << "derived-1\n"; }
    void fun_2() { cout << "derived-2\n"; }
    void fun_4(int x) { cout << "derived-4\n"; }
};

int main() {
    
    base* p;
    derived obj1;
    p = &obj1;

    // Early binding because fun1() is non-virtual
    // in base
    p->fun_1();

    // Late binding (RTP)
    p->fun_2();

    // Late binding (RTP)
    p->fun_3();

    // Late binding (RTP)
    p->fun_4();

    // Early binding but this function call is
    // illegal (produces error) because pointer
    // is of base type and function is of
    // derived class
    // p->fun_4(5);

    return 0;
}

Output
base-1
derived-2
base-3
base-4

 Explanation: Initially, we create a pointer of the type base class and initialize it with the address of the derived class object. When we create an object of the derived class, the compiler creates a pointer as a data member of the class containing the address of VTABLE of the derived class.

A similar concept of Late and Early Binding is used as in the above example. For the fun_1() function call, the base class version of the function is called, fun_2() is overridden in the derived class so the derived class version is called, fun_3() is not overridden in the derived class and is a virtual function so the base class version is called, similarly fun_4() is not overridden so base class version is called.

Note: fun_4(int) in the derived class is different from the virtual function fun_4() in the base class as prototypes of both functions are different.

virtual pointer and virtual table

Rules for Virtual Functions

The rules for the virtual functions in C++ are as follows:

  1. Virtual functions cannot be static.
  2. A virtual function can be a friend function of another class.
  3. Virtual functions should be accessed using a pointer or reference of base class type to achieve runtime polymorphism.
  4. The prototype of virtual functions should be the same in the base as well as the derived class.
  5. They are always defined in the base class and overridden in a derived class. It is not mandatory for the derived class to override (or re-define the virtual function), in that case, the base class version of the function is used.
  6. A class may have a virtual destructor, but it cannot have a virtual constructor.

Limitations of Virtual Functions

  • Slower: The function call takes slightly longer due to the virtual mechanism and makes it more difficult for the compiler to optimize because it does not know exactly which function is going to be called at compile time.
  • Difficult to Debug: In a complex system, virtual functions can make it a little more difficult to figure out where a function is being called from.


Next Article
Practice Tags :

Similar Reads