C++ is one of the most popular programming languages worldwide. It is an extended version of the C programming language and partially follows the Object-Oriented Programming (OOPs) concept. Like other OOPs languages, even C++ allows fundamentals such as encapsulation, inheritance, polymorphism, and so on. In this article, you will focus on virtual functions in C++ that are used to achieve polymorphism.

What is a Virtual Function in C++?

A virtual function in C++ is a base class member function that you can redefine in a derived class to achieve polymorphism. You can declare the function in the base class using the virtual keyword. Once you declare the function in the base class, you can use a pointer or reference to call the virtual class and execute its virtual version in the derived class. Thus, it asks the compiler to determine the object’s type during run-time and create a function bind (late binding or dynamic linkage).

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

A virtual function in C++ helps ensure you call the correct function via a reference or pointer. The C++ programming language allows you only to use a single pointer to refer to all the derived class objects. Since the pointer refers to all the derived objects, calling it will consistently execute the function in the base class. You can overcome this challenge with a virtual function in C++ as it helps execute the virtual version of the derived class, which is done at the run-time.

What Are the Rules of Virtual Function in C++?

There are a few rules you need to follow to create a virtual function in C++. These rules are:

  • The functions cannot be static
  • You derive them using the “virtual” keyword
  • Virtual functions in C++ needs to be a member of some other class (base class)
  • They can be a friend function of another class
  • The prototype of these functions should be the same for both the base and derived class
  • Virtual functions are accessible using object pointers
  • Redefining the virtual function in the derived class is optional, but it needs to be defined in the base class
  • The function call resolving is done at run-time
  • You can create a virtual destructor but not a constructor

Using a Virtual Function in C++

Now that you know the fundamentals of the virtual function in C++, it’s time to head on to an example of using it. In the below example, you will create a base class and a derived class. In both classes, you will create two functions: Output and Display. To see the difference between the virtual function and a regular function, you will only declare the Output function of the base class as virtual, and keep the display function as it is.

#include <iostream>

using namespace std;

class Base{

public:

virtual void Output(){

cout << "Output Base class" << endl;

}

void Display(){

cout << "Display Base class" << endl;

}

};

class Derived : public Base{

public:

void Output(){

cout << "Output Derived class" << endl;

}

void Display()

{

cout << "Display Derived class" << endl;

}

};

int main(){

Base* bpointr;

Derived dpointr;

bpointr = &dpointr;

// virtual function binding

bpointr->Output();

// Non-virtual function binding

bpointr->Display();

}

Output:

Virtual_Function_In_C_Pus_Plus_1

As you can see in the output, the output function gave the result “Output Derived class,” and the Derived function gave the result “Display Base class”. That’s because the pointer of the base class referred to the virtual output function of the derived class, and the non-virtual display function of the base class.

Compile-Time VS Runtime Behavior of Virtual Functions in C++

A virtual function in C++ exhibits two different types of behavior: compile-time and runtime. 

Compile-time behavior

Runtime behavior

Alternative name

Early binding

Late binding

How is it achieved

The type of pointer

Depending on the location where the pointer is pointing

Consider the example below to understand the behavior of virtual functions better.

#include <iostream>

using namespace std;

class base {

public:

virtual void show(){

cout << "show base class" << endl;

}

void print(){

cout << "print base class" << endl;

}

};

class derived : public base {

public:

void show(){

cout << "show derived class" << endl;

}

void print(){

cout << "print derived class" << endl;

}

};

int main(){

base* bpointr;

derived dev;

bpointr = &dev;

// runtime binding

bpointr->show();

// compile time binding

bpointr->print();

}

Output:

Virtual_Function_In_C_Pus_Plus_2

Learn the Ins & Outs of Software Development

Caltech Coding BootcampExplore Program
Learn the Ins & Outs of Software Development

Since you have declared the show function as virtual in the above code, it will be bound at runtime. Hence, the output shows the derived class as the pointer that was pointing to that location. On the other hand, the print was a non-virtual function; it exhibited compile-time behavior, and the output was print base class.

What is a Pure Virtual Function in C++?

A pure virtual function in C++, also known as the do-nothing function, is a virtual function that does not perform any task. It is only used as a placeholder and does not contain any function definition (do-nothing function). It is declared in an abstract base class. These types of classes cannot declare any objects of their own. You derive a pure virtual function in C++ using the following syntax:

Virtual void class_name() = 0;

Example of Pure Virtual Functions in C++

#include <iostream>

using namespace std;

class Base{

    public:

    virtual void Output() = 0;  

};

class Derived : public Base{

    public:

    void Output()  

    {

        std::cout << "Class derived from the Base class." << std::endl;

    }

};

int main(){

    Base *bpointr;

    Derived dpointr;

    bpointr = &dpointr;

    bpointr->Output();  

    return 0;

}

Output:

Virtual_Function_In_C_Pus_Plus_3

Conclusion

In this article, you have explored virtual functions in C++ along with simple examples. You have also seen what a pure virtual function in C++ is. You can now create virtual functions of your own and use them to execute derived versions of base class functions and achieve run-time polymorphism through it. 

With virtual functions, you can also overcome the problem of a pointer always pointing towards the base class. If you want to learn more about such fundamental concepts of C++, you can refer to our C++ Tutorial for Beginners. The tutorials are designed to help you grasp various topics and concepts of C++ programming and help you excel in C++ development.

Have any questions for us? Leave them in the comments section of this article. Our experts will help you with the same, ASAP!