Virtual functions: परिभाषा, विशेषताएँ, महत्व, लाभ और हानियाँ

Virtual functions (वर्चुअल फंक्शन)

C++ में virtual function एक ऐसा member function होता है, जिसे base class में declare किया जाता है, लेकिन उसे derived class में override किया जा सकता है।

  • इसे declare करने के लिए virtual keyword का use किया जाता है।
  • जब हम किसी function को pointer या reference के through call करते हैं, तो C++ runtime पर decide करता है, कि कौन सा function call होगा।
  • इस process को Runtime Polymorphism या Dynamic Dispatch कहते हैं।

Syntax :-

class Base {
public:
virtual void functionName() {
// Base class ka code
}
};

class Derived : public Base {
public:
void functionName() override { // override function
// Derived class ka code
}
};

int main() {
Base* basePtr; // Base class pointer
Derived d; // Derived class object

basePtr = &d; // base pointer derived object ko point karega
basePtr->functionName(); // Output: Derived class ka function chalega

return 0;
}

Features (विशेषताएँ)

  • Virtual function को हमेशा base class में declare किया जाता है, और इसके साथ virtual keyword लगाया जाता है।
  • Derived class उसी function को आसानी से override कर सकती है।
  • Function call का decision हमेशा object type के आधार पर होता है, न कि pointer type के आधार पर।
  • इसे Run-Time Polymorphism achieve करने के लिए use किया जाता है।
  • अगर function को base class pointer के through call किया जाए, और function override किया गया हो, तो derived class का function execute होगा।

Importance of Virtual Function in C++ (महत्व)

Object-Oriented Programming (OOPs) में Inheritance use करते हैं, अक्सर ऐसा scenario होता है, जहाँ Base Class का pointer या reference किसी Derived Class के object को point कर रहा होता है।

अगर Base Class का function virtual नहीं है, तो compiler, compile-time में ही decide कर देता है, कि कौन सा function call होगा। इसका मतलब यह है कि हमेशा Base Class का function execute होगा, चाहे object Derived Class का ही क्यों न हो।

 

Key Reasons (मुख्य कारण) :

  1. Run-Time Polymorphism
    • Virtual Function हमें allow करता है, कि function call object type के अनुसार decide हो, ना कि pointer type के अनुसार।
    • Example : Base pointer → Derived object → Correct function executes.
  2. Flexible And Maintainable Code
    • एक ही interface (Base class function) के जरिए multiple behavior implement किया जा सकता है।
    • Future में अगर नई Derived class add हो, तो Base class code change करने की जरूरत नहीं होती।
  3. Code Reusability
    • Base class pointer/references से सभी Derived class के functions call किए जा सकते हैं।
    • बार-बार code लिखने की जरूरत नहीं होती।
  4. Easy Maintenance in Large Projects
    • Banking, ERP, Healthcare, IRCTC जैसे बड़े software में Virtual Function अलग-अलग modules का behavior manage करना आसान बनाता है।
  5. Future-Ready System
    • नए features add करना आसान होता है, क्योंकि सिर्फ Derived class में नया function override करना होता है।

example (उदाहरण)

#include <iostream>
using namespace std;

class Base {
public:
virtual void show() { // Virtual function
cout << “Base class function” << endl;
}
};

class Derived : public Base {
public:
void show() override { // Override in derived class
cout << “Derived class function” << endl;
}
};

int main() {
Base* b; // Base class pointer
Derived d; // Derived class object

b = &d; // Base pointer points to Derived object
b->show(); // Output: Derived class function

return 0;
}

Output :

Derived class function

Explanation :

  1. Base class में show() function को virtual declare किया गया।
  2. Derived class में show() को override किया।
  3. Base pointer से Derived object को point किया।
  4. Function call पर Derived class का function execute हुआ, क्योंकि virtual function use किया गया।

Advantages (लाभ)

1. Run-Time Polymorphism possible:
Virtual functions का use करके हम Run-Time Polymorphism achieve कर सकते हैं। Program run-time पर decide करता है, कि कौन सा function call होगा।

2. Dynamic Binding (Late Binding):
Normal functions में Early Binding होती है, लेकिन Virtual functions में Late Binding use होती है। इससे Derived class का function automatically call हो जाता है।

3. Flexible & Extensible Code:
Code ज़्यादा flexible और extendable बन जाता है। नए classes add करना आसान होता है, बिना existing code को बदलें।

4. Proper Function Overriding:
Virtual function ensure करता है कि Derived class का function ही execute होगा, ना कि Base class का।

Disadvantages (हानि)

1. Slight Performance Overhead:
Virtual functions में vtable (Virtual Table) use होती है, जिससे function call में थोड़ा extra time लगता है।

2. Memory Overhead:
हर class में vtable (Virtual Table) और vptr store (Virtual Pointer) होती है, जिससे memory usage बढ़ जाता है।

3. Cannot be Inline:
Virtual functions को compiler usually inline नहीं कर सकता, जिससे performance पर थोड़ा impact पड़ता है।

4. Complexity in Multiple Inheritance:
Multiple inheritance में virtual function handling थोड़ी complicated हो जाती है।

5. Cannot be Static:
Virtual functions static नहीं हो सकती, क्योंकि run-time polymorphism के लिए instance object जरूरी है।

error: Content is protected !!