Function Overriding in C++ Notes | Method Overriding, Runtime Polymorphism & Examples | Basic Computer Engineering | RGPV BTech First Year
Function Overriding in C++ Notes | Method Overriding, Runtime Polymorphism & Examples | Basic Computer Engineering | RGPV BTech First Year
Function Overriding in C++
Function Overriding C++ Object Oriented Programming (OOP) का एक महत्वपूर्ण Concept है जिसमें Derived Class, Base Class के किसी Member Function को उसी Name तथा समान Parameters के साथ पुनः Define करती है। जब Derived Class उसी Function का अपना अलग Implementation प्रदान करती है, तो इसे Function Overriding कहा जाता है।
Function Overriding का मुख्य उद्देश्य Base Class के Existing Behavior को आवश्यकता अनुसार Modify करना है। इससे Derived Class अपनी विशेष आवश्यकताओं के अनुसार Function की कार्यप्रणाली बदल सकती है, जबकि Function का Name तथा Signature समान रहता है।
RGPV B.Tech First Year के Basic Computer Engineering Subject में Function Overriding अत्यंत महत्वपूर्ण Topic है। University Examination, Practical तथा Viva में Function Overriding, Method Overriding, Runtime Polymorphism तथा Function Overriding Programs से संबंधित प्रश्न नियमित रूप से पूछे जाते हैं।
↓
Derived Class
↓
Same Function Name
↓
New Implementation
Introduction to Function Overriding
Object Oriented Programming में कई बार ऐसी आवश्यकता होती है कि Base Class का Function सभी Derived Classes के लिए उपयुक्त न हो। ऐसी स्थिति में Derived Class उसी Function को अपनी आवश्यकता के अनुसार पुनः Define करती है। यही प्रक्रिया Function Overriding कहलाती है।
Function Overriding की सहायता से प्रत्येक Derived Class समान Function Name का उपयोग करते हुए अलग-अलग कार्य कर सकती है। इससे Program अधिक Flexible, Reusable तथा Maintainable बनता है।
What is Function Overriding?
Function Overriding वह प्रक्रिया है जिसमें Derived Class, Base Class के Member Function को उसी Name, समान Return Type तथा समान Parameter List के साथ पुनः Define करती है। इससे Derived Class का Function Base Class के Function को Replace कर देता है।
ऊपर दिए गए उदाहरण में display() Function दोनों Classes में उपलब्ध है। Derived Class ने Base Class के Function को Override किया है।
Need of Function Overriding
जब Derived Class को Base Class के Function की कार्यप्रणाली बदलने की आवश्यकता हो, तब Function Overriding का उपयोग किया जाता है।
- Base Class के Behavior को Modify करने के लिए।
- Runtime Polymorphism लागू करने के लिए।
- Different Objects के लिए अलग-अलग Implementation प्रदान करने के लिए।
- Code Reusability बनाए रखते हुए Customized Functionality देने के लिए।
- Flexible Software Design विकसित करने के लिए।
- Large Object Oriented Applications बनाने के लिए।
- Professional Software Development Standards को लागू करने के लिए।
Advantages of Function Overriding
| Advantage | Description |
|---|---|
| Runtime Polymorphism | Runtime में अलग-अलग Objects अलग Behavior प्रदान कर सकते हैं। |
| Flexible Design | Derived Classes अपनी आवश्यकता अनुसार Function बदल सकती हैं। |
| Code Reusability | Base Class की Structure का पुनः उपयोग किया जा सकता है। |
| Easy Maintenance | Code को Update तथा Maintain करना सरल हो जाता है। |
| Supports OOP | Object Oriented Programming के Polymorphism सिद्धांत को लागू करता है। |
Characteristics of Function Overriding
Function Overriding में Base Class तथा Derived Class दोनों में समान नाम (Same Name) तथा समान Parameter List वाला Function होता है। Derived Class उसी Function का नया Implementation प्रदान करती है। यही विशेषता Function Overriding को Function Overloading से अलग बनाती है।
- Function का Name समान होना चाहिए।
- Parameter List समान होनी चाहिए।
- Function Base Class एवं Derived Class दोनों में होना चाहिए।
- Inheritance का उपयोग आवश्यक है।
- Derived Class Base Function का नया Implementation देती है।
- Runtime Polymorphism प्राप्त करने के लिए सामान्यतः virtual Function का उपयोग किया जाता है।
- Object Oriented Programming में Dynamic Behavior प्रदान करता है।
Rules of Function Overriding
Function Overriding सही प्रकार से कार्य करने के लिए कुछ महत्वपूर्ण नियमों का पालन करना आवश्यक होता है।
| Rule | Description |
|---|---|
| Same Function Name | Base तथा Derived Class में Function का नाम समान होना चाहिए। |
| Same Parameters | दोनों Functions की Parameter List समान होनी चाहिए। |
| Inheritance Required | Base तथा Derived Class के बीच Inheritance होना चाहिए। |
| Same Return Type | सामान्यतः Return Type समान रखा जाता है। |
| Virtual Function | Runtime Polymorphism के लिए Base Function को Virtual घोषित किया जाता है। |
Syntax of Function Overriding
ऊपर दिए गए Syntax में display() Function Derived Class द्वारा Override किया गया है। Base Class में virtual Keyword Runtime Polymorphism को Enable करता है।
Example of Function Overriding
Output
इस उदाहरण में Dog Class ने Animal Class के sound() Function को Override किया है। इसलिए Object द्वारा Call करने पर Derived Class का Function Execute होता है।
Method Overriding
Method Overriding तथा Function Overriding दोनों का अर्थ समान होता है। Object Oriented Programming में Member Functions को कई बार Methods भी कहा जाता है। इसलिए जब Derived Class किसी Base Class Method को पुनः Define करती है, तो इसे Method Overriding कहा जाता है।
↓
Derived Class Method
↓
Same Name
↓
Different Implementation
Working of Function Overriding
Function Overriding में जब Base Class तथा Derived Class दोनों में समान नाम एवं समान Parameter List वाला Function होता है, तब Program के Execution के दौरान उचित Function का चयन किया जाता है। यदि Base Class के Function को virtual घोषित किया गया हो, तो Runtime पर Object के वास्तविक Type के अनुसार Derived Class का Function Execute होता है।
↓
Virtual Function
↓
Derived Class Overrides Function
↓
Runtime Function Selection
↓
Derived Function Executes
Runtime Polymorphism using Function Overriding
Function Overriding का सबसे महत्वपूर्ण उपयोग Runtime Polymorphism प्राप्त करना है। इसमें Base Class के Pointer अथवा Reference द्वारा Derived Class के Object को Access किया जाता है तथा Runtime पर सही Function Execute होता है।
Output
ऊपर दिए गए Program में Pointer का Type Animal है, लेकिन Runtime पर Pointer Cat Object को Refer कर रहा है। इसलिए Virtual Function के कारण Cat Class का sound() Function Execute होता है।
Function Overriding vs Function Overloading
| Function Overriding | Function Overloading |
|---|---|
| Inheritance आवश्यक है। | Inheritance आवश्यक नहीं है। |
| Function Name समान होता है। | Function Name समान होता है। |
| Parameter List समान होती है। | Parameter List अलग-अलग होती है। |
| Runtime Polymorphism प्रदान करता है। | Compile Time Polymorphism प्रदान करता है। |
| Virtual Function का उपयोग किया जा सकता है। | Virtual Function की आवश्यकता नहीं होती। |
| Derived Class Base Function को Replace करती है। | एक ही Class में कई Functions उपलब्ध होते हैं। |
Real Life Example
मान लीजिए एक Shape Base Class में draw() Function है। अब Circle, Rectangle तथा Triangle Derived Classes उसी draw() Function को अपनी आवश्यकता अनुसार Override करती हैं। Program जब किसी Object का draw() Function Call करता है, तब प्रत्येक Shape अपनी अलग Drawing Logic Execute करती है। यही Function Overriding तथा Runtime Polymorphism का वास्तविक उपयोग है।
Important Points
- Function Name एवं Parameter List समान होनी चाहिए।
- Base एवं Derived Class के बीच Inheritance होना आवश्यक है।
- Runtime Polymorphism प्राप्त करने के लिए सामान्यतः virtual Function का उपयोग किया जाता है।
- Derived Class Base Function का नया Implementation प्रदान करती है।
- Function Overriding Dynamic Binding का आधार है।
- Function Overriding तथा Function Overloading दोनों अलग-अलग Concepts हैं।
- RGPV Practical एवं Viva में Virtual Function तथा Function Overriding आधारित Programs अत्यंत महत्वपूर्ण हैं।
Advantages of Function Overriding
Function Overriding Object Oriented Programming में Dynamic Behavior प्रदान करने का सबसे प्रभावी तरीका है। इसकी सहायता से प्रत्येक Derived Class अपनी आवश्यकता के अनुसार Base Class के Function का नया Implementation प्रदान कर सकती है। इससे Program अधिक Flexible तथा Maintainable बनता है।
- Runtime Polymorphism प्राप्त होता है।
- Derived Classes अपनी आवश्यकता अनुसार Function बदल सकती हैं।
- Code Reusability बनी रहती है।
- Software Design अधिक Flexible बनता है।
- Maintenance एवं Future Enhancement सरल हो जाती है।
- Large Scale Enterprise Applications के लिए उपयुक्त है।
- Object Oriented Programming के Polymorphism Principle को मजबूत बनाता है।
Limitations of Function Overriding
यद्यपि Function Overriding अत्यंत उपयोगी Concept है, फिर भी इसका उपयोग करते समय कुछ सीमाओं (Limitations) का ध्यान रखना आवश्यक है।
- Inheritance के बिना Function Overriding संभव नहीं है।
- Runtime Polymorphism के लिए सामान्यतः Virtual Function आवश्यक होता है।
- Large Class Hierarchy में Program समझना कठिन हो सकता है।
- Improper Overriding से Logical Errors उत्पन्न हो सकते हैं।
- Runtime Binding के कारण थोड़ा अतिरिक्त Processing Time लग सकता है।
- Beginners के लिए Virtual Functions तथा Dynamic Binding समझना कठिन हो सकता है।
Applications of Function Overriding
| Application | Purpose |
|---|---|
| Graphics Software | Circle, Rectangle तथा Triangle की अलग-अलग Drawing Logic लागू करना। |
| Game Development | Characters के अलग-अलग Attack एवं Movement Functions बनाना। |
| Banking Software | Savings एवं Current Account के अलग-अलग Interest Calculation Functions। |
| Hospital Management | Different Patient Categories के लिए Customized Processing। |
| Vehicle Management System | Car, Bike तथा Bus के अलग-अलग Start() अथवा Drive() Functions। |
| Enterprise Applications | Business Modules के लिए Customized Runtime Behavior। |
Best Practices
- जहाँ Runtime Polymorphism आवश्यक हो, वहाँ virtual Function का उपयोग करें।
- Function Name, Return Type तथा Parameter List को समान रखें।
- Override किए गए Function का उद्देश्य स्पष्ट रखें।
- अनावश्यक Function Overriding से बचें।
- Class Hierarchy को सरल एवं Maintainable रखें।
- Meaningful Function Names तथा Proper Documentation का उपयोग करें।
- Object Oriented Design Principles का पालन करें।
Real Life Example
मान लीजिए किसी Payment System में Payment नाम की Base Class है जिसमें processPayment() Function उपलब्ध है। अब CreditCard, UPI तथा NetBanking जैसी Derived Classes उसी Function को अपनी-अपनी Payment Method के अनुसार Override करती हैं। जब Program Runtime पर Payment Process करता है, तब संबंधित Derived Class का Function Execute होता है। यही Function Overriding का वास्तविक उपयोग है।
Working of Function Overriding
↓
Declare Virtual Function
↓
Create Derived Class
↓
Override Same Function
↓
Create Derived Object
↓
Runtime Function Selection
↓
Derived Function Executes
Applications of Function Overriding
Function Overriding का उपयोग आधुनिक Object Oriented Programming में Dynamic Behavior प्रदान करने के लिए किया जाता है। इसकी सहायता से Base Class का समान Function विभिन्न Derived Classes में अलग-अलग प्रकार से कार्य कर सकता है। यही कारण है कि Enterprise Software, Game Development तथा Banking Systems जैसे बड़े Applications में इसका व्यापक उपयोग होता है।
| Application | Purpose |
|---|---|
| Graphics Software | Circle, Rectangle तथा Triangle के अलग-अलग Draw() Functions। |
| Game Development | Enemy, Player तथा Boss Characters के अलग-अलग Attack() Functions। |
| Banking Software | Savings, Current एवं Loan Accounts के लिए अलग-अलग Interest Calculation। |
| Hospital Management System | Different Patient Categories के लिए Customized Billing एवं Treatment Functions। |
| Vehicle Management System | Car, Bike एवं Bus के लिए अलग-अलग Start() तथा Drive() Methods। |
| Payment Gateway | UPI, Credit Card एवं Net Banking के लिए अलग-अलग Payment Processing। |
| Enterprise ERP | Business Modules में Runtime पर अलग-अलग Processing Logic लागू करना। |
Advantages of Function Overriding
- Runtime Polymorphism प्राप्त होता है।
- Derived Classes अपनी आवश्यकता अनुसार Function का नया Implementation दे सकती हैं।
- Code Reusability बनी रहती है।
- Software अधिक Flexible एवं Extensible बनता है।
- Maintenance तथा Future Enhancement सरल हो जाती है।
- Dynamic Binding को Support करता है।
- Professional Object Oriented Programming के लिए अत्यंत उपयोगी है।
Limitations of Function Overriding
- Inheritance के बिना Function Overriding संभव नहीं है।
- Runtime Polymorphism के लिए सामान्यतः Virtual Function आवश्यक होता है।
- Large Class Hierarchy Program की Complexity बढ़ा सकती है।
- Dynamic Binding के कारण थोड़ा Runtime Overhead हो सकता है।
- गलत Overriding Logical Errors उत्पन्न कर सकती है।
- Beginners के लिए Virtual Functions समझना कठिन हो सकता है।
- Improper Class Design से Program Maintain करना कठिन हो सकता है।
Best Practices
- जहाँ Runtime Polymorphism आवश्यक हो, वहाँ Base Function को virtual घोषित करें।
- Override किए गए Function का उद्देश्य स्पष्ट रखें।
- Function Name एवं Parameter List समान रखें।
- अनावश्यक Function Overriding से बचें।
- Class Hierarchy को सरल एवं Maintainable बनाए रखें।
- Meaningful Function Names तथा Proper Documentation का उपयोग करें।
- Object Oriented Programming के SOLID एवं Polymorphism Principles का पालन करें।
Real Life Example
मान लीजिए किसी E-Commerce Application में Notification नाम की Base Class है जिसमें sendNotification() Function उपलब्ध है। अब EmailNotification, SMSNotification तथा PushNotification नाम की Derived Classes इसी Function को Override करती हैं। जब Runtime पर Notification भेजा जाता है, तब संबंधित Derived Class का Function Execute होता है और उसी माध्यम से संदेश भेजा जाता है। इससे एक ही Function Name का उपयोग करते हुए अलग-अलग प्रकार की Notifications आसानी से भेजी जा सकती हैं।
Working of Function Overriding
↓
Declare Virtual Function
↓
Create Derived Class
↓
Override Same Function
↓
Create Base Pointer
↓
Assign Derived Object
↓
Runtime Binding
↓
Derived Function Executes
Summary
Function Overriding C++ Object Oriented Programming का एक महत्वपूर्ण Concept है जिसमें Derived Class, Base Class के Member Function को उसी Function Name, समान Parameter List तथा सामान्यतः समान Return Type के साथ पुनः Define करती है। यदि Base Class का Function virtual घोषित किया गया हो, तो Runtime पर Object के वास्तविक Type के अनुसार सही Function Execute होता है। यही प्रक्रिया Runtime Polymorphism अथवा Dynamic Binding कहलाती है। Function Overriding की सहायता से Program अधिक Flexible, Reusable, Extensible तथा Maintainable बनता है।
| Concept | Description |
|---|---|
| Function Overriding | Derived Class द्वारा Base Class Function का नया Implementation देना। |
| Inheritance | Function Overriding के लिए आवश्यक है। |
| Function Name | दोनों Classes में समान होना चाहिए। |
| Parameter List | समान होनी चाहिए। |
| Virtual Function | Runtime Polymorphism को Enable करता है। |
| Binding Type | Dynamic (Runtime) Binding। |
| Main Benefit | Customized Runtime Behavior प्रदान करना। |
| Applications | Graphics, Banking, ERP, Games, Hospital एवं Enterprise Software। |
Important Viva Questions
- Function Overriding क्या है?
- Function Overriding तथा Method Overriding में क्या अंतर है?
- Function Overriding के लिए कौन-कौन से Rules आवश्यक हैं?
- Virtual Function क्या है?
- Runtime Polymorphism क्या है?
- Dynamic Binding क्या है?
- Function Overriding तथा Function Overloading में क्या अंतर है?
- Base Pointer द्वारा Derived Function कैसे Execute होता है?
- Function Overriding के Advantages क्या हैं?
- Real Life Applications में Function Overriding कहाँ उपयोग होता है?
University Examination Important Questions
- Function Overriding क्या है? उदाहरण सहित समझाइए।
- Runtime Polymorphism को Virtual Function की सहायता से समझाइए।
- Function Overriding तथा Function Overloading में अंतर स्पष्ट कीजिए।
- Virtual Function का Program लिखिए।
- Base Class तथा Derived Class में Function Overriding को उदाहरण सहित समझाइए।
- Dynamic Binding क्या है? Function Overriding में इसकी भूमिका स्पष्ट कीजिए।
- Function Overriding के Advantages एवं Applications लिखिए।
- Runtime Polymorphism तथा Compile Time Polymorphism में अंतर स्पष्ट कीजिए।
- Method Overriding क्या है? उदाहरण सहित समझाइए।
- Object Oriented Programming में Function Overriding का महत्व स्पष्ट कीजिए।
Exam Tips
- हमेशा याद रखें कि Function Overriding के लिए Inheritance आवश्यक है।
- Function Name तथा Parameter List समान होनी चाहिए।
- Runtime Polymorphism प्राप्त करने के लिए Base Class Function को सामान्यतः virtual घोषित किया जाता है।
- Function Overriding तथा Function Overloading के अंतर को Comparison Table के माध्यम से याद करें।
- Base Pointer तथा Derived Object वाले Programs का अभ्यास अवश्य करें।
- Dynamic Binding तथा Runtime Polymorphism परीक्षा में अत्यंत महत्वपूर्ण Concepts हैं।
- RGPV Practical एवं Viva में Virtual Function तथा Function Overriding आधारित Programs नियमित रूप से पूछे जाते हैं।
Conclusion
Function Overriding C++ Object Oriented Programming का आधारभूत एवं अत्यंत महत्वपूर्ण Concept है, जो Runtime Polymorphism तथा Dynamic Binding को संभव बनाता है। इसकी सहायता से Derived Class, Base Class के Function का नया Implementation प्रदान कर सकती है और Runtime पर Object के अनुसार उचित Function Execute होता है। इससे Software अधिक Flexible, Extensible तथा Maintainable बनता है। Graphics Applications, Banking Systems, Hospital Management, Payment Gateways, ERP Software तथा Game Development जैसे अनेक Real World Projects में Function Overriding का व्यापक उपयोग किया जाता है। RGPV B.Tech First Year के विद्यार्थियों के लिए यह Topic Theory, Practical तथा Viva तीनों दृष्टिकोण से अत्यंत महत्वपूर्ण है।
Related Articles
Standard Template Library (STL) in C++ Notes | Containers, Iterators & Algorithms | Basic Computer Engineering | RGPV BTech First Year
Standar...
Read More →Exception Handling in C++ Notes | try, throw, catch & Exception Handling Mechanism | Basic Computer Engineering | RGPV BTech First Year
Excepti...
Read More →Templates in C++ Notes | Function Templates, Class Templates & Generic Programming | Basic Computer Engineering | RGPV BTech First Year
Templat...
Read More →Virtual Functions in C++ Notes | Virtual Function, Pure Virtual Function & Dynamic Binding | Basic Computer Engineering | RGPV BTech First Year
Virtual...
Read More →Constructors and Destructors in Inheritance Notes | Constructor & Destructor Calling Order in C++ | Basic Computer Engineering | RGPV BTech First Year
Constru...
Read More →