वर्चुअल फंक्शन क्या होता है एवं इसके क्या उपयोग होते है।

वर्चुअल फंक्शन क्या होता है एवं इसके क्या उपयोग होते है।

वर्चुअल फंक्शन क्या होता है एवं इसके क्या उपयोग होते है।
Ganesh
Wednesday 8 March 2017

Virtual Function-


Virtual Function का use run time polymorphism को प्राप्त करने के लिए किया जाता है।
अगर हमारे पास दो class हो एक base class तथा दूसरी derived class हो और हम pointer का इस्तेमाल करके derived class के object को access करना चाहते हो तो ऐसा नही हो पाता है क्योंकि base class का pointer हमेशा base class के function को ही execute करेगा।
इस problem को solve करने के लिए virtual function use किया जाता है।

ukpolytechnicnotes.blogspot.com

            Base class के function को virtual बनाया जाता है जिसके लिए base class के आगे Virtual शब्द लिख दिया जाता है जब कोई function virtual बना दिया जाता है तो C++ base pointer द्वारा point किये गए object से यह निर्धारित करती है कि किस function को चलाना है।

Example-
class base
{
void C()
{
Cout<<"C is here";
}
Virtual void C++()       //virtual Function
{
Cout<<"C++ is here";
}
}
class derived : Public base
{
void C()
{
Cout<<"C is there";
}
void C++()
{
Cout<<"C++ is there";
}
}
void main()
{
base.b1;       //object
derived d1;    //object
base*pptr;      //pointer
bptr=&b1;
bptr->C();      // C is here
bptr->C++();   C++ is here
bptr=&d1;
bptr->C();      // C is here
bptr->C++();   //C++ is here
//Output virtual Function
}

derived class के object C function को class करने पर base class का ही output आयेगा, क्योकि base pointer हमेशा base class के function को ही लाता है परंतु यदि base class का function virtual हो जैसा कि C++ name का function है तो derived class का function call हो जाता है।

ukpolytechnicnotes.blogspot.com





Open Comments
Close comment

6 comments

  1. sir mujhe base class or driver class samjh me nhi aayi ye class kiyo hoti hai please sir ditail me expln kar bataaiye

    ReplyDelete
  2. Virtual function to samjh geya
    But mujhe operating system, software engineering ke bhi content upload kro

    ReplyDelete