BCA / B.Tech 9 min read

Inheritance in C++ in Hindi | C++ में Inheritance हिंदी में

RK

लेखक Ratan Kumawat · संस्थापक, NotesMedia

प्रकाशित:

त्वरित सारांश और मुख्य बिंदु

  • • विषय फोकस: C++ Notes in Hindi | CPP नोट्स हिंदी में (BCA / B.Tech Syllabus)
  • • मुख्य विषय: Inheritance in C++ in Hindi | C++ में Inheritance हिंदी में - General Topics
  • • अवलोकन: Quick Answer: Inheritance in C++ वह feature है जिसमें एक नई class (derived class) किसी पहले से बनी class (base class) के data members और member function...
  • • मुख्य अवधारणाएं: Inheritance in C++ in Hindi, C++ में Inheritance हिंदी में, what is inheritance in C++ in hindi, Inheritance के प्रकार C++,Inheritance in C++, C++ Inheritance in English, what is inheritance in C++ in english, Types of Inheritance in C++
Quick Answer: Inheritance in C++ वह feature है जिसमें एक नई class (derived class) किसी पहले से बनी class (base class) के data members और member functions को अपना लेती है। Syntax है class Derived : access-mode Base { };। C++ में पाँच types होते हैं: single, multiple, multilevel, hierarchical और hybrid। Diamond problem को virtual base class से solve किया जाता है।

Inheritance Meaning in Hindi (C++ में)

Inheritance का हिंदी मतलब है विरासत, यानी जो चीज़ पिछली पीढ़ी से अपने आप मिलती है। C++ में भी यही होता है: derived class (child) को base class (parent) के members बिना दोबारा लिखे मिल जाते हैं, और वह अपने नए members भी जोड़ सकती है।

Example: Vehicle class में speed और wheels हैं। Car और Bike इन्हें दोबारा लिखने की जगह Vehicle से inherit कर लेती हैं। इसे IS-A relationship कहते हैं: Car IS-A Vehicle।

Class और object की basics पहले साफ़ न हों तो यह पढ़ें: Class and Objects in Hindi.

Advertisement

Inheritance in C++: Syntax और Single Inheritance Example

class DerivedClass : access-mode BaseClass {
    // derived class के अपने members
};

Access-mode public, protected या private हो सकता है। अगर आप कुछ न लिखें, तो class के लिए default private होता है और struct के लिए public।

#include <iostream>
using namespace std;

class Employee {
protected:
    int salary;
public:
    Employee() { salary = 30000; }
};

class Manager : public Employee {      // single inheritance
    int bonus;
public:
    Manager() { bonus = 5000; }
    void show() {
        cout << "Salary: " << salary << endl;   // base का protected member
        cout << "Bonus: " << bonus << endl;
        cout << "Total: " << salary + bonus << endl;
    }
};

int main() {
    Manager m;
    m.show();
    return 0;
}

Output:

Salary: 30000
Bonus: 5000
Total: 35000

salary protected है, इसलिए Manager के function उसे use कर सकते हैं, पर main() में m.salary लिखने पर error आएगा।

Access Modes: Public, Protected और Private Inheritance

Inheritance का mode तय करता है कि base के members derived class में किस access level पर पहुँचेंगे। Exam में यह table सबसे ज़्यादा पूछी जाती है:

Base class में memberpublic inheritanceprotected inheritanceprivate inheritance
publicpublicprotectedprivate
protectedprotectedprotectedprivate
privateaccessible नहींaccessible नहींaccessible नहीं

याद रखें: base का private member किसी भी mode में derived class में सीधे access नहीं होता। उसे इस्तेमाल करना हो तो base में public/protected function बनाइए। Access specifiers detail में: Access Specifiers in Hindi.

Types of Inheritance in C++

Single      Multiple       Multilevel    Hierarchical      Hybrid
  A          A     B           A              A               A
  |           \   /            |            / | \            / \
  B             C              B           B  C  D          B   C
                               |                             \ /
                               C                              D
TypeमतलबExample
Singleएक derived, एक baseEmployee → Manager
Multipleएक derived, दो या ज़्यादा baseMother, Father → Child
MultilevelDerived class आगे किसी की base बनती हैA → B → C
Hierarchicalएक base से कई derivedShape → Circle, Square
Hybridदो या ज़्यादा types का mixHierarchical + Multiple (diamond)

1. Single Inheritance

एक derived class सिर्फ एक base class से inherit करती है। ऊपर वाला Employee → Manager example इसी का है।

2. Multiple Inheritance in C++

एक derived class दो या ज़्यादा base classes से inherit करती है। यह C++ की खास बात है; Java और C# में classes से यह नहीं होता। Base classes के constructors उसी order में चलते हैं जिस order में वे class declaration में लिखी गई हैं।

#include <iostream>
using namespace std;

class Mother {
public:
    Mother() { cout << "Mother constructor" << endl; }
    void cooking() { cout << "Child can cook" << endl; }
};

class Father {
public:
    Father() { cout << "Father constructor" << endl; }
    void driving() { cout << "Child can drive" << endl; }
};

class Child : public Mother, public Father {   // Mother पहले लिखी है
public:
    Child() { cout << "Child constructor" << endl; }
};

int main() {
    Child c;
    c.cooking();
    c.driving();
    return 0;
}

Output:

Mother constructor
Father constructor
Child constructor
Child can cook
Child can drive

Ambiguity in Multiple Inheritance: अगर Mother और Father दोनों में show() नाम का function हो, तो c.show() लिखने पर compile error आता है ("ambiguous")। इसे scope resolution operator से solve करते हैं: c.Mother::show(); या c.Father::show();।

3. Multilevel Inheritance (Constructor और Destructor Order)

A → B → C की chain में object बनने पर constructors base से derived की ओर चलते हैं, और destructors ठीक उल्टे order में।

#include <iostream>
using namespace std;

class A {
public:
    A()  { cout << "A constructor" << endl; }
    ~A() { cout << "A destructor" << endl; }
};

class B : public A {
public:
    B()  { cout << "B constructor" << endl; }
    ~B() { cout << "B destructor" << endl; }
};

class C : public B {
public:
    C()  { cout << "C constructor" << endl; }
    ~C() { cout << "C destructor" << endl; }
};

int main() {
    C obj;
    return 0;
}

Output:

A constructor
B constructor
C constructor
C destructor
B destructor
A destructor

4. Hierarchical Inheritance

एक base class से कई derived classes बनती हैं, जैसे Shape से Circle और Square। हर derived class को base के members मिलते हैं, पर derived classes एक-दूसरे के members नहीं पातीं। Real life example: एक college software में Person base class है, और Student, Teacher, Staff उससे बनी derived classes हैं। Name और address तीनों में common हैं, इसलिए वे Person में एक बार लिखे जाते हैं; roll number सिर्फ Student में और subject सिर्फ Teacher में।

#include <iostream>
using namespace std;

class Base {
public:
    void display() { cout << "Base class function" << endl; }
};

class Derived1 : public Base {
public:
    void show1() { cout << "Derived1 class function" << endl; }
};

class Derived2 : public Base {
public:
    void show2() { cout << "Derived2 class function" << endl; }
};

int main() {
    Derived1 obj1;
    Derived2 obj2;
    obj1.display();   // Base से मिला
    obj1.show1();
    obj2.display();   // Base से मिला
    obj2.show2();
    return 0;
}

Output:

Base class function
Derived1 class function
Base class function
Derived2 class function

Derived1 और Derived2 दोनों को display() Base से मिला, पर obj1.show2() लिखने पर error आएगा, क्योंकि show2() सिर्फ Derived2 में है।

5. Hybrid Inheritance और Diamond Problem

Hybrid inheritance दो या ज़्यादा types का mix है। सबसे common form diamond है: B और C दोनों A से inherit करती हैं (hierarchical), और D, B और C दोनों से (multiple)। तब D के object में A की दो copies बन जाती हैं, और d.x लिखने पर compiler को पता नहीं कि कौन सी copy लेनी है। यही diamond problem है।

Solution: virtual base class. B और C, A को virtual लिखकर inherit करें, तो D में A की सिर्फ एक shared copy बनती है।

#include <iostream>
using namespace std;

class A {
public:
    int x;
    A() { cout << "A constructor" << endl; }
};

class B : virtual public A {
public:
    B() { cout << "B constructor" << endl; }
};

class C : virtual public A {
public:
    C() { cout << "C constructor" << endl; }
};

class D : public B, public C {
public:
    D() { cout << "D constructor" << endl; }
};

int main() {
    D d;
    d.x = 10;            // अब ambiguity नहीं, A की एक ही copy है
    cout << "x = " << d.x << endl;
    return 0;
}

Output:

A constructor
B constructor
C constructor
D constructor
x = 10

ध्यान दें: virtual के साथ "A constructor" सिर्फ एक बार छपता है। अगर virtual हटा दें, तो A का constructor दो बार चलेगा (B से पहले और C से पहले), और d.x वाली line पर "ambiguous" compile error आएगा।

Advertisement

Base Class का Parameterized Constructor कैसे Call करें?

Derived class के constructor में member initializer list से base का constructor call होता है:

class Person {
protected:
    string name;
public:
    Person(string n) { name = n; }
};

class Student : public Person {
    int roll;
public:
    Student(string n, int r) : Person(n) {   // base constructor पहले
        roll = r;
    }
};

अगर base में default constructor नहीं है और आप : Person(n) न लिखें, तो compile error आता है।

Inheritance in C++ vs Java vs C#

बहुत से students "inheritance in c#" भी search करते हैं, इसलिए एक छोटी comparison:

PointC++JavaC#
Syntaxclass B : public Aclass B extends Aclass B : A
Multiple inheritance (classes)हाँनहीं (interfaces से)नहीं (interfaces से)
Access mode (public/private inheritance)हाँनहींनहीं
Parent को refer करनाBase:: (scope resolution)superbase
Inheritance रोकनाfinal (C++11 से)finalsealed

फायदे और कमियाँ

फायदेकमियाँ
Code reusability: common code एक बार base मेंBase और derived आपस में कसकर जुड़ जाते हैं
Function overriding और runtime polymorphism (virtual functions) possibleMultiple inheritance में ambiguity और diamond problem
Classes की साफ़ hierarchy बनती हैगहरी chain में debugging मुश्किल
Base में bug fix करने से सारी derived classes ठीक हो जाती हैंBase में बदलाव का असर सब derived classes पर पड़ता है

Base pointer से derived class का function चलाने के लिए Virtual Function in Hindi पढ़ें, और overriding vs overloading के लिए Overloading and Overriding in Hindi.

Common Mistakes

  • Access mode लिखना भूल जाना: class B : A में inheritance private हो जाती है, और main() में A के public functions भी B के object से call नहीं होते।
  • सोचना कि derived class का constructor पहले चलता है। हमेशा base का constructor पहले चलता है।
  • Diamond में सिर्फ एक class (B या C) में virtual लिखना। A की एक copy के लिए B और C दोनों में virtual public A लिखना होता है।
  • Base का private member derived class के function में सीधे use करना। इसके लिए member को protected बनाइए।
  • Base pointer से derived का function चलाने की उम्मीद करना, जबकि base में function virtual नहीं है। तब base वाला version ही चलता है।

Exam के लिए Important Questions

Short Questions (2–3 marks)

  1. Inheritance in C++ क्या है? Syntax लिखिए।
  2. Class में default inheritance mode क्या होता है? (private; struct में public)
  3. Multilevel inheritance में destructors किस order में चलते हैं? (Derived से base की ओर, constructors का उल्टा)
  4. Virtual base class क्या है? (Diamond problem में base की एक ही copy बनाने वाली class)
  5. Multiple inheritance में ambiguity कैसे दूर करते हैं? (Scope resolution operator :: से)

Long Questions (5–10 marks)

  1. Inheritance क्या है? C++ में इसके types diagram और example के साथ समझाइए।
  2. Public, protected और private inheritance का अंतर table के साथ समझाइए।
  3. Diamond problem क्या है? Virtual base class से इसे कैसे solve करते हैं? Program और output दीजिए।
  4. Multilevel inheritance में constructor और destructor calling order program से explain कीजिए।

FAQs

Inheritance meaning in Hindi in C++ क्या है?

Inheritance का मतलब है विरासत। C++ में इसका मतलब है कि derived class, base class के data members और member functions को बिना दोबारा लिखे पा लेती है और उनमें अपनी नई चीज़ें जोड़ सकती है।

C++ में inheritance के कितने types हैं?

पाँच: single, multiple, multilevel, hierarchical और hybrid।

क्या constructor और destructor inherit होते हैं?

नहीं। वे derived class में inherit नहीं होते, पर derived object बनते समय base का constructor अपने आप पहले चलता है और object खत्म होने पर base का destructor सबसे आखिर में।

Multiple inheritance में base constructors का order कौन तय करता है?

Class declaration में base classes जिस order में लिखी हैं (class Child : public Mother, public Father), वही order। Initializer list में लिखा order इसे नहीं बदलता।

Friend functions inherit होते हैं क्या?

नहीं। Friendship inherit नहीं होती। Base का friend function, derived class के private members access नहीं कर सकता।

अक्सर पूछे जाने वाले प्रश्न (FAQs)

Quick Answer: Inheritance in C++ वह feature है जिसमें एक नई class (derived class) किसी पहले से बनी class (base class) के data members और member functions को अपना लेती है।
C++ Notes in Hindi | CPP नोट्स हिंदी में के पाठ्यक्रम में Inheritance in C++ in Hindi | C++ में Inheritance हिंदी में एक महत्वपूर्ण अध्याय है। यह मुख्य सिद्धांतों, परीक्षा प्रश्नों और सूत्रों को समझने के लिए अत्यधिक आवश्यक है।
हाँ, NotesMedia पर आपको Inheritance in C++ in Hindi | C++ में Inheritance हिंदी में के महत्वपूर्ण प्रश्नोत्तर, हल किए गए उदाहरण और नोट्स बिल्कुल मुफ्त मिलेंगे। अधिक अभ्यास के लिए आप हमारा ऐप भी डाउनलोड कर सकते हैं।
Advertisement