Types of Inheritance

The different ways classes can be related to one another.

Types of Inheritance

Inheritance Object-Oriented Programming (OOP) का एक महत्वपूर्ण feature है, जिसमें एक class दूसरी class के properties (गुण) और member functions (सदस्य functions) को प्राप्त कर सकती है। Inheritance के structure और classes के बीच relationship के आधार पर इसे विभिन्न प्रकारों में बाँटा जाता है।

Types of Inheritance: Single → Multilevel → Multiple → Hierarchical → Hybrid

What are Types of Inheritance?

C++ में inheritance के प्रकार यह बताते हैं कि base classes और derived classes के बीच relationship किस प्रकार बनाया गया है। एक derived class एक base class से inherit कर सकती है, कई levels तक inheritance हो सकता है या एक derived class एक से अधिक base classes से inherit कर सकती है।

C++ में inheritance के प्रमुख प्रकार निम्नलिखित हैं:

  1. Single Inheritance
  2. Multilevel Inheritance
  3. Multiple Inheritance
  4. Hierarchical Inheritance
  5. Hybrid Inheritance

1. Single Inheritance

जब एक derived class केवल एक base class से inherit करती है, तो इसे Single Inheritance कहा जाता है। यह inheritance का सबसे सरल प्रकार है।

Structure:
Base Class → Derived Class

उदाहरण के लिए, यदि Student class, Person class से inherit करती है, तो यह Single Inheritance है।

#include <iostream>
using namespace std;

class Person
{
public:
    void showPerson()
    {
        cout << "This is a Person" << endl;
    }
};

class Student : public Person
{
public:
    void showStudent()
    {
        cout << "This is a Student";
    }
};

int main()
{
    Student s;

    s.showPerson();
    s.showStudent();

    return 0;
}
Output:
This is a Person
This is a Student

यहाँ Person base class और Student derived class है। Student ने Person से inheritance प्राप्त किया है। इसलिए यह Single Inheritance का उदाहरण है।

Features of Single Inheritance

  • इसमें केवल एक base class होती है।
  • इसमें एक derived class होती है।
  • यह inheritance का सबसे सरल प्रकार है।
  • इसका उपयोग common functionality को derived class में reuse करने के लिए किया जाता है।

2. Multilevel Inheritance

जब inheritance एक से अधिक levels में होता है और एक derived class आगे किसी दूसरी class की base class बन जाती है, तो इसे Multilevel Inheritance कहा जाता है।

Structure:
Base Class → Derived Class → Further Derived Class

उदाहरण के लिए, Animal से Dog और Dog से Puppy inherit करता है।

#include <iostream>
using namespace std;

class Animal
{
public:
    void eat()
    {
        cout << "Animal Eats" << endl;
    }
};

class Dog : public Animal
{
public:
    void bark()
    {
        cout << "Dog Barks" << endl;
    }
};

class Puppy : public Dog
{
public:
    void play()
    {
        cout << "Puppy Plays";
    }
};

int main()
{
    Puppy p;

    p.eat();
    p.bark();
    p.play();

    return 0;
}
Output:
Animal Eats
Dog Barks
Puppy Plays

यहाँ Puppy class ने Dog से और Dog class ने Animal से inheritance प्राप्त किया है। इसलिए Puppy को Animal और Dog के accessible members प्राप्त होते हैं।

Features of Multilevel Inheritance

  • Inheritance multiple levels में होता है।
  • एक derived class आगे base class बन सकती है।
  • Inheritance एक chain के रूप में दिखाई देता है।
  • ऊपरी class के accessible members नीचे की derived class तक पहुँच सकते हैं।

3. Multiple Inheritance

जब एक derived class एक से अधिक base classes से inherit करती है, तो इसे Multiple Inheritance कहा जाता है।

Structure:
Base Class A + Base Class B → Derived Class

उदाहरण के लिए, एक Child class Father और Mother दोनों classes से inherit कर सकती है।

#include <iostream>
using namespace std;

class Father
{
public:
    void fatherFeature()
    {
        cout << "Father Feature" << endl;
    }
};

class Mother
{
public:
    void motherFeature()
    {
        cout << "Mother Feature" << endl;
    }
};

class Child : public Father, public Mother
{
public:
    void childFeature()
    {
        cout << "Child Feature";
    }
};

int main()
{
    Child c;

    c.fatherFeature();
    c.motherFeature();
    c.childFeature();

    return 0;
}
Output:
Father Feature
Mother Feature
Child Feature

यहाँ Child class ने Father और Mother दोनों classes से inheritance प्राप्त किया है। इसलिए यह Multiple Inheritance का उदाहरण है।

Ambiguity in Multiple Inheritance

Multiple Inheritance में कभी-कभी ambiguity (अस्पष्टता) की स्थिति उत्पन्न हो सकती है। यदि दोनों base classes में समान नाम का member function हो और derived class के object से उसे directly call किया जाए, तो compiler यह निर्धारित नहीं कर पाता कि किस base class के function को call किया जाए।

#include <iostream>
using namespace std;

class A
{
public:
    void show()
    {
        cout << "Class A";
    }
};

class B
{
public:
    void show()
    {
        cout << "Class B";
    }
};

class C : public A, public B
{
};

int main()
{
    C obj;

    obj.A::show();

    return 0;
}
Output:
Class A

यहाँ A और B दोनों में show() function है। इसलिए A::show() लिखकर स्पष्ट किया गया है कि A class के show() function को call करना है। Scope resolution operator ambiguity को resolve करने में सहायता करता है।

4. Hierarchical Inheritance

जब एक base class से एक से अधिक derived classes inherit करती हैं, तो इसे Hierarchical Inheritance कहा जाता है।

Structure:
       Base Class
       /         \
Derived Class A   Derived Class B

उदाहरण के लिए, Shape एक base class हो सकती है और Circle तथा Rectangle उसकी derived classes हो सकती हैं।

#include <iostream>
using namespace std;

class Shape
{
public:
    void showShape()
    {
        cout << "This is a Shape" << endl;
    }
};

class Circle : public Shape
{
public:
    void showCircle()
    {
        cout << "This is a Circle" << endl;
    }
};

class Rectangle : public Shape
{
public:
    void showRectangle()
    {
        cout << "This is a Rectangle";
    }
};

int main()
{
    Circle c;
    Rectangle r;

    c.showShape();
    c.showCircle();

    r.showShape();
    r.showRectangle();

    return 0;
}
Output:
This is a Shape
This is a Circle
This is a Shape
This is a Rectangle

यहाँ Circle और Rectangle दोनों ने Shape class से inheritance प्राप्त किया है। इसलिए यह Hierarchical Inheritance का उदाहरण है।

Features of Hierarchical Inheritance

  • एक common base class होती है।
  • एक से अधिक derived classes हो सकती हैं।
  • सभी derived classes base class की accessible functionality को reuse कर सकती हैं।
  • Common functionality को एक ही base class में रखने से code duplication कम होता है।

5. Hybrid Inheritance

जब दो या दो से अधिक प्रकार के inheritance को एक ही program में combine किया जाता है, तो इसे Hybrid Inheritance कहा जाता है।

Hybrid inheritance में Single, Multiple, Multilevel या Hierarchical inheritance के combinations हो सकते हैं।

Hybrid Inheritance: दो या दो से अधिक inheritance types का combination

उदाहरण के लिए, यदि एक program में Hierarchical और Multiple Inheritance दोनों का combination हो, तो यह Hybrid Inheritance का उदाहरण हो सकता है।

#include <iostream>
using namespace std;

class Person
{
public:
    void showPerson()
    {
        cout << "Person" << endl;
    }
};

class Student : public Person
{
public:
    void showStudent()
    {
        cout << "Student" << endl;
    }
};

class Sports
{
public:
    void showSports()
    {
        cout << "Sports" << endl;
    }
};

class Result : public Student, public Sports
{
public:
    void showResult()
    {
        cout << "Result";
    }
};

int main()
{
    Result r;

    r.showPerson();
    r.showStudent();
    r.showSports();
    r.showResult();

    return 0;
}
Output:
Person
Student
Sports
Result

इस example में Result class, Student और Sports से inherit करती है तथा Student स्वयं Person से inherit करती है। इसलिए इसमें Multilevel और Multiple Inheritance का combination दिखाई देता है।

Comparison of Types of Inheritance

Type Structure Main Feature
Single A → B One base और one derived class
Multilevel A → B → C Inheritance multiple levels में
Multiple A + B → C One derived class, multiple base classes
Hierarchical A → B, C One base class, multiple derived classes
Hybrid Combination Multiple inheritance types का combination

Single vs Multilevel Inheritance

Single Inheritance Multilevel Inheritance
एक direct inheritance relationship होता है। Inheritance कई levels में होता है।
Structure: A → B Structure: A → B → C
यह अपेक्षाकृत सरल होता है। इसमें inheritance chain बनती है।

Multiple vs Hierarchical Inheritance

Multiple Inheritance Hierarchical Inheritance
एक derived class कई base classes से inherit करती है। कई derived classes एक base class से inherit करती हैं।
Structure: A + B → C Structure: A → B, C
Base classes की संख्या अधिक हो सकती है। Derived classes की संख्या अधिक हो सकती है।

Inheritance and Code Reusability

Inheritance का प्रत्येक प्रकार अलग class relationships को represent करता है, लेकिन सभी में code reusability एक महत्वपूर्ण उद्देश्य है। Common functionality को base class में रखकर derived classes में reuse किया जा सकता है।

#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";
    }
};

class Clerk : public Employee
{
public:
    void work()
    {
        cout << "Clerk is Working";
    }
};

int main()
{
    Teacher t;
    Clerk c;

    t.login();
    t.teach();

    c.login();
    c.work();

    return 0;
}
Output:
Employee Login
Teacher is Teaching
Employee Login
Clerk is Working

यहाँ Teacher और Clerk दोनों Employee class से inherit करते हैं। Employee का login() function दोनों derived classes में reuse किया गया है। यह Hierarchical Inheritance का practical example है।

Important Points

  • C++ में inheritance के पाँच प्रमुख प्रकार हैं—Single, Multilevel, Multiple, Hierarchical और Hybrid।
  • Single Inheritance में एक derived class एक base class से inherit करती है।
  • Multilevel Inheritance में inheritance कई levels तक होता है।
  • Multiple Inheritance में एक derived class एक से अधिक base classes से inherit करती है।
  • Hierarchical Inheritance में एक base class से कई derived classes inherit करती हैं।
  • Hybrid Inheritance दो या अधिक inheritance types का combination है।
  • Multiple Inheritance में same-name members के कारण ambiguity हो सकती है।
  • Scope resolution operator (::) ambiguity को resolve करने में सहायता कर सकता है।
  • Inheritance का प्रमुख लाभ code reusability है।

Board Focus

Exam के लिए याद रखें:
Single → One Base → One Derived
Multilevel → A → B → C
Multiple → Multiple Base → One Derived
Hierarchical → One Base → Multiple Derived
Hybrid → Combination of Two or More Types

Most Important: Multiple Inheritance में एक derived class कई base classes से inherit करती है, जबकि Hierarchical Inheritance में कई derived classes एक ही base class से inherit करती हैं।

Board Important Questions

Very Short Answer Questions

Q1. C++ में inheritance के प्रमुख प्रकार कौन-कौन से हैं?

Answer: Single, Multilevel, Multiple, Hierarchical और Hybrid Inheritance प्रमुख प्रकार हैं।

Q2. Single Inheritance क्या है?

Answer: जब एक derived class एक base class से inherit करती है, तो इसे Single Inheritance कहते हैं।

Q3. Multilevel Inheritance क्या है?

Answer: जब inheritance कई levels की chain में होता है, जैसे A → B → C, तो इसे Multilevel Inheritance कहते हैं।

Q4. Multiple Inheritance क्या है?

Answer: जब एक derived class एक से अधिक base classes से inherit करती है, तो इसे Multiple Inheritance कहते हैं।

Q5. Hierarchical Inheritance क्या है?

Answer: जब एक base class से एक से अधिक derived classes inherit करती हैं, तो इसे Hierarchical Inheritance कहते हैं।

Q6. Hybrid Inheritance क्या है?

Answer: जब दो या दो से अधिक inheritance types को combine किया जाता है, तो उसे Hybrid Inheritance कहते हैं।

Short Answer Questions

Q7. Single और Multiple Inheritance में अंतर बताइए।

Single Inheritance Multiple Inheritance
एक base class होती है। एक से अधिक base classes हो सकती हैं।
एक derived class base class से inherit करती है। एक derived class multiple base classes से inherit करती है।
Structure: A → B Structure: A + B → C

Q8. Multilevel और Hierarchical Inheritance में अंतर बताइए।

Multilevel Inheritance Hierarchical Inheritance
Inheritance multiple levels में होता है। एक base class से multiple derived classes inherit करती हैं।
Structure: A → B → C Structure: A → B और A → C
एक derived class आगे base class बन सकती है। एक base class कई derived classes के लिए common होती है।

Q9. Multiple Inheritance में ambiguity क्या है?

Answer: जब multiple base classes में समान नाम का member function मौजूद हो और derived class के object से उसे directly call किया जाए, तो compiler को यह तय करने में समस्या हो सकती है कि किस base class के function को call करना है। इस स्थिति को ambiguity कहते हैं।

Q10. Hybrid Inheritance से क्या समझते हैं?

Answer: जब एक program में दो या दो से अधिक inheritance types का combination किया जाता है, तो उसे Hybrid Inheritance कहते हैं।

Long Answer Questions

Q11. C++ में inheritance के विभिन्न प्रकारों को समझाइए।

Answer: C++ में inheritance को मुख्य रूप से पाँच प्रकारों में बाँटा जाता है—Single, Multilevel, Multiple, Hierarchical और Hybrid। Single में एक base और एक derived class होती है। Multilevel में inheritance chain बनती है। Multiple में एक derived class कई base classes से inherit करती है। Hierarchical में एक base class से कई derived classes inherit करती हैं। Hybrid में दो या अधिक inheritance types का combination होता है।

Q12. Single Inheritance का C++ program लिखिए।

#include <iostream>
using namespace std;

class Animal
{
public:
    void eat()
    {
        cout << "Animal Eats";
    }
};

class Dog : public Animal
{
};

int main()
{
    Dog d;

    d.eat();

    return 0;
}
Output:
Animal Eats

Q13. Multilevel Inheritance का C++ program लिखिए।

#include <iostream>
using namespace std;

class A
{
public:
    void showA()
    {
        cout << "A" << endl;
    }
};

class B : public A
{
public:
    void showB()
    {
        cout << "B" << endl;
    }
};

class C : public B
{
public:
    void showC()
    {
        cout << "C";
    }
};

int main()
{
    C obj;

    obj.showA();
    obj.showB();
    obj.showC();

    return 0;
}
Output:
A
B
C

Q14. Multiple Inheritance का C++ program लिखिए।

#include <iostream>
using namespace std;

class A
{
public:
    void showA()
    {
        cout << "A" << endl;
    }
};

class B
{
public:
    void showB()
    {
        cout << "B" << endl;
    }
};

class C : public A, public B
{
};

int main()
{
    C obj;

    obj.showA();
    obj.showB();

    return 0;
}
Output:
A
B

Q15. Hierarchical Inheritance का C++ program लिखिए।

#include <iostream>
using namespace std;

class Animal
{
public:
    void eat()
    {
        cout << "Animal Eats" << endl;
    }
};

class Dog : public Animal
{
public:
    void bark()
    {
        cout << "Dog Barks" << endl;
    }
};

class Cat : public Animal
{
public:
    void meow()
    {
        cout << "Cat Meows";
    }
};

int main()
{
    Dog d;
    Cat c;

    d.eat();
    d.bark();

    c.eat();
    c.meow();

    return 0;
}
Output:
Animal Eats
Dog Barks
Animal Eats
Cat Meows

Quick Revision

  • Single: One base → One derived
  • Multilevel: A → B → C
  • Multiple: Multiple bases → One derived
  • Hierarchical: One base → Multiple derived
  • Hybrid: Combination of inheritance types
One-Line Revision: C++ में Single, Multilevel, Multiple, Hierarchical और Hybrid पाँच प्रमुख प्रकार के inheritance हैं, जो अलग-अलग class relationships और code reuse को represent करते हैं।

Practice Questions

  1. Inheritance के विभिन्न प्रकारों के नाम लिखिए।
  2. Single Inheritance को उदाहरण सहित समझाइए।
  3. Multilevel Inheritance क्या है?
  4. Multiple Inheritance को उदाहरण सहित समझाइए।
  5. Hierarchical Inheritance क्या है?
  6. Hybrid Inheritance से आप क्या समझते हैं?
  7. Single और Multiple Inheritance में अंतर बताइए।
  8. Multilevel और Hierarchical Inheritance में अंतर बताइए।
  9. Multiple Inheritance में ambiguity क्या है?
  10. Scope resolution operator ambiguity को कैसे resolve करता है?
  11. Single Inheritance का C++ program लिखिए।
  12. Multilevel Inheritance का C++ program लिखिए।
  13. Multiple Inheritance का C++ program लिखिए।
  14. Hierarchical Inheritance का C++ program लिखिए।
  15. Inheritance के विभिन्न प्रकारों को diagram और program की सहायता से समझाइए।
Lesson 11 of 37
On This Page