Theory
एक ही class को दो बार लिखना
Canteen membership cards launch करता है। आप एक Student class लिखते हैं: name, ID, show(), plus एक 10% student discount। फिर staff को cards चाहिए, तो आप file copy करते हैं, नाम बदलकर Staff करते हैं, discount बदलकर 20% करते हैं।
अगले हफ़्ते college आपसे एक phone number जोड़ने को कहता है। अब आप दो files edit करते हैं, और एक भूल जाते हैं। दो महीने बाद पाँच card types हैं और हर change का मतलब है पाँच edits।
Copy-paste reuse नहीं है। C++ के पास असली reuse है: inheritance।
Theory
Family property
एक बच्चा family house को दोबारा नहीं बनाता; वे इसे inherit करते हैं और एक floor जोड़ते हैं।
C++ में, एक derived class वह सब कुछ inherit करती है जो base class के पास है (इसके members) और फिर वह जोड़ती या adjust करती है जो इसे ख़ास बनाता है। Student और Staff दोनों Person से name, ID और show() inherit करते हैं; हर एक सिर्फ़ अपना discount rule जोड़ता है। एक house, कई floors, कोई duplicate bricks नहीं।
Theory
Inheritance, formally
Inheritance वह mechanism है जिससे एक class (derived या child class) दूसरी class (base या parent class) की properties और behaviours acquire करती है।
Syntax:
class Student : public Person { ... };
Colon को "एक तरह का है" पढ़िए। वहाँ public inheritance mode है: public inheritance के साथ, base के public members child में public रहते हैं।
दो फ़ायदे: reusability (एक बार लिखिए, हर child में इस्तेमाल कीजिए) और hierarchy (code real-world is-a relationships को mirror करता है)।
Practical
Person एक बार, cards हमेशा के लिए
#include <iostream>
#include <cstring>
using namespace std;
class Person { // base class
protected:
char name[30];
public:
void setName(const char n[]) { strcpy(name, n); }
void show() { cout << "Card holder: " << name << endl; }
};
class Student : public Person { // derived: single inheritance
public:
double discount() { return 0.10; }
};
class Staff : public Person { // Person now has two children
public:
double discount() { return 0.20; }
};
int main() {
Student s;
s.setName("Riya"); // inherited from Person
s.show(); // inherited from Person
cout << s.discount();// Student's own addition
return 0;
}
At a glance
Inheritance के पाँच types
| Type | Shape | Example |
|---|---|---|
| Single | A → B | Person → Student |
| Multilevel | A → B → C (chain) | Person → Student → CommitteeStudent |
| Multiple | A + B → C (दो parents) | Student + SportsMember → Captain |
| Hierarchical | A → B, A → C (एक parent, कई children) | Person → Student, Staff, Guest |
| Hybrid | ऊपर वालों का mix | Hierarchical + multiple combined |
Think first
वह shape नाम दीजिए
Canteen की classes बढ़ती हैं: Person → Student, Person → Staff, और Student → CommitteeStudent। tap करने से पहले: इस diagram में कौन से DO inheritance types हैं?
Show the answer
Hierarchical (Person के दो children हैं, Student और Staff) और multilevel (chain Person → Student → CommitteeStudent)। क्योंकि एक structure में दो pure types combine हुए हैं, पूरी picture hybrid qualify करती है। Exams अक्सर बिल्कुल ऐसा एक figure खींचते हैं और आपसे मौजूद हर type नाम करने को कहते हैं, तो chains AND fan-outs दोनों scan कीजिए।
Quiz
class Person के पास private: char name[30] है। class Student : public Person अपने member function के अंदर सीधे name इस्तेमाल करने की कोशिश करता है। क्या होता है?
- Compile error: private members inherited तो होते हैं पर derived class में accessible नहीं होते
- काम करता है: children parent की हर चीज़ access कर सकते हैं
- Runtime error: name exist करता है पर garbage रखता है
- यह सिर्फ़ तभी काम करता है अगर Student, Person के साथ उसी file में है
Show the answer
Compile error: private members inherited तो होते हैं पर derived class में accessible नहीं होते
Private children के लिए भी private है: वह member Student object के अंदर exist करता है पर compiler Student के code से सीधी access refuse करता है। Standard fix बिल्कुल इसीलिए protected exist करता है (class और इसकी derived classes के लिए accessible, बाहर से अभी भी hidden)। Option B वही misconception है जिसे यह सवाल पकड़ता है; file placement (option D) access control में कोई role नहीं निभाता।
Watch out
Marks लायक़ दो traps
Private इस्तेमाल के लिए आगे pass नहीं होता: लिखिए "private members inherited होते हैं पर derived class में accessible नहीं; protected इसे solve करता है" और आप वह point collect करते हैं जो exams ढूँढते हैं।
Multiple बनाम multilevel confusion: multiple = एक साथ दो parents (C++ में allowed, मशहूर तौर पर Java में नहीं); multilevel = grandparent chain। Hybrid inheritance diamond problem बना सकता है (एक grandparent दो paths से पहुँचा गया); उस risk को नाम देना bonus-mark territory है।
Theory
अगले lessons को यह क्यों चाहिए
Inheritance वह stage है जिस पर बाक़ी Unit 2 और पूरा Unit 3 perform करता है: जब एक child पैदा होता है constructors base-first run होते हैं, और polymorphism (एक discount() call जो Student और Staff के लिए अलग व्यवहार करती है) सिर्फ़ इसलिए exist करता है क्योंकि दोनों children Person base share करते हैं। Card family याद रखिए; यह बाक़ी हर OOP lesson में लौटती है।
Summary
Key takeaways
- Inheritance: एक derived class base class के members acquire करती है और उन्हें extend करती है।
- Syntax: class Child : public Parent { }; public inheritance public members को public रखती है।
- पाँच types: single, multilevel (chain), multiple (दो parents), hierarchical (fan-out), hybrid (mix)।
- Private members inherited होते हैं पर child के लिए inaccessible; protected इसका fix है।
- C++ में multiple inheritance legal है (Java में नहीं); hybrid diamond problem cause कर सकता है।
- फ़ायदे: reusability और real-world is-a hierarchies।
- Memory hook: house inherit कीजिए, एक floor जोड़िए।