इस ट्यूटोरियल में, हम C ++ में फ़ंक्शन ओवरराइडिंग के बारे में उदाहरणों की मदद से सीखेंगे।
जैसा कि हम जानते हैं, विरासत OOP की एक विशेषता है जो हमें आधार वर्ग से व्युत्पन्न वर्ग बनाने की अनुमति देती है। व्युत्पन्न वर्ग बेस क्लास की विशेषताएं हैं।
मान लीजिए, एक ही फ़ंक्शन व्युत्पन्न वर्ग और आधारित वर्ग दोनों में परिभाषित किया गया है। अब अगर हम व्युत्पन्न वर्ग के ऑब्जेक्ट का उपयोग करके इस फ़ंक्शन को कहते हैं, तो व्युत्पन्न वर्ग के कार्य को निष्पादित किया जाता है।
इसे C ++ में फ़ंक्शन ओवरराइडिंग के रूप में जाना जाता है । व्युत्पन्न वर्ग में फ़ंक्शन बेस क्लास में फ़ंक्शन को ओवरराइड करता है।
उदाहरण 1: C ++ फ़ंक्शन ओवरराइडिंग
// C++ program to demonstrate function overriding #include using namespace std; class Base ( public: void print() ( cout << "Base Function" << endl; ) ); class Derived : public Base ( public: void print() ( cout << "Derived Function" << endl; ) ); int main() ( Derived derived1; derived1.print(); return 0; )
आउटपुट
व्युत्पन्न कार्य
यहां, एक ही फ़ंक्शन print()
को दोनों Base
और Derived
वर्गों में परिभाषित किया गया है ।
तो, जब हम फोन print()
से Derived
वस्तु derived1, print()
से Derived
में समारोह अधिभावी द्वारा निष्पादित किया जाता है Base
।

C ++ में ओवरराइड फ़ंक्शन को एक्सेस करें
बेस क्लास के ओवरराइड फ़ंक्शन को एक्सेस करने के लिए, हम स्कोप रिज़ॉल्यूशन ऑपरेटर का उपयोग करते हैं ::
।
हम व्युत्पन्न वर्ग के किसी ऑब्जेक्ट को इंगित करने के लिए बेस क्लास के पॉइंटर का उपयोग करके ओवरराइड फ़ंक्शन का उपयोग कर सकते हैं और फिर उस पॉइंटर से फ़ंक्शन को कॉल कर सकते हैं।
उदाहरण 2: C ++ बेस क्लास के लिए ओवरराइड फंक्शन
// C++ program to access overridden function // in main() using the scope resolution operator :: #include using namespace std; class Base ( public: void print() ( cout << "Base Function" << endl; ) ); class Derived : public Base ( public: void print() ( cout << "Derived Function" << endl; ) ); int main() ( Derived derived1, derived2; derived1.print(); // access print() function of the Base class derived2.Base::print(); return 0; )
आउटपुट
व्युत्पन्न फंक्शन बेस फंक्शन
यहाँ, यह कथन
derived2.Base::print();
print()
बेस क्लास के फंक्शन को एक्सेस करता है।

उदाहरण 3: C ++ कॉल अविकसित कार्य व्युत्पन्न वर्ग से
// C++ program to call the overridden function // from a member function of the derived class #include using namespace std; class Base ( public: void print() ( cout << "Base Function" << endl; ) ); class Derived : public Base ( public: void print() ( cout << "Derived Function" << endl; // call overridden function Base::print(); ) ); int main() ( Derived derived1; derived1.print(); return 0; )
आउटपुट
व्युत्पन्न फंक्शन बेस फंक्शन
इस कार्यक्रम में, हमने Derived
कक्षा के अंदर ही ओवरराइड फ़ंक्शन को बुलाया है।
class Derived : public Base ( public: void print() ( cout << "Derived Function" << endl; Base::print(); ) );
कोड को नोटिस करें Base::print();
, जो Derived
वर्ग के अंदर ओवरराइड फ़ंक्शन को कॉल करता है ।

उदाहरण 4: C ++ पॉइंटर का उपयोग करके ओवरराइड फ़ंक्शन को कॉल करें
// C++ program to access overridden function using pointer // of Base type that points to an object of Derived class #include using namespace std; class Base ( public: void print() ( cout << "Base Function" << endl; ) ); class Derived : public Base ( public: void print() ( cout << "Derived Function"
Output
Base Function
In this program, we have created a pointer of
Base
type named ptr. This pointer points to the Derived
object derived1.
// pointer of Base type that points to derived1 Base* ptr = &derived1;
When we call the
print()
function using ptr, it calls the overridden function from Base
.
// call function of Base class using ptr ptr->print();
This is because even though ptr points to a
Derived
object, it is actually of Base
type. So, it calls the member function of Base
.
In order to override the
Base
function instead of accessing it, we need to use virtual functions in the Base
class.