Please have a look at the following articles i.e. polymorphism and inheritance in C++ which is really important to know before going through the virtual functions.

As we know, through inheritance one class (derived class) can inherit the features of another class (base class). Calling a method through the object created with the derived class type that is pointing to the same derived class instance then always the derived class methods get called.

This won’t be the case when we use inheritance along with polymorphism i.e. when we refer to a derived class object using a base class pointer or reference type.

See the following example for more information on this.

#include <iostream>

using namespace std;

class Base{
    public:
    void fun1(){
        cout << "This is fun1 in base" << endl;
    }
};

class Derived : public Base{
    public:
    void fun1(){
    	cout << "This is fun1 in derived" << endl;
    }
};

int main(){

    // Base class instance with the Base class type 
    Base *b = new Base();
    b->fun1();
    
    // Derived class instance with the Derived class type
    Derived *d = new Derived();
    d->fun1();
    
    // Derived class instance with Base class type 
    Base *c = d;
    c->fun1();
    
    return 0;
}

Output:

This is fun1 in base
This is fun1 in derived
This is fun1 in base

Explanation

In the above example, b is an object to the Base class type and hence base class method gets called. Similarly, d is an object of the derived class and hence derived class method gets called.

Object c points to the derived class instance but its type is Base class. Although it is legal to use this because the Derived class is inherited from the Base class and when we call fun1() with object c, the Derived class method won’t get called instead Base class method will get called. But we expect the derived class method to execute as the object it is pointing to is a derived class one.

To avoid this, we need to specify the fun1() in the Base class as a virtual function so that compiler can understand and call the method in the derived class.

Virtual functions allow us to redefine/extend the functionality of the base class in the derived class. such that when we refer to a derived class object using a base class pointer or reference type then always derived class method will get executed.

In C++ 11 a new keyword called override is introduced such that we can mark the functions in the derived class with override if we know that there is a virtual function present in the base class. This is not mandatory to use but improves the code’s readability.

#include <iostream>

using namespace std;

class Base{
    public:
    virtual void fun1(){
        cout << "This is fun1 in base" << endl;
    }
};

class Derived : public Base{
    public:
    void fun1() override{
    	cout << "This is fun1 in derived" << endl;
    }
};

int main(){

    Base *b = new Base();
    b->fun1();
    
    Derived *d = new Derived();
    d->fun1();
    
    Base *c = d;
    c->fun1();
    
    return 0;
}

Output:

This is fun1 in base
This is fun1 in derived
This is fun1 in derived

Important points to remember

  1. Static virtual functions are not allowed because virtual functions are always linked to a particular instance of a class. Whereas static functions are linked to the entire class. Pointers are needed to invoke virtual functions in C++ but in C++ we cannot create a pointer to Class.
  2. The virtual function can be a friend function of another class.
  3. To achieve runtime polymorphism, the prototype of the virtual function in the base class and the function in the derived class should be the same.
  4. It is not necessary to override virtual functions in the derived class. If the compiler doesn’t find the overridden function in the derived class then it will call the Base class method.

Categorized in:

Tagged in: