Inheritance in C++
Inheritance क्या है?
Inheritance एक class को दूसरी पर बनाने देता है। मौजूदा class base है; नई derived class है, जो base के members पाती है और व्यवहार जोड़ या बदल सकती है। यह एक "is-a" संबंध पकड़ता है — Dog एक Animal है — तो साझा code एक जगह रहता है।
Base और derived classes
class Animal { // base
public:
void eat() { cout << "Eating\n"; }
};
class Dog : public Animal { // derived: Dog is-a Animal
public:
void bark() { cout << "Woof\n"; }
};: public Animal भाग कहता है Dog, Animal से publicly inherit करता है।
एक चलता उदाहरण
int main() {
Dog d;
d.eat(); // Animal se inherited
d.bark(); // Dog ka apna
return 0;
}Eating
Woof
Dog object अपने bark() और inherited eat() दोनों इस्तेमाल कर सकता है।
Access और inheritance modes
Derived class base के public और protected members देखती है, पर उसके private नहीं। Inheritance mode भी मायने रखता है:
| Mode | Public base members बनते हैं |
|---|---|
public | public (is-a मॉडल) — सबसे आम |
protected | protected (दुर्लभ) |
private | private (दुर्लभ) |
Base में उस data के लिए protected इस्तेमाल करें जिस तक subclasses पहुँचें। access specifiers देखें।
Constructor क्रम
जब derived object बनता है, base constructor पहले चलता है, फिर derived। Destructors इसे उल्टा करते हैं। आप derived से base constructor को arguments भेज सकते हैं:
class Animal {
public:
Animal(string n) { cout << n << " created\n"; }
};
class Dog : public Animal {
public:
Dog() : Animal("Dog") { } // base constructor call
};Function override करना
Derived class inherited function को फिर से परिभाषित करके उसका व्यवहार बदल सकती है:
class Animal { public: void sound() { cout << "Some sound\n"; } };
class Cat : public Animal {
public:
void sound() { cout << "Meow\n"; } // override karta hai
};Base-class pointers के ज़रिए काम करने वाले overriding के लिए, आपको virtual functions और polymorphism चाहिए।
आम गलतियाँ
- Derived class से base के
privatemembers access करने की कोशिश। - यह भूलना कि base constructor derived से पहले चलता है।
- "has-a" संबंध के लिए inheritance इस्तेमाल करना जहाँ composition बेहतर बैठता है।
virtualबिना base pointer के ज़रिए overridden function के चुने जाने की उम्मीद।
एक Vehicle base बनाएँ जिसमें start() function हो, और Car तथा Bike derive करें जो हर एक अपना function जोड़ें। Objects बनाएँ और inherited तथा अपने दोनों functions call करें।
सारांश
- Inheritance derived class को base class दोबारा इस्तेमाल और विस्तारित करने देता है।
- यह "is-a" संबंध मॉडल करता है और code दोहराव से बचाता है।
- Derived classes public/protected base members देखती हैं, private नहीं।
- Base constructor पहले चलता है; destructors उल्टे चलते हैं।
- Derived classes inherited functions override कर सकती हैं।
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C++ में inheritance क्या है?
Base class और derived class में क्या अंतर है?
C++ में inheritance के access modes क्या हैं?
public, protected या private हो सकती है, जो नियंत्रित करती है कि base के members derived class में कैसे दिखें। Public inheritance public members को public रखती है और "is-a" मॉडल करती है; यह अब तक सबसे आम है, जबकि protected और private inheritance कम इस्तेमाल होती हैं।C++ में inheritance के साथ constructors किस क्रम में call होते हैं?
क्या C++ में derived class base class के private members access कर सकती है?
private members को सीधे access नहीं कर सकती; वे छिपे रहते हैं। यह base के public और protected members access कर सकती है, तो subclasses के लिए बना data आमतौर पर protected घोषित होता है।