Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
📘 Lesson  ·  Lesson 71

Virtual Functions

Written and reviewed by · 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

C++
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

C++
#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
}
Output:
Woof
Meow

With vs without virtual

With virtualWithout virtual
basePtr->f() runsDerived's versionBase's version
DispatchRun 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:

C++
class Shape {
public:
    virtual double area() = 0;   // pure virtual
};

See abstract classes for the full story.

Keyword cheat sheet

KeywordWhereMeaning
virtualBase functionCan be overridden; enables dynamic dispatch
overrideDerived functionVerifies it overrides a base virtual
= 0Base functionPure virtual; class becomes abstract
finalFunction/classPrevents further overriding/derivation

Common mistakes

  • Writing = 0 but 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).
🏋️ Practice

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

  • virtual goes on the base function; override on the derived.
  • Call through a base pointer/reference for run-time dispatch.
  • = 0 makes 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

C++
class Base {
public:
    virtual void draw();      // base me 'virtual'
};
class Derived : public Base {
public:
    void draw() override;     // derived me 'override'
};

Base-pointer उदाहरण

C++
#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
}
Output:
Woof
Meow

virtual के साथ बनाम बिना

virtual के साथvirtual के बिना
basePtr->f() चलाता हैDerived का versionBase का version
DispatchRun time (dynamic)Compile time (static)

Pure virtual syntax

किसी function को pure virtual बनाने को = 0 जोड़ें — कोई body नहीं, और derived classes को इसे implement करना होगा। यह base को abstract class बनाता है:

C++
class Shape {
public:
    virtual double area() = 0;   // pure virtual
};

पूरी कहानी के लिए abstract classes देखें।

Keyword cheat sheet

Keywordकहाँअर्थ
virtualBase functionOverride हो सकता; dynamic dispatch सक्षम
overrideDerived functionसत्यापित करता कि यह base virtual override करता है
= 0Base functionPure virtual; class abstract बनती है
finalFunction/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 करें।

सारांश

  • virtual base function पर जाता है; override derived पर।
  • Run-time dispatch के लिए base pointer/reference के ज़रिए call करें।
  • = 0 pure virtual और abstract class बनाता है।
  • Keyword cheat sheet: virtual, override, =0, final।
  • Internals और design के लिए, मुख्य virtual functions lesson देखें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में virtual function का syntax क्या है?
आप base class में return type से पहले virtual लिखकर virtual function घोषित करते हैं, जैसे virtual void draw();। Derived class में आप इसे फिर से परिभाषित करते हैं और override जोड़ते हैं, जैसे void draw() override;, ताकि compiler override जाँचे।
C++ में pure virtual function क्या है?
Pure virtual function का base class में कोई body नहीं होता और = 0 assign करके घोषित होता है, जैसे virtual void draw() = 0;। यह derived classes को अपना implementation देने पर मजबूर करता है और base class को abstract class बनाता है जिसका instance नहीं बन सकता।
क्या C++ में overriding function पर virtual लिखते हैं?
आपको derived class में virtual दोहराने की ज़रूरत नहीं; एक बार function base में virtual हो, उसके overrides भी अपने आप virtual हो जाते हैं। इसके बजाय override जोड़ना अच्छा अभ्यास है, जो इरादा दस्तावेज़ करता है और compiler को signature बेमेल पकड़ने देता है।
C++ में virtual function कैसे call करते हैं?
आप इसे किसी भी member function की तरह call करते हैं, पर polymorphic व्यवहार के लिए इसे base-class pointer या reference के ज़रिए invoke करते हैं, जैसे basePtr->draw();। फिर वास्तविक object से मेल खाता version run time पर चुना जाता है।
C++ में virtual और override में क्या अंतर है?
Base class में virtual किसी function को overridable चिह्नित करता है और run-time dispatch सक्षम करता है। Derived class में override बताता है कि function किसी base virtual को override करने को है, compiler को signature सत्यापित करने और मेल न खाने पर error रिपोर्ट करने देते हुए।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.