C++ Virtual Function In Hindi

📔 : C++ 🔗

Virtual function को समझने से पहले हम polymorphism के बारे में थोड़ा सा समझते हैं।


Normally base / parent class में defined method को fir से child class में define करके override करना फिर need के according different - different तरीके से tasks perform करना ही polymorphism है।
For Example :

CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define base class
class Food {
  public:
  void eat() {
    cout << "Eating Food \n" ;
  }
};

// Derived class
class Paneer : public Food {
  public:
  
  // give different definition
  void eat() {
    cout << "Eating Paneer" ;
  }
};


int main() {
  Food foodObj;
  Paneer paneerObj;

  foodObj.eat();
  paneerObj.eat();
  return 0;
}
Output
Eating Food 
Eating Paneer

ऊपर polymorphism का एक example दिया गया है , जिसमे eat() function को override करके need के according अलग अलग definition दी गयी है।

C++ Virtual Function

ठीक polymorphism की तरह ही virtual functions define किये जाते हैं , लेकिन इन्हे virtual keyword के साथ child / derived class में override किया जाता है।


यह special type के function होते हैं जिनका मैं purpose है runtime polymorphism को achieve करना, मतलब इसका प्रयोग function पर late binding करने के लिए किया जाता है। इसमें function call run-time में complete होता है। Virtual Function यह sure करता है कि object के लिए सही function को call किया गया है।

C++ Virtual Function Example

CopyFullscreenClose FullscreenRun
#include <iostream>
  using namespace std;

// define base class
class Food {
  public:
  
  // define as virtual function.
  void virtual eat() {
    cout << "Eating Food \n" ;
  }
};

// Derived class
class Paneer : public Food {
  public:
  
  // give different definition
  void eat() {
    cout << "Eating Paneer" ;
  }
};


int main() {
  Food* foodObj;
  Paneer paneerObj;

  // now bind at run time.
  foodObj = &paneerObj;
  foodObj->eat();
  return 0;
}
Output
Eating Paneer

हालाँकि जूरी नहीं है आप , derived / child class के object का address base class pointer में assign करें , आप directly new keyword का use करके भी assign कर सकते हैं -

Food* foodObj = new Paneer;
// now bind at run time.
foodObj->eat();

// output : 
Eating Paneer

C++ Rules For Virtual Function

  • virtual functions को कभी-भी as a static members define नहीं किया जा सकता है। और न ही किसी class के friend function नहीं हो सकते।

  • virtual functions को हमेशा public declare किया जाना चाहिए , ये private या protected नहीं हो सकते हैं।

  • Run time polymorphism को achieve करने के लिए virtual functions को pointer का उसे करके ही access किया जाना चाहिए।

  • अब चूंकि Run time polymorphism achieve करनी है तो virtual functions का prototype (parameters और arguments list) हमेशा base / parent class और derived / derived class में same होना चाहिए।

C++ Pure Virtual Function

Pure virtual function में base class में defined function के लिए definition नहीं provide की जाती है , simply 0 assign कर दिया जाता है। उसकी definition को इसकी derived / child class में दिया जाता है।

C++ Pure Virtual Function Example
CopyFullscreenClose FullscreenRun
#include <iostream>
using namespace std;

// define base class
class Food {
  public:
  // this is pure virtual function.
  void virtual eat() = 0;
};

// Derived class
class Paneer : public Food {
  public:
  // provide definition
  void eat() {
    cout << "Eating Paneer" ;
  }
};


int main() {
  Food* foodObj;
  Paneer paneerObj;

  // now bind at run time.
  foodObj = &paneerObj;
  foodObj->eat();
  return 0;
}
Output
Eating Paneer

I Hope, आपको C++ में virtual functions के बारे में अच्छे से समझ आया होगा।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers