Introduction to Inheritance
Building new classes on top of existing ones instead of starting from scratch.
Introduction to Inheritance
Inheritance Object-Oriented Programming (OOP) का एक महत्वपूर्ण feature है, जिसमें एक class किसी दूसरी existing class के properties (गुण) और member functions (सदस्य functions) को प्राप्त कर सकती है। इससे existing code को reuse (पुनः उपयोग) करना आसान होता है।
Basic Idea of Inheritance
Inheritance में जिस class से properties और functions प्राप्त किए जाते हैं, उसे Base Class या Parent Class कहा जाता है। जिस class को ये properties और functions प्राप्त होते हैं, उसे Derived Class या Child Class कहा जाता है।
Parent Class → Child Class
उदाहरण के लिए, यदि Animal एक base class है और Dog एक derived class है, तो Dog class Animal के accessible properties और member functions को inherit कर सकती है।
Why is Inheritance Used?
Inheritance का मुख्य उद्देश्य code reusability (code का पुनः उपयोग) है। यदि किसी class में पहले से कोई common functionality उपलब्ध है, तो उसे दोबारा लिखने के बजाय दूसरी class में inherit किया जा सकता है।
Inheritance के प्रमुख लाभ निम्नलिखित हैं:
- Code को reuse किया जा सकता है।
- Program में duplication कम होता है।
- Classes के बीच relationship स्थापित किया जा सकता है।
- Program को maintain करना आसान होता है।
- Existing class की functionality को आगे बढ़ाया जा सकता है।
Base Class and Derived Class
Inheritance में दो मुख्य classes होती हैं:
| Term | अर्थ |
|---|---|
| Base Class | वह class जिससे properties और member functions inherit किए जाते हैं। |
| Derived Class | वह class जो base class से properties और member functions प्राप्त करती है। |
Syntax of Inheritance
C++ में inheritance को colon : के माध्यम से define किया जाता है।
class DerivedClass : accessSpecifier BaseClass
{
// Members of derived class
};
यहाँ DerivedClass base class से inherit करती है और accessSpecifier के रूप में public, protected या private का उपयोग किया जा सकता है।
Simple Example of Inheritance
निम्न example में Student class, Person class से inherit करती है:
#include <iostream>
using namespace std;
class Person
{
public:
string name;
void displayName()
{
cout << "Name: " << name << endl;
}
};
class Student : public Person
{
public:
int rollNo;
void displayRollNo()
{
cout << "Roll No: " << rollNo;
}
};
int main()
{
Student s;
s.name = "Rahul";
s.rollNo = 101;
s.displayName();
s.displayRollNo();
return 0;
}
Name: Rahul
Roll No: 101
यहाँ Person base class है और Student derived class है। Student class ने Person class के name data member और displayName() function को inherit किया है।
Inheritance Represents an "IS-A" Relationship
Inheritance सामान्यतः IS-A relationship को represent करता है। इसका अर्थ है कि derived class का object base class का भी एक प्रकार है।
उदाहरण:
Student IS-A Person
Car IS-A Vehicle
इस प्रकार inheritance classes के बीच logical relationship स्थापित करने में सहायता करता है।
Accessing Base Class Members
यदि base class के members public हैं और inheritance public mode में किया गया है, तो derived class के object के माध्यम से उन members को access किया जा सकता है।
#include <iostream>
using namespace std;
class Vehicle
{
public:
void start()
{
cout << "Vehicle Started" << endl;
}
};
class Car : public Vehicle
{
public:
void drive()
{
cout << "Car is Moving";
}
};
int main()
{
Car c;
c.start();
c.drive();
return 0;
}
Vehicle Started
Car is Moving
यहाँ Car class ने Vehicle class के start() function को inherit किया है। इसलिए Car object के माध्यम से start() function को access किया जा सकता है।
Adding New Members to Derived Class
Derived class inherited members के साथ अपने नए data members और member functions भी define कर सकती है।
#include <iostream>
using namespace std;
class Person
{
public:
string name;
void showName()
{
cout << "Name: " << name << endl;
}
};
class Student : public Person
{
public:
int marks;
void showMarks()
{
cout << "Marks: " << marks;
}
};
int main()
{
Student s;
s.name = "Amit";
s.marks = 90;
s.showName();
s.showMarks();
return 0;
}
Name: Amit
Marks: 90
यहाँ Student class ने Person class से name और showName() inherit किया है तथा स्वयं marks और showMarks() define किए हैं।
Types of Inheritance
C++ में inheritance को class relationship और inheritance structure के आधार पर विभिन्न प्रकारों में समझा जाता है। प्रमुख प्रकार हैं:
- Single Inheritance
- Multilevel Inheritance
- Multiple Inheritance
- Hierarchical Inheritance
- Hybrid Inheritance
इन सभी प्रकारों का उपयोग अलग-अलग class relationships को represent करने के लिए किया जाता है।
Single Inheritance
जब एक derived class केवल एक base class से inherit करती है, तो इसे Single Inheritance कहा जाता है।
Base Class → Derived Class
#include <iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout << "Animal Eats" << endl;
}
};
class Dog : public Animal
{
public:
void bark()
{
cout << "Dog Barks";
}
};
int main()
{
Dog d;
d.eat();
d.bark();
return 0;
}
Animal Eats
Dog Barks
Multilevel Inheritance
जब एक derived class किसी दूसरी derived class के लिए base class बन जाती है और inheritance एक chain के रूप में आगे बढ़ता है, तो इसे Multilevel Inheritance कहा जाता है।
Class A → Class B → Class C
#include <iostream>
using namespace std;
class Animal
{
public:
void eat()
{
cout << "Eating" << endl;
}
};
class Dog : public Animal
{
public:
void bark()
{
cout << "Barking" << endl;
}
};
class Puppy : public Dog
{
public:
void play()
{
cout << "Playing";
}
};
int main()
{
Puppy p;
p.eat();
p.bark();
p.play();
return 0;
}
Eating
Barking
Playing
Multiple Inheritance
जब एक derived class एक से अधिक base classes से inherit करती है, तो इसे Multiple Inheritance कहा जाता है।
Base Class A + Base Class B → Derived Class
#include <iostream>
using namespace std;
class Father
{
public:
void fatherProperty()
{
cout << "Father's Property" << endl;
}
};
class Mother
{
public:
void motherProperty()
{
cout << "Mother's Property" << endl;
}
};
class Child : public Father, public Mother
{
public:
void ownProperty()
{
cout << "Child's Property";
}
};
int main()
{
Child c;
c.fatherProperty();
c.motherProperty();
c.ownProperty();
return 0;
}
Father's Property
Mother's Property
Child's Property
Hierarchical Inheritance
जब एक ही base class से एक से अधिक derived classes inherit करती हैं, तो इसे Hierarchical Inheritance कहा जाता है।
Base Class
/ \
Derived A Derived B
Hybrid Inheritance
जब एक program में inheritance के एक से अधिक types का combination होता है, तो उसे Hybrid Inheritance कहा जाता है।
Hybrid inheritance में complex class relationships बन सकते हैं। ऐसे cases में ambiguity को दूर करने के लिए C++ में appropriate access और design techniques का उपयोग किया जाता है।
Access Specifiers and Inheritance
C++ में inheritance करते समय public, protected और private access modes का उपयोग किया जा सकता है। ये निर्धारित करते हैं कि base class के inherited members derived class में किस access level पर उपलब्ध होंगे।
| Inheritance Mode | Base public member | Base protected member |
|---|---|---|
| Public | Public | Protected |
| Protected | Protected | Protected |
| Private | Private | Private |
Base class के private members derived class से directly accessible नहीं होते। हालांकि वे base class के object का हिस्सा बने रहते हैं और base class के public या protected member functions के माध्यम से access किए जा सकते हैं।
Inheritance and Code Reusability
Inheritance का सबसे महत्वपूर्ण लाभ code reusability है। यदि कई classes में common functionality है, तो उसे एक base class में रखा जा सकता है और आवश्यक derived classes में inherit किया जा सकता है।
#include <iostream>
using namespace std;
class Employee
{
public:
void login()
{
cout << "Employee Login" << endl;
}
};
class Teacher : public Employee
{
public:
void teach()
{
cout << "Teacher is Teaching";
}
};
int main()
{
Teacher t;
t.login();
t.teach();
return 0;
}
Employee Login
Teacher is Teaching
यहाँ login() function को Employee class में केवल एक बार define किया गया है और Teacher class ने इसे inherit करके reuse किया है।
Important Points
- Inheritance OOP का एक महत्वपूर्ण feature है।
- Inheritance का उपयोग existing class की functionality को reuse करने के लिए किया जाता है।
- जिस class से members inherit किए जाते हैं उसे Base Class कहते हैं।
- जो class members inherit करती है उसे Derived Class कहते हैं।
- C++ में inheritance के लिए colon : का उपयोग किया जाता है।
- Public inheritance में base class के public members derived class में public और protected members protected रहते हैं।
- Base class के private members derived class से directly accessible नहीं होते।
- Inheritance classes के बीच IS-A relationship को represent कर सकता है।
- Single, Multilevel, Multiple, Hierarchical और Hybrid inheritance प्रमुख प्रकार हैं।
- Inheritance code duplication को कम करने में सहायता करता है।
Board Focus
Inheritance → Existing class की properties/functions को दूसरी class में प्राप्त करना
Base Class → Parent Class
Derived Class → Child Class
Main Purpose → Code Reusability
Syntax → class Derived : public Base
Relationship → IS-A Relationship
Single → One Base, One Derived
Multilevel → Class A → Class B → Class C
Multiple → Multiple Base Classes → One Derived Class
Hierarchical → One Base → Multiple Derived Classes
Board Important Questions
Very Short Answer Questions
Q1. Inheritance क्या है?
Answer: एक class द्वारा दूसरी class के properties और member functions को प्राप्त करने की प्रक्रिया Inheritance कहलाती है।
Q2. Base Class क्या है?
Answer: वह class जिससे दूसरी class properties और member functions inherit करती है, Base Class कहलाती है।
Q3. Derived Class क्या है?
Answer: वह class जो किसी base class से properties और member functions inherit करती है, Derived Class कहलाती है।
Q4. Inheritance का मुख्य उद्देश्य क्या है?
Answer: Inheritance का मुख्य उद्देश्य code reusability और existing functionality को reuse करना है।
Q5. C++ में inheritance के लिए किस symbol का उपयोग किया जाता है?
Answer: Colon : symbol का उपयोग किया जाता है।
Q6. Single Inheritance क्या है?
Answer: जब एक derived class केवल एक base class से inherit करती है, तो उसे Single Inheritance कहते हैं।
Q7. Multiple Inheritance क्या है?
Answer: जब एक derived class एक से अधिक base classes से inherit करती है, तो उसे Multiple Inheritance कहते हैं।
Short Answer Questions
Q8. Inheritance के लाभ लिखिए।
Answer: Inheritance से code reuse किया जा सकता है, code duplication कम होता है, classes के बीच relationship स्थापित किया जा सकता है और program को maintain तथा extend करना आसान होता है।
Q9. Base Class और Derived Class में अंतर बताइए।
| Base Class | Derived Class |
|---|---|
| जिस class से members inherit किए जाते हैं। | जो class members inherit करती है। |
| इसे Parent Class भी कहते हैं। | इसे Child Class भी कहते हैं। |
| इसमें common properties और functions रखे जा सकते हैं। | इसमें inherited members के साथ नए members भी हो सकते हैं। |
Q10. Single और Multilevel Inheritance में अंतर बताइए।
| Single Inheritance | Multilevel Inheritance |
|---|---|
| एक base class और एक derived class होती है। | Inheritance कई levels में होता है। |
| Structure: A → B | Structure: A → B → C |
| एक direct inheritance relationship होता है। | Derived class आगे दूसरी class की base बन सकती है। |
Q11. Inheritance में public, protected और private access modes का क्या महत्व है?
Answer: ये access modes निर्धारित करते हैं कि base class के inherited public और protected members derived class में किस access level पर उपलब्ध होंगे। Base class के private members derived class से directly accessible नहीं होते।
Long Answer Questions
Q12. Inheritance को C++ program की सहायता से समझाइए।
Answer: Inheritance OOP का feature है जिसमें derived class, base class के accessible members को प्राप्त कर सकती है। इसका मुख्य उद्देश्य code reusability है।
#include <iostream>
using namespace std;
class Person
{
public:
string name;
void showName()
{
cout << "Name: " << name << endl;
}
};
class Student : public Person
{
public:
int rollNo;
void showRollNo()
{
cout << "Roll No: " << rollNo;
}
};
int main()
{
Student s;
s.name = "Ravi";
s.rollNo = 101;
s.showName();
s.showRollNo();
return 0;
}
Name: Ravi
Roll No: 101
यहाँ Person base class है और Student derived class है। Student class ने Person के name और showName() को inherit किया है तथा अपने rollNo और showRollNo() members भी define किए हैं।
Q13. Inheritance के विभिन्न प्रकारों को समझाइए।
Answer: C++ में inheritance के प्रमुख प्रकार Single, Multilevel, Multiple, Hierarchical और Hybrid हैं। Single inheritance में एक derived class एक base class से inherit करती है। Multilevel inheritance में inheritance chain के रूप में होता है। Multiple inheritance में एक derived class कई base classes से inherit करती है। Hierarchical inheritance में एक base class से कई derived classes inherit करती हैं। Hybrid inheritance में inheritance के एक से अधिक प्रकारों का combination होता है।
Q14. Multiple Inheritance को program की सहायता से समझाइए।
#include <iostream>
using namespace std;
class A
{
public:
void showA()
{
cout << "Class A" << endl;
}
};
class B
{
public:
void showB()
{
cout << "Class B" << endl;
}
};
class C : public A, public B
{
public:
void showC()
{
cout << "Class C";
}
};
int main()
{
C obj;
obj.showA();
obj.showB();
obj.showC();
return 0;
}
Class A
Class B
Class C
यहाँ C class ने A और B दोनों classes से inheritance प्राप्त किया है। इसलिए C के object के माध्यम से A और B के accessible member functions को access किया जा सकता है।
Q15. Inheritance code reusability में किस प्रकार सहायता करता है?
Answer: Inheritance में common functionality को base class में define किया जाता है। Derived classes उस functionality को दोबारा लिखे बिना inherit करके उपयोग कर सकती हैं। इससे code duplication कम होता है और program को maintain करना आसान होता है।
Quick Revision
- Inheritance: Existing class से members प्राप्त करने की प्रक्रिया
- Base Class: Parent class
- Derived Class: Child class
- Main Benefit: Code reusability
- Syntax: class Derived : public Base
- Single: One base → One derived
- Multilevel: A → B → C
- Multiple: Multiple bases → One derived
- Hierarchical: One base → Multiple derived
- Hybrid: Combination of inheritance types
Practice Questions
- Inheritance क्या है?
- Base Class और Derived Class से आप क्या समझते हैं?
- Inheritance का मुख्य उद्देश्य क्या है?
- Inheritance के प्रमुख लाभ लिखिए।
- C++ में inheritance का syntax लिखिए।
- Single Inheritance क्या है?
- Multilevel Inheritance को उदाहरण सहित समझाइए।
- Multiple Inheritance क्या है?
- Hierarchical Inheritance क्या है?
- Hybrid Inheritance से आप क्या समझते हैं?
- Base Class और Derived Class में अंतर बताइए।
- Inheritance code reusability में कैसे सहायता करता है?
- Single और Multiple Inheritance में अंतर बताइए।
- Multiple Inheritance का C++ program लिखिए।
- Inheritance का उपयोग करके Person और Student classes का program लिखिए।