C++ Virtual Functions

Virtual Functions are a important part of the Object Oriented Programming Concept of Polymorphism in C++. Virtual Functions go hand in hand with other OOP concepts like Inheritance, so be sure to read up on those if you haven’t already.


Types of Virtual Functions

There are two main types of Virtual Functions, listed below:

  1. Partial Virtual Functions
  2. Pure Virtual Functions

Partial Virtual Functions

Partial Virtual Functions are those functions which are declared as Virtual, but still have a function body. When creating Child class for a Class with virtual function(s), there are two choices you can make.

You can either simply inherit the parent from child and use the parent definition of the function which is used by default. Otherwise, you may choose to redeclare the virtual function (overriding) in the child class, and the new definition will then be used. This is useful if you want some Child classes to use the Parent definition, and some to have their own custom version.

You need to declare it just like you would do for a regular function, while adding the virtual keyword in the function signature.

virtual void display() {
    cout << "This is a Virtual Function" << endl;
}

Partial Virtual Functions Example

The below Class A, has a virtual function called display(), which prints out a simple message.

class A {
public:
    virtual void display() {
        cout << "This is the Display from Class A" << endl;
    }
};

We create two Child Classes, Class B and Class C which both inherit from Class A.

Class B inherits the virtual function from Class A without making any modifications to it. Class C on the other hand re-declares or “over-rides” the virtual display() function with it’s own.

class B : public A {
};

class C : public A {
public:
    virtual void display() {
        cout << "This is the Display from Class C" << endl;
    }
};

Now when the display methods are called on the objects of Class B and C, there will be a different output.

int main() {
    A objectA;
    B objectB;
    C objectC;

    objectA.display();
    objectB.display();
    objectC.display();
}

The output of the above code:

This is the Display from Class A
This is the Display from Class A
This is the Display from Class C

Class B uses the virtual function from Class A, whereas Class C has it’s own version of it. This is the concept of Polymorphism in C++ with Virtual functions.


Pure Virtual Functions

In C++, Pure Virtual Functions are used primarily to declare classes as Abstract. An Abstract class is a special type of Class which cannot have any objects. The way we do this is to declare a virtual function without any definition, or in simpler terms, there should be no function body.

class A {
public:
    virtual void display() = 0;
};

The key idea here, is that if you define a function without a body, then C++ will not allow an object of that Class to be created. This however means that the Children of the Class need to have the virtual function defined, otherwise they will also be treated as Abstract Classes.


Pure Virtual Functions Example

Here is a complete example using pure virtual functions in C++. There isn’t much to see, but if you try running this code and modifying it, you will realize that you cannot make Objects of Class A, or any Child Class of Class A that doesn’t define the pure virtual function.

#include <iostream>
using namespace std;

class A {
public:
    virtual void display() = 0;
};

class B : public A {
public:
    virtual void display() {
        cout << "This is the Defined Display function" << endl;
    }
};

int main() {
    B objectB;
    objectB.display();
}

The output:

This is the Defined Display function

Further Reading:

There is a little more to virtual functions than what we have discussed here. If you know about Function Overriding, you will notice that the end result in Overring and Virtual functions is very similar, although the concept is different.

They aren’t identical in several situations however. Virtual functions have the concept of Upcasting and Downcasting which makes them very useful in certain situations. We have covered this concept in a separate tutorial on C++ Upcasting and Downcasting.


This marks the end of the C++ Virtual Functions and Classes Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments