Virtual Functions
Written and reviewed by Gagan Bhardwaj · Senior IT Faculty · 15+ years’ experience
Quick recap
A virtual function lets a call through a base pointer run the derived version at run time. This page is a compact syntax reference; for how it works internally (the vtable), virtual destructors, and the full concept, see the main virtual functions lesson and polymorphism.
Basic syntax
class Base {
public:
virtual void draw(); // 'virtual' in the base
};
class Derived : public Base {
public:
void draw() override; // 'override' in the derived
};Base-pointer example
#include <iostream>
using namespace std;
class Animal { public: virtual void speak() { cout << "...\n"; } };
class Dog : public Animal { public: void speak() override { cout << "Woof\n"; } };
class Cat : public Animal { public: void speak() override { cout << "Meow\n"; } };
int main() {
Animal *arr[2] = { new Dog(), new Cat() };
for (Animal *a : arr) a->speak(); // run-time dispatch
}Woof
Meow
With vs without virtual
With virtual | Without virtual | |
|---|---|---|
basePtr->f() runs | Derived's version | Base's version |
| Dispatch | Run time (dynamic) | Compile time (static) |
Pure virtual syntax
Add = 0 to make a function pure virtual — no body, and derived classes must implement it. This makes the base an abstract class:
class Shape {
public:
virtual double area() = 0; // pure virtual
};See abstract classes for the full story.
Keyword cheat sheet
| Keyword | Where | Meaning |
|---|---|---|
virtual | Base function | Can be overridden; enables dynamic dispatch |
override | Derived function | Verifies it overrides a base virtual |
= 0 | Base function | Pure virtual; class becomes abstract |
final | Function/class | Prevents further overriding/derivation |
Common mistakes
- Writing
= 0but still giving the function a body without meaning to. - Expecting dispatch when calling on an object instead of a pointer/reference.
- A mismatched signature silently hiding instead of overriding (use
override). - Forgetting a virtual destructor in the base (covered in the main lesson).
Write a base Employee with a virtual salary(), and derived Manager and Intern. Put pointers in an array and print each salary through the base pointer.
Summary
virtualgoes on the base function;overrideon the derived.- Call through a base pointer/reference for run-time dispatch.
= 0makes a pure virtual and an abstract class.- Keyword cheat sheet: virtual, override, =0, final.
- For internals and design, see the main virtual functions lesson.
त्वरित सार
Virtual function base pointer के ज़रिए call को run time पर derived version चलाने देता है। यह पृष्ठ एक संक्षिप्त syntax reference है; यह आंतरिक रूप से कैसे काम करता है (vtable), virtual destructors, और पूरे concept के लिए, मुख्य virtual functions lesson और polymorphism देखें।
बुनियादी syntax
class Base {
public:
virtual void draw(); // base me 'virtual'
};
class Derived : public Base {
public:
void draw() override; // derived me 'override'
};Base-pointer उदाहरण
#include <iostream>
using namespace std;
class Animal { public: virtual void speak() { cout << "...\n"; } };
class Dog : public Animal { public: void speak() override { cout << "Woof\n"; } };
class Cat : public Animal { public: void speak() override { cout << "Meow\n"; } };
int main() {
Animal *arr[2] = { new Dog(), new Cat() };
for (Animal *a : arr) a->speak(); // run-time dispatch
}Woof
Meow
virtual के साथ बनाम बिना
virtual के साथ | virtual के बिना | |
|---|---|---|
basePtr->f() चलाता है | Derived का version | Base का version |
| Dispatch | Run time (dynamic) | Compile time (static) |
Pure virtual syntax
किसी function को pure virtual बनाने को = 0 जोड़ें — कोई body नहीं, और derived classes को इसे implement करना होगा। यह base को abstract class बनाता है:
class Shape {
public:
virtual double area() = 0; // pure virtual
};पूरी कहानी के लिए abstract classes देखें।
Keyword cheat sheet
| Keyword | कहाँ | अर्थ |
|---|---|---|
virtual | Base function | Override हो सकता; dynamic dispatch सक्षम |
override | Derived function | सत्यापित करता कि यह base virtual override करता है |
= 0 | Base function | Pure virtual; class abstract बनती है |
final | Function/class | आगे override/derivation रोकता है |
आम गलतियाँ
= 0लिखना पर बिना इरादे function को body भी देना।- Pointer/reference के बजाय object पर call करते समय dispatch की उम्मीद।
- बेमेल signature चुपचाप override के बजाय hiding करना (
overrideइस्तेमाल करें)। - Base में virtual destructor भूलना (मुख्य lesson में शामिल)।
एक base Employee लिखें जिसमें virtual salary() हो, और derived Manager तथा Intern। Pointers एक array में रखें और हर salary base pointer के ज़रिए print करें।
सारांश
virtualbase function पर जाता है;overridederived पर।- Run-time dispatch के लिए base pointer/reference के ज़रिए call करें।
= 0pure virtual और abstract class बनाता है।- Keyword cheat sheet: virtual, override, =0, final।
- Internals और design के लिए, मुख्य virtual functions lesson देखें।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में virtual function का syntax क्या है?
virtual लिखकर virtual function घोषित करते हैं, जैसे virtual void draw();। Derived class में आप इसे फिर से परिभाषित करते हैं और override जोड़ते हैं, जैसे void draw() override;, ताकि compiler override जाँचे।C++ में pure virtual function क्या है?
= 0 assign करके घोषित होता है, जैसे virtual void draw() = 0;। यह derived classes को अपना implementation देने पर मजबूर करता है और base class को abstract class बनाता है जिसका instance नहीं बन सकता।क्या C++ में overriding function पर virtual लिखते हैं?
virtual दोहराने की ज़रूरत नहीं; एक बार function base में virtual हो, उसके overrides भी अपने आप virtual हो जाते हैं। इसके बजाय override जोड़ना अच्छा अभ्यास है, जो इरादा दस्तावेज़ करता है और compiler को signature बेमेल पकड़ने देता है।C++ में virtual function कैसे call करते हैं?
basePtr->draw();। फिर वास्तविक object से मेल खाता version run time पर चुना जाता है।C++ में virtual और override में क्या अंतर है?
virtual किसी function को overridable चिह्नित करता है और run-time dispatch सक्षम करता है। Derived class में override बताता है कि function किसी base virtual को override करने को है, compiler को signature सत्यापित करने और मेल न खाने पर error रिपोर्ट करने देते हुए।