Inheritance in C++ Notes | Types of Inheritance, Visibility Modes & Examples | Basic Computer Engineering | RGPV BTech First Year

Inheritance in C++ Notes | Types of Inheritance, Visibility Modes & Examples | Basic Computer Engineering | RGPV BTech First Year


Inheritance in C++

Inheritance C++ की सबसे महत्वपूर्ण Object Oriented Programming (OOP) विशेषताओं में से एक है। Inheritance की सहायता से एक नई Class किसी Existing Class के Data Members तथा Member Functions को प्राप्त (Inherit) कर सकती है। इससे Code Reusability बढ़ती है तथा Program अधिक Efficient, Modular एवं Maintainable बनता है।

Inheritance में जिस Class से Properties प्राप्त की जाती हैं उसे Base Class (Parent Class) कहा जाता है तथा जो Class उन Properties को प्राप्त करती है उसे Derived Class (Child Class) कहा जाता है। Derived Class Base Class के Members का उपयोग करने के साथ-साथ अपने नए Members भी जोड़ सकती है।

RGPV B.Tech First Year के Basic Computer Engineering Subject में Inheritance in C++ अत्यंत महत्वपूर्ण Topic है। University Examination, Practical तथा Viva में Base Class, Derived Class, Types of Inheritance, Visibility Modes तथा Inheritance Programs से संबंधित प्रश्न नियमित रूप से पूछे जाते हैं।

Base Class



Inheritance



Derived Class



Reuse Existing Features

Introduction to Inheritance

Object Oriented Programming का मुख्य उद्देश्य Code को Reusable बनाना है। यदि किसी Program में पहले से बनाई गई Class की Properties को पुनः उपयोग करना हो, तो नई Class को शुरू से बनाने की आवश्यकता नहीं होती। ऐसी स्थिति में Inheritance का उपयोग किया जाता है।

Inheritance द्वारा Existing Class की सभी आवश्यक Properties नई Class में उपलब्ध हो जाती हैं। इससे Development Time कम होता है, Code Duplication घटती है तथा Software की Quality बेहतर होती है।


What is Inheritance?

Inheritance वह प्रक्रिया है जिसमें एक Class दूसरी Class की Properties तथा Behaviors को प्राप्त करती है। नई Class Existing Class के Data Members एवं Member Functions का उपयोग कर सकती है तथा आवश्यकता अनुसार नए Members भी जोड़ सकती है।

class Student { // Base Class }; class Result : public Student { // Derived Class };

ऊपर दिए गए उदाहरण में Student एक Base Class है तथा Result एक Derived Class है। Result Class, Student Class की Public तथा Protected Properties को Inherit करती है।


Need of Inheritance

जब Program में कई Classes में समान प्रकार के Data Members तथा Member Functions की आवश्यकता हो, तब Inheritance का उपयोग किया जाता है। इससे Code दोबारा लिखने की आवश्यकता नहीं होती तथा Program अधिक Efficient बनता है।

  • Code Reusability बढ़ाने के लिए।
  • Code Duplication कम करने के लिए।
  • Program को Modular बनाने के लिए।
  • Software Maintenance आसान करने के लिए।
  • Object Oriented Programming Concepts लागू करने के लिए।
  • Existing Classes का पुनः उपयोग करने के लिए।
  • Professional Software Development को सरल बनाने के लिए।

Advantages of Inheritance

Advantage Description
Code Reusability Existing Code का पुनः उपयोग किया जा सकता है।
Reduced Code Duplication एक ही Code को बार-बार लिखने की आवश्यकता नहीं होती।
Easy Maintenance Program को Update तथा Maintain करना आसान हो जाता है।
Extensibility नई Features जोड़ना सरल हो जाता है।
Supports OOP Object Oriented Programming के मुख्य सिद्धांतों को लागू करता है।

Characteristics of Inheritance

Inheritance की कई महत्वपूर्ण विशेषताएँ (Characteristics) हैं जो इसे Object Oriented Programming का एक शक्तिशाली Concept बनाती हैं। यह Existing Class की Properties को नई Class में Reuse करने की सुविधा प्रदान करता है।

  • Code Reusability को बढ़ावा देता है।
  • Base Class की Properties Derived Class में उपलब्ध होती हैं।
  • Code Duplication को कम करता है।
  • Program को Modular एवं Maintainable बनाता है।
  • Derived Class में नए Members जोड़े जा सकते हैं।
  • Object Oriented Programming का मुख्य Feature है।
  • Large Software Projects के लिए अत्यंत उपयोगी है।

Syntax of Inheritance

C++ में Inheritance Declare करने के लिए Derived Class के साथ Base Class का नाम तथा Visibility Mode लिखा जाता है।

class DerivedClass : visibility_mode BaseClass { // Members of Derived Class };

यहाँ visibility_mode Public, Protected अथवा Private हो सकता है।


Base Class

Base Class (Parent Class अथवा Super Class) वह Class होती है जिसकी Properties एवं Member Functions दूसरी Class द्वारा Inherit किए जाते हैं।

class Student { public: void display() { cout << "Student Information"; } };

ऊपर दिए गए उदाहरण में Student एक Base Class है।


Derived Class

Derived Class (Child Class अथवा Sub Class) वह Class होती है जो Base Class की Properties तथा Member Functions को प्राप्त करती है तथा आवश्यकता अनुसार अपने नए Members भी जोड़ सकती है।

class Result : public Student { public: void showResult() { cout << "Result"; } };

ऊपर दिए गए उदाहरण में Result Class, Student Class से Inherit की गई है।


Code Reusability through Inheritance

Inheritance का सबसे बड़ा लाभ Code Reusability है। यदि किसी Class में पहले से आवश्यक Functions उपलब्ध हैं, तो नई Class उन्हीं Functions का पुनः उपयोग कर सकती है।

#include<iostream> using namespace std; class Student { public: void display() { cout << "Student Details"; } }; class Result : public Student { public: void marks() { cout << "Marks Available"; } }; int main() { Result r; r.display(); r.marks(); return 0; }

Output

Student Details Marks Available

इस Program में Result Class ने Student Class के display() Function का पुनः उपयोग किया है तथा अपना नया Function marks() भी जोड़ा है। यही Inheritance द्वारा प्राप्त Code Reusability है।


Working of Inheritance

Create Base Class



Define Data Members & Functions



Create Derived Class



Inherit Base Class



Reuse Existing Members



Add New Features

Basic Example of Inheritance

नीचे दिया गया Program Inheritance का एक सरल उदाहरण है। इसमें Person Base Class है तथा Student Derived Class है। Student Class, Person Class के Data Members तथा Member Functions का उपयोग करती है और अपना अतिरिक्त Function भी प्रदान करती है।

#include<iostream> using namespace std; class Person { public: string name = "Rahul"; void showName() { cout << "Name : " << name << endl; } }; class Student : public Person { public: int roll = 101; void showRoll() { cout << "Roll Number : " << roll; } }; int main() { Student s; s.showName(); s.showRoll(); return 0; }

Output

Name : Rahul Roll Number : 101

ऊपर दिए गए Program में Student Class ने Person Class के showName() Function तथा name Variable को Inherit किया है तथा अपना स्वयं का roll Variable एवं showRoll() Function भी Define किया है।


Real Life Example of Inheritance

मान लीजिए किसी Vehicle Management System में सभी Vehicles की सामान्य Properties जैसे Brand, Model तथा Start() Function समान हैं। ऐसी स्थिति में Vehicle नाम की Base Class बनाई जाती है।

अब Car, Bike तथा Bus जैसी Classes, Vehicle Class से Inherit करती हैं। इससे प्रत्येक नई Class को Common Properties दोबारा लिखने की आवश्यकता नहीं होती तथा केवल अपनी विशेष Properties जोड़नी पड़ती हैं।

Vehicle (Base Class)



Car
Bike
Bus



Reuse Common Features

Types of Inheritance (Introduction)

C++ में आवश्यकता के अनुसार विभिन्न प्रकार के Inheritance का उपयोग किया जाता है। प्रत्येक Type का उद्देश्य अलग-अलग प्रकार के Class Relationships को दर्शाना होता है।

Type Description
Single Inheritance एक Derived Class केवल एक Base Class से Properties प्राप्त करती है।
Multiple Inheritance एक Derived Class एक से अधिक Base Classes से Inherit करती है।
Multilevel Inheritance एक Derived Class आगे किसी अन्य Class की Base Class बन जाती है।
Hierarchical Inheritance एक Base Class से अनेक Derived Classes बनाई जाती हैं।
Hybrid Inheritance दो या दो से अधिक Inheritance Types का Combination।

अगले भागों में प्रत्येक प्रकार के Inheritance को Programs एवं Diagrams सहित विस्तार से समझाया जाएगा।


Visibility Modes (Introduction)

Inheritance में Visibility Mode यह निर्धारित करता है कि Base Class के Members Derived Class में किस प्रकार उपलब्ध होंगे। C++ में मुख्य रूप से तीन Visibility Modes उपलब्ध हैं।

  • Public Inheritance
  • Protected Inheritance
  • Private Inheritance

Visibility Modes का प्रभाव Base Class के Public, Protected तथा Private Members पर पड़ता है। अगले भाग में इन तीनों Modes को Examples एवं Comparison Table सहित विस्तार से समझाया जाएगा।


Important Points

  • Inheritance C++ की प्रमुख Object Oriented Programming विशेषता है।
  • Base Class की Properties Derived Class में उपलब्ध होती हैं।
  • Derived Class अपने नए Members भी जोड़ सकती है।
  • Inheritance द्वारा Code Reusability तथा Maintainability बढ़ती है।
  • C++ में पाँच प्रमुख Types of Inheritance उपलब्ध हैं।
  • Visibility Modes यह निर्धारित करते हैं कि Base Class के Members किस प्रकार Inherit होंगे।
  • Large Scale Software Development में Inheritance का व्यापक उपयोग किया जाता है।

Advantages of Inheritance

Inheritance Object Oriented Programming का एक अत्यंत महत्वपूर्ण Feature है जो Existing Classes का पुनः उपयोग (Reuse) करने की सुविधा प्रदान करता है। इससे Program अधिक Organized, Efficient तथा Maintainable बनता है।

  • Code Reusability बढ़ती है।
  • Code Duplication कम हो जाती है।
  • Software Development का समय कम हो जाता है।
  • Program अधिक Modular बनता है।
  • Maintenance एवं Updating आसान हो जाती है।
  • Object Oriented Design को प्रभावी रूप से लागू किया जा सकता है।
  • Professional Software Development में Productivity बढ़ती है।

Limitations of Inheritance

यद्यपि Inheritance अत्यंत उपयोगी है, लेकिन इसका गलत अथवा अत्यधिक उपयोग Program की Complexity बढ़ा सकता है।

  • बहुत अधिक Inheritance Program को जटिल बना सकती है।
  • Base Class में परिवर्तन होने पर Derived Classes भी प्रभावित हो सकती हैं।
  • Improper Design से Tight Coupling उत्पन्न हो सकती है।
  • Multiple Inheritance में Ambiguity Problem उत्पन्न हो सकती है।
  • Program की Debugging कठिन हो सकती है।
  • सही Class Hierarchy Design करना आवश्यक होता है।

Applications of Inheritance

Application Purpose
Student Management System Student, Faculty एवं Staff Classes विकसित करना।
Banking Software Savings, Current एवं Loan Accounts बनाना।
Hospital Management Doctor, Patient एवं Staff Classes तैयार करना।
Vehicle Management Car, Bike एवं Bus Classes विकसित करना।
Game Development Player, Enemy तथा NPC Characters बनाना।
Employee Management Manager, Engineer एवं Clerk Classes तैयार करना।
E-Commerce Systems Product Categories को Organize करना।

Best Practices of Inheritance

  • Inheritance का उपयोग तभी करें जब वास्तविक IS-A Relationship हो।
  • Base Class को Generic तथा Reusable बनाएँ।
  • Derived Class में केवल आवश्यक Members ही जोड़ें।
  • अनावश्यक Deep Inheritance Hierarchy से बचें।
  • Meaningful Class Names का उपयोग करें।
  • Visibility Modes का सही चयन करें।
  • Readable एवं Maintainable Object Oriented Code लिखें।

Real Life Example

मान लीजिए किसी School Management System में Person नाम की Base Class बनाई गई है जिसमें Name, Age तथा Address जैसी सामान्य जानकारी उपलब्ध है। अब Student, Teacher तथा Principal नाम की Derived Classes उसी Base Class से Inherit करती हैं। प्रत्येक Derived Class Common Information का पुनः उपयोग करती है तथा अपनी अतिरिक्त Properties जैसे Roll Number, Subject अथवा Department जोड़ती है। इससे Code दोबारा लिखने की आवश्यकता नहीं होती तथा पूरा System अधिक व्यवस्थित एवं Maintainable बन जाता है।


Working of Inheritance

Create Base Class



Define Common Members



Create Derived Class



Inherit Base Class



Reuse Existing Members



Add New Features



Execute Program

Applications of Inheritance

Inheritance का उपयोग आधुनिक Object Oriented Programming में Code Reusability, Extensibility तथा Maintainability बढ़ाने के लिए किया जाता है। Enterprise Software, Banking Applications, Hospital Management Systems तथा Game Development जैसे क्षेत्रों में Inheritance का व्यापक उपयोग होता है।

Application Purpose
Student Management System Student, Teacher तथा Staff Classes विकसित करना।
Banking Software Savings Account, Current Account तथा Loan Account बनाना।
Hospital Management System Doctor, Nurse, Patient तथा Staff Classes तैयार करना।
Vehicle Management System Car, Bike, Bus तथा Truck Classes विकसित करना।
Employee Management System Manager, Engineer, HR तथा Clerk Classes तैयार करना।
Game Development Player, Enemy तथा NPC Characters को विकसित करना।
E-Commerce Applications Product Categories एवं User Types को Organize करना।

Advantages of Inheritance

  • Existing Code का पुनः उपयोग (Code Reusability) किया जा सकता है।
  • Program में Code Duplication कम हो जाती है।
  • Software Development का समय कम हो जाता है।
  • Program अधिक Modular तथा Organized बनता है।
  • Software को Maintain एवं Upgrade करना आसान हो जाता है।
  • Extensible Class Hierarchy विकसित की जा सकती है।
  • Object Oriented Programming के सिद्धांतों को प्रभावी रूप से लागू किया जा सकता है।

Limitations of Inheritance

  • अत्यधिक Inheritance Program की Complexity बढ़ा सकती है।
  • Base Class में परिवर्तन होने पर Derived Classes प्रभावित हो सकती हैं।
  • Multiple Inheritance में Ambiguity Problem उत्पन्न हो सकती है।
  • गलत Class Hierarchy Design करने पर Maintenance कठिन हो सकता है।
  • Improper Inheritance से Tight Coupling उत्पन्न हो सकती है।
  • Debugging एवं Testing अपेक्षाकृत कठिन हो सकती है।
  • हर स्थिति में Inheritance सर्वोत्तम समाधान नहीं होता।

Best Practices

  • Inheritance का उपयोग केवल वास्तविक IS-A Relationship होने पर करें।
  • Base Class को Generic एवं Reusable बनाएँ।
  • Derived Class में केवल आवश्यक Features ही जोड़ें।
  • अनावश्यक Deep Inheritance Hierarchy से बचें।
  • Visibility Modes का उचित चयन करें।
  • Meaningful Class एवं Member Names का उपयोग करें।
  • Readable तथा Maintainable Object Oriented Code लिखें।

Real Life Example

मान लीजिए किसी University Management System में Person नाम की Base Class बनाई गई है जिसमें Name, Age, Address तथा displayDetails() जैसे Common Members उपलब्ध हैं। अब Student, Teacher तथा Employee नाम की Derived Classes इन्हीं Common Members को Inherit करती हैं तथा अपनी अतिरिक्त Properties जैसे Roll Number, Subject, Salary आदि जोड़ती हैं। इससे Common Code केवल एक बार लिखा जाता है तथा पूरा System अधिक Efficient एवं Maintainable बन जाता है।


Working of Inheritance

Create Base Class



Define Common Data Members



Define Common Member Functions



Create Derived Class



Inherit Base Class



Reuse Existing Features



Add New Features



Execute Program

Summary

Inheritance C++ की सबसे महत्वपूर्ण Object Oriented Programming (OOP) विशेषताओं में से एक है। इसकी सहायता से एक नई Class, Existing Class की Properties तथा Member Functions को प्राप्त (Inherit) कर सकती है। जिस Class से Properties प्राप्त होती हैं उसे Base Class (Parent Class) तथा जो Class उन Properties को प्राप्त करती है उसे Derived Class (Child Class) कहा जाता है। Inheritance द्वारा Code Reusability, Maintainability तथा Extensibility बढ़ती है। C++ में मुख्य रूप से Single, Multiple, Multilevel, Hierarchical तथा Hybrid Inheritance उपलब्ध हैं। Base Class के Members किस प्रकार Derived Class में उपलब्ध होंगे, यह Visibility Modes (Public, Protected, Private) द्वारा निर्धारित किया जाता है।

Concept Description
Inheritance Existing Class की Properties को नई Class में प्राप्त करना।
Base Class जिस Class से Properties प्राप्त होती हैं।
Derived Class जो Class Base Class की Properties प्राप्त करती है।
Purpose Code Reusability एवं Maintainability बढ़ाना।
Types Single, Multiple, Multilevel, Hierarchical एवं Hybrid Inheritance।
Visibility Modes Public, Protected तथा Private।
Major Benefit Common Features का Reuse तथा Modular Programming।
Applications Banking, Hospital, Education, ERP, Game Development तथा Enterprise Software।

Important Viva Questions

  1. Inheritance क्या है?
  2. Base Class तथा Derived Class में क्या अंतर है?
  3. Inheritance की आवश्यकता क्यों होती है?
  4. Inheritance के मुख्य Advantages क्या हैं?
  5. C++ में कितने प्रकार के Inheritance होते हैं?
  6. Single Inheritance क्या है?
  7. Multiple Inheritance क्या है?
  8. Visibility Modes क्या होते हैं?
  9. Public Inheritance तथा Private Inheritance में क्या अंतर है?
  10. Inheritance द्वारा Code Reusability कैसे प्राप्त होती है?

University Examination Important Questions

  1. Inheritance क्या है? उदाहरण सहित समझाइए।
  2. Base Class तथा Derived Class को उदाहरण सहित समझाइए।
  3. Inheritance की आवश्यकता एवं Advantages लिखिए।
  4. C++ में उपलब्ध सभी Types of Inheritance को उदाहरण सहित समझाइए।
  5. Public, Protected तथा Private Visibility Modes में अंतर स्पष्ट कीजिए।
  6. Inheritance द्वारा Code Reusability कैसे प्राप्त होती है?
  7. Inheritance के Applications लिखिए।
  8. Inheritance की Limitations लिखिए।
  9. Vehicle Management System अथवा Student Management System का उदाहरण देकर Inheritance समझाइए।
  10. Object Oriented Programming में Inheritance की भूमिका स्पष्ट कीजिए।

Exam Tips

  • हमेशा याद रखें कि Inheritance का मुख्य उद्देश्य Code Reusability है।
  • Base Class से Properties प्राप्त होती हैं तथा Derived Class उन्हें उपयोग करती है।
  • C++ में पाँच प्रमुख Types of Inheritance उपलब्ध हैं।
  • Visibility Modes परीक्षा में अत्यंत महत्वपूर्ण हैं, इसलिए Public, Protected तथा Private Inheritance का अंतर अवश्य याद रखें।
  • Inheritance का उपयोग तभी करें जब वास्तविक IS-A Relationship हो।
  • Inheritance Programs में Base Class एवं Derived Class का संबंध Diagram सहित बनाना अधिक अंक दिलाता है।
  • RGPV Practical एवं Viva में Single Inheritance, Multilevel Inheritance तथा Visibility Modes आधारित Programs अक्सर पूछे जाते हैं।

Conclusion

Inheritance C++ Object Oriented Programming का अत्यंत महत्वपूर्ण Concept है जो Existing Classes की Properties एवं Behaviors को नई Classes में Reuse करने की सुविधा प्रदान करता है। इससे Code Duplication कम होती है, Software Development तेज़ होता है तथा Program अधिक Modular एवं Maintainable बनता है। Banking Software, Hospital Management Systems, School ERP, Vehicle Management Systems, Enterprise Applications तथा Game Development जैसे अनेक Real World Projects में Inheritance का व्यापक उपयोग किया जाता है। 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 →

Function Overriding in C++ Notes | Method Overriding, Runtime Polymorphism & Examples | Basic Computer Engineering | RGPV BTech First Year

Functio...

Read More →