Virtual functions in C++ are one of the most powerful features of Object-Oriented Programming (OOP). They provide runtime polymorphism, dynamic binding, method overriding, and a flexible way to design reusable and scalable code.
In this complete guide, you will learn everything about:
- Virtual function in C++
- Pure virtual function in C++
- Definition of virtual function in C++
- Syntax of virtual function in C++
- Uses of virtual function in C++
- Virtual function example in C++
- Virtual function overriding in C++
- Abstract class and pure virtual function in C++
- Difference between virtual function and pure virtual function in C++
- How virtual functions work internally in C++ (vtable mechanism)
- Runtime polymorphism in C++ using virtual function
This article includes dozens of long-tail SEO keywords, grammar-correct usage, optimized headings, and deeply detailed explanations.
Let’s begin.
⭐ 1. What is a Virtual Function in C++? (Definition)
A virtual function in C++ is a member function of a class that is declared using the keyword virtual and is redefined (overridden) in a derived class. The call to a virtual function is resolved at runtime instead of compile time, enabling dynamic polymorphism.
🔹 Formal Definition of Virtual Function in C++
A virtual function is a function in a base class that you expect to override in derived classes. When you use a base class pointer to refer to a derived object, the call to the virtual function will invoke the derived class implementation.
⭐ 2. Need and Purpose of Virtual Functions in C++
Why do we need virtual functions in C++?
Here are the main reasons:
- To achieve runtime polymorphism
- To support method overriding
- For dynamic binding through virtual functions in C++
- To allow base class pointers to call derived class methods
- To design flexible and extensible OOP architectures
- To create abstract classes using pure virtual functions
Thus, the purpose of virtual function in C++ is to ensure that the correct function is called for an object, regardless of the type of reference used.
⭐ 3. Syntax of Virtual Function in C++
class Base {
public:
virtual returnType functionName(parameters) {
// function body
}
};
This is the virtual function syntax in C++ that every programmer must know.
⭐ 4. Simple Example of Virtual Function in C++
Here is a simple example of virtual function in C++:
#include <iostream>
using namespace std;
class Base {
public:
virtual void show() { // virtual function
cout << "Base class show()" << endl;
}
};
class Derived : public Base {
public:
void show() override { // overriding
cout << "Derived class show()" << endl;
}
};
int main() {
Base* ptr;
Derived d;
ptr = &d;
ptr->show(); // calls Derived class function
return 0;
}
Output:
Derived class show()
This demonstrates virtual function overriding in C++.
⭐ 5. How Virtual Functions Work Internally in C++ (VTable & VPtr)
To support runtime polymorphism, C++ uses:
- VTable (Virtual Table)
- VPtr (Virtual Pointer)
🔹 Internal Working:
- Every class with virtual functions gets a VTable.
- Every object of such a class has a hidden pointer called VPtr.
- VPtr stores the address of the class’s VTable.
- At runtime, when a virtual function is called, VPtr is checked.
- The function mapping is resolved dynamically through the VTable.
This is the backbone of runtime polymorphism in C++ using virtual function.
⭐ 6. Rules for Virtual Functions in C++
These are the most important rules of virtual function in C++:
- They must be declared using the keyword virtual.
- They can be overridden in derived classes.
- A virtual function cannot be static.
- It can be a private function (yes, virtual functions can be private in C++).
- Constructors cannot be virtual, but destructors can be virtual.
- Declaring a function virtual does not force overriding.
- Virtual functions support runtime polymorphism.
⭐ 7. Program Using Virtual Function in C++
#include <iostream>
using namespace std;
class Shape {
public:
virtual void draw() { // virtual function
cout << "Drawing a Shape" << endl;
}
};
class Circle : public Shape {
public:
void draw() override {
cout << "Drawing a Circle" << endl;
}
};
int main() {
Shape *s;
Circle c;
s = &c;
s->draw(); // derived version executes
return 0;
}
This is a classic program for virtual function in C++.
⭐ 8. What Is a Pure Virtual Function in C++?
A pure virtual function in C++ is a function with no body in the base class. It forces derived classes to override the function.
🔹 Pure Virtual Function Syntax
virtual returnType functionName() = 0;
A class containing at least one pure virtual function becomes an abstract class.
⭐ 9. Pure Virtual Function in C++ Example
#include <iostream>
using namespace std;
class Animal {
public:
virtual void sound() = 0; // pure virtual function
};
class Dog : public Animal {
public:
void sound() override {
cout << "Dog barks" << endl;
}
};
int main() {
Animal* a = new Dog();
a->sound();
return 0;
}
This is the pure virtual function in C++ example and also demonstrates abstract class and pure virtual function in C++.
⭐ 10. Difference Between Virtual Function and Pure Virtual Function in C++
| Feature | Virtual Function | Pure Virtual Function |
|---|---|---|
| Definition | A normal virtual method with a body | A virtual function with no body |
| Syntax | virtual void fun() | virtual void fun() = 0 |
| Overriding | Optional | Mandatory |
| Abstract class requirement | No | Yes |
| Object creation | Allowed | Cannot create object of class |
This table satisfies the keyword:
difference between virtual function and pure virtual function in c++
⭐ 11. Advantages of Virtual Functions in C++
- Provide runtime polymorphism
- Allow flexible and reusable code
- Help in achieving dynamic binding
- Support overriding
- Used in designing abstract classes
- Enable polymorphism in frameworks
⭐ 12. Types of Virtual Functions in C++
While C++ officially defines only virtual functions, in practice they are categorized as:
- Simple virtual functions
- Pure virtual functions
- Virtual destructors
- Virtual function overriding
⭐ 13. Virtual Destructor in C++ Example
class Base {
public:
virtual ~Base() {
cout << "Base Destructor" << endl;
}
};
class Derived : public Base {
public:
~Derived() {
cout << "Derived Destructor" << endl;
}
};
Virtual destructors prevent memory leaks when deleting objects via base class pointers.
⭐ 14. Applications of Virtual Function in C++
Virtual functions are widely used in:
- GUI frameworks
- Game development
- Compiler design
- Database engines
- Simulation systems
- Plugin architectures
- API design
- Inheritance-based systems
⭐ 15. Polymorphism and Virtual Function in C++
Virtual functions implement runtime polymorphism, allowing behavior to change depending on the object type.
Example:
Base* b = new Derived();
b->show(); // polymorphism
⭐ 16. Pointers and Virtual Functions in C++
Virtual functions almost always work with pointers or references:
Base* ptr = &derivedObject;
ptr->virtualFunction();
This leads to dynamic binding.
⭐ 17. Program on Virtual Function in C++ (Full Demo)
#include <iostream>
using namespace std;
class Vehicle {
public:
virtual void start() {
cout << "Vehicle starting..." << endl;
}
};
class Car : public Vehicle {
public:
void start() override {
cout << "Car starting..." << endl;
}
};
class Bike : public Vehicle {
public:
void start() override {
cout << "Bike starting..." << endl;
}
};
int main() {
Vehicle* v;
Car c;
Bike b;
v = &c; v->start();
v = &b; v->start();
return 0;
}
⭐ 18. MCQ on Virtual Function in C++ (For Students)
- Which keyword is used to declare virtual functions?
- a) abstract
- b) static
- c) virtual ✔
- d) override
- A class with at least one pure virtual function is called:
- a) sealed class
- b) final class
- c) interface
- d) abstract class ✔
- Virtual function supports:
- a) Compile-time polymorphism
- b) Runtime polymorphism ✔
- c) Both
- d) None
⭐ 19. Can Static Functions Be Virtual in C++?
No.
Static functions cannot be virtual because virtual functions require a class object and vptr, while static functions do not belong to any object.
⭐ 20. Can Virtual Functions Be Private in C++?
Yes.
Virtual functions can be private. They can still be overridden in derived classes.
⭐ 21. Real-Life Analogy of Virtual Functions
Imagine a remote control (base class pointer) operating different TVs (derived classes).
Even though the remote is generic, it performs the correct function depending on which TV object it points to.
⭐ 22. Final Summary
In this article, we covered:
- Definition of virtual function in C++
- Syntax for virtual function in C++
- Need for virtual functions in C++
- Uses and applications
- Pure virtual function and abstract class
- Runtime polymorphism
- Multiple C++ programs
- Rules, advantages, examples, and comparisons