Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟣 OOP  ·  Lesson 45

Inheritance in C++

Inheritance क्या है?

Inheritance एक class को दूसरी पर बनाने देता है। मौजूदा class base है; नई derived class है, जो base के members पाती है और व्यवहार जोड़ या बदल सकती है। यह एक "is-a" संबंध पकड़ता है — Dog एक Animal है — तो साझा code एक जगह रहता है।

Base और derived classes

C++
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 करता है।

एक चलता उदाहरण

C++
int main() {
    Dog d;
    d.eat();    // Animal se inherited
    d.bark();   // Dog ka apna
    return 0;
}
Output:
Eating
Woof

Dog object अपने bark() और inherited eat() दोनों इस्तेमाल कर सकता है।

Access और inheritance modes

Derived class base के public और protected members देखती है, पर उसके private नहीं। Inheritance mode भी मायने रखता है:

ModePublic base members बनते हैं
publicpublic (is-a मॉडल) — सबसे आम
protectedprotected (दुर्लभ)
privateprivate (दुर्लभ)

Base में उस data के लिए protected इस्तेमाल करें जिस तक subclasses पहुँचें। access specifiers देखें।

Constructor क्रम

जब derived object बनता है, base constructor पहले चलता है, फिर derived। Destructors इसे उल्टा करते हैं। आप derived से base constructor को arguments भेज सकते हैं:

C++
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 को फिर से परिभाषित करके उसका व्यवहार बदल सकती है:

C++
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 के private members 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 क्या है?
Inheritance एक नई class को, जिसे derived class कहते हैं, किसी मौजूदा class के, जिसे base class कहते हैं, members दोबारा इस्तेमाल करने और व्यवहार जोड़ने या बदलने देता है। यह "is-a" संबंध मॉडल करता है — Dog एक Animal है — और संबंधित classes में साझा code दोहराने से बचाता है।
Base class और derived class में क्या अंतर है?
Base class वह मौजूदा class है जिसके members inherit होते हैं, और derived class वह नई class है जो इससे inherit करती है। Derived class को base के public और protected members मिलते हैं और वह अपने जोड़ सकती या inherited functions override कर सकती है।
C++ में inheritance के access modes क्या हैं?
Inheritance public, protected या private हो सकती है, जो नियंत्रित करती है कि base के members derived class में कैसे दिखें। Public inheritance public members को public रखती है और "is-a" मॉडल करती है; यह अब तक सबसे आम है, जबकि protected और private inheritance कम इस्तेमाल होती हैं।
C++ में inheritance के साथ constructors किस क्रम में call होते हैं?
Base class constructor पहले चलता है, फिर derived class constructor। Destructors उल्टे क्रम में चलते हैं — पहले derived, फिर base — तो हर class अपने जोड़ से पहले सेट होती है और उनके बाद सफ़ाई करती है।
क्या C++ में derived class base class के private members access कर सकती है?
नहीं। Derived class base class के private members को सीधे access नहीं कर सकती; वे छिपे रहते हैं। यह base के public और protected members access कर सकती है, तो subclasses के लिए बना data आमतौर पर protected घोषित होता है।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicMultiple Inheritance आपकी प्रगति: 45 / 81 (56%) · Lesson 46 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें