DEV Community

Cover image for C++ Virtual Function: A Complete Guide
Rachit Joshi
Rachit Joshi

Posted on

C++ Virtual Function: A Complete Guide

Introduction

C++ is a powerful object-oriented programming language that provides several advanced features, and one of the most important among them is the virtual function. Virtual functions play a crucial role in implementing runtime polymorphism, allowing programs to behave dynamically according to the object type.

Virtual functions enable developers to write flexible, reusable, and scalable code. They are widely used in game development, operating systems, software applications, and enterprise-level projects.

What Is a Virtual Function?

A virtual function is a member function of a base class that can be redefined in a derived class. It is declared using the virtual keyword.

The primary purpose of a virtual function is to achieve runtime polymorphism, allowing the correct function to be called according to the type of object being used.

Syntax
class Base {
public:
virtual void display() {
cout << "Base class function";
}
};
Why Are Virtual Functions Important?

Without virtual functions, C++ performs early binding, meaning the function call is determined during compilation.

With virtual functions, the program uses late binding (dynamic binding), and the decision is made during runtime.

Virtual functions provide several advantages:

Better code reusability
Runtime polymorphism
Greater flexibility
Easier maintenance
Improved scalability
Example of a Virtual Function

include

using namespace std;

class Animal {
public:
virtual void sound() {
cout << "Animal makes a sound" << endl;
}
};

class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks" << endl;
}
};

int main() {
Animal* obj;
Dog d;

obj = &d;
obj->sound();

return 0;
Enter fullscreen mode Exit fullscreen mode

}
Output
Dog barks

Because the function is declared as virtual, the program executes the derived class function instead of the base class function.

How Virtual Functions Work

The C++ compiler creates a special structure called a virtual table (vtable). Every object that contains virtual functions maintains a pointer to this table.

The virtual table stores addresses of virtual functions, allowing the program to select the appropriate function during execution.

The process follows these steps:

The compiler creates a virtual table.
The object stores a pointer to the table.
The appropriate function address is selected.
The function executes at runtime.
Rules for Using Virtual Functions

Developers should remember the following rules:

Virtual functions must belong to a class.
Static member functions cannot be virtual.
Constructors cannot be declared as virtual functions.
Destructors can be virtual.
Derived classes may override virtual functions.
Pure Virtual Functions

A pure virtual function contains no implementation in the base class and is assigned the value 0.

class Shape {
public:
virtual void draw() = 0;
};

Classes containing pure virtual functions are called abstract classes.

Virtual Function Versus Normal Function
Feature Virtual Function Normal Function
Binding Runtime binding Compile-time binding
Speed Slightly slower Faster
Flexibility High Limited
Inheritance support Yes Yes
Polymorphism support Yes No
Advantages of Virtual Functions
Supports dynamic polymorphism
Makes programs more flexible
Improves code reusability
Simplifies large projects
Reduces code duplication
Limitations of Virtual Functions
Requires additional memory.
Slightly increases execution time.
Increases program complexity.
Makes debugging more difficult.
Applications of Virtual Functions

Virtual functions are widely used in the following areas:

Operating systems
Video games
Database management systems
Artificial intelligence applications
Graphical user interface (GUI) development
Software design patterns
Conclusion

Virtual functions are among the most powerful features of C++. They make runtime polymorphism possible and allow programs to behave intelligently according to object type. By understanding virtual functions, developers can build more efficient, scalable, and maintainable applications.

If you want to strengthen your C++ knowledge further, tutorials available on TPointTech can help you understand inheritance, polymorphism, constructors, destructors, and other advanced concepts in greater detail.

Top comments (0)