Virtual Functions in C++
In this lesson: How virtual functions work in C++, override keyword, vtable and virtual destructors — with output. In Hindi & English.
What is a virtual function?
A virtual function is a member function marked virtual in a base class, designed to be overridden by derived classes. Its point is dynamic dispatch: when you call it through a base-class pointer or reference, C++ runs the version belonging to the actual object, not the pointer's type.
The problem it solves
Without virtual, a call through a base pointer always runs the base version, ignoring the override. Compare:
class Base { public: virtual void hi() { cout << "Base\n"; } };
class Derived : public Base { public: void hi() override { cout << "Derived\n"; } };
int main() {
Base *p = new Derived();
p->hi(); // "Derived" — thanks to virtual
delete p;
}Derived
Remove virtual and this would print Base instead.
The override keyword
Add override to a derived function you intend as an override. If it does not actually match a base virtual, the compiler stops you — catching typos in the signature:
class Shape { public: virtual double area() { return 0; } };
class Circle : public Shape {
double r;
public:
Circle(double x) : r(x) {}
double area() override { return 3.14159 * r * r; } // safe
};How it works: the vtable
Most compilers implement this with a vtable — a per-class table of function pointers. Each polymorphic object holds a hidden pointer to its class's vtable, and a virtual call looks up the right function there at run time. This is why virtual calls have a tiny overhead over ordinary calls.
Virtual destructors
If you delete a derived object through a base pointer, the destructor must be virtual — otherwise only the base part is destroyed and the derived resources leak:
class Base {
public:
virtual ~Base() { cout << "~Base\n"; } // virtual destructor
};
class Derived : public Base {
public:
~Derived() { cout << "~Derived\n"; }
};
// delete (Base*)new Derived(); → ~Derived then ~BaseAny class with a virtual function should also have a virtual destructor.
Overriding vs hiding
True overriding needs a matching signature on a virtual base function. If the base function is not virtual, or the signatures differ, the derived function merely hides the base one — no dynamic dispatch. The override keyword guards against this by mistake.
Common mistakes
- Forgetting
virtual, so overrides are ignored through base pointers. - Omitting a virtual destructor in a polymorphic base (resource leak).
- A slightly different signature causing hiding instead of overriding (use
override). - Expecting dynamic dispatch when calling on an object rather than a pointer/reference.
For a compact syntax reference and quick example, see the virtual functions syntax page. For the wider idea, see polymorphism.
Build a Shape base with a virtual area() and a virtual destructor. Derive Circle and Rectangle. Store them via Shape* pointers and print each area through the base pointer.
Summary
- A virtual function enables run-time dispatch to the right override.
- Call it through a base pointer/reference for polymorphism.
overridemakes the compiler verify your override.- Compilers use a vtable to resolve virtual calls at run time.
- Always give a polymorphic base class a virtual destructor.
Frequently Asked Questions
What is a virtual function in C++?
virtual keyword in a base class, meant to be overridden in derived classes. When called through a base-class pointer or reference, the version matching the actual object is chosen at run time, enabling run-time polymorphism.What does the override keyword do in C++?
override keyword tells the compiler that a function is meant to override a virtual function from the base class. If the signature does not actually match a base virtual function, the compiler reports an error, which catches subtle mistakes early.