What is Virtual Function in c++ in hindi || वर्चुअल फंक्शन क्या है?

Explain virtual function in detail?


Table of Contents (toc)

Introduction

Virtual function का तात्पर्य अप्रत्यक्ष रूप से प्रभावी होना है, परंतु यह वास्तव में नहीं होता है। C++ Language में एक member function runtime में जब विभिन्न प्रकार के काम को पूरा करता है इस feature को polymorphism कहते हैं। Runtime पर polymorphism का feature रखने वाले function को virtual function कहा जाता है। Virtual function से संबंधित claas को polymorphism class कहते हैं।

Definition of Virtual function in Hindi

ऐसा function जो वास्तव में उपलब्ध नहीं होता परंतु प्रोग्राम के कुछ भागों में फिर भी वास्तव में प्रकट होता है, Virtual Function कहलाता है।

Syntax

Virtual function का syntax इस प्रकार से है-

class class name  
 {  
  public:  
  virtual void function name ()  
 {  
  .................................................  
  ................................................. [body of virtual function]  
 }};(code-box)

Program

#include <iostream>
using namespace std;

class base {
public:
    void dis() {
        cout << "in display base" << endl;
    }
    virtual void show() {
        cout << "show base" << endl;
    }
};

class D : public base {
public:
    void dis() {
        cout << "display derived" << endl;
    }
    void show() {
        cout << "show derived" << endl;
    }
};

int main() {
    base OB;
    D OD;
    base* bptr;

    cout << "bptr points to base" << endl;
    bptr = &OB;
    bptr->dis();
    bptr->show();

    cout << "bptr points to derived" << endl;
    bptr = &OD;
    bptr->dis();   // Calls base::dis() because it's not virtual
    bptr->show();  // Calls D::show() because show() is virtual

    return 0;
}(code-box)

Need for virtual function 

C++ language में function call दो प्रकार के हो सकते हैं- 

1. Compile time

2. Run time

जब function call compilation के समय होता है तो इसे compile time कहते हैं। इसके अलावा यदि function call runtime पर होता है तो उसे runtime कहते हैं। Runtime की स्थिति को virtual function की सहायता से प्राप्त किया जा सकता है। Virtual function की सहायता से हम function को base class में derived class में इस virtual function को define keyword की मदद से declare किया जाता है।

Features of Virtual Function

1. ये एक base class के non-static member होते हैं।

2. Virtual function, runtime के समय किये जाते हैं। 

3. C++ में non-virtual function compile time के समय solve किये जाते हैं।

4. ये virtual keyword के साथ declare किये जाते हैं।

5. Virtual function के object pointer की सहायता से access किये जाते हैं।

6. ये दूसरे function के friend function हो सकते हैं।

7. Virtual function का base class और derived class का proto type एक समान चाहिए।

8. यदि virtual function को base  class में declare किया जाता है तो derived class में declare करने की आवश्यकता नहीं होती है।

Explain Pure-virtual function in detail?

Pure-virtual function in Hindi

यह एक ऐसा function होता है जो base class के अंदर declare किया जाता है। जिसकी base class से relative कोई definition नहीं होती। इन classes में compiler को प्रत्येक ऐसे derived classes की आवश्यकता होती है जो function को define करे या pure virtual function के रूप को redeclare करे।

Definition

वे function जो base class में घोषित किये जाते हैं तथा जिनका base class से कोई संबंध नहीं होता, pure-virtual function कहलाते हैं।

Program

#include <iostream>
#include <conio.h>  //

class base {
private:
    int x;
    float y;

public:
    virtual void get_data();
    virtual void display();
};

class derived : public base {
    // Add specific members and methods for the derived class
    // For example:
private:
    int z;

public:
    void get_data() override;
    void display() override;
};

// Definition of base class methods
void base::get_data() {
    cout << "Enter int x: ";
    cin >> x;
    cout << "Enter float y: ";
    cin >> y;
}

void base::display() {
    cout << "Base class display" << endl;
}(code-box)

लेख पसंद आया है तो इसे जरूर शेयर करें।

Post a Comment

1 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Hi please, do not spam in comments

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !