Types of Inheritance in C++ with Examples - Multiple Inheritance and Hierarchical Inheritance
C++ supports different types of inheritance that help programmers create relationships between classes and reuse existing code. Multiple Inheritance and Hierarchical Inheritance are two important types of inheritance in Object-Oriented Programming. In Multiple Inheritance, one derived class inherits from two or more base classes. In Hierarchical Inheritance, two or more derived classes inherit from a single base class.
C++ में Inheritance के अलग-अलग प्रकार होते हैं, जो classes के बीच संबंध स्थापित करने और existing code को reuse करने में मदद करते हैं। Multiple Inheritance और Hierarchical Inheritance, Object-Oriented Programming के दो महत्वपूर्ण प्रकार हैं। Multiple Inheritance में एक Derived Class दो या दो से अधिक Base Classes से inherit करती है। Hierarchical Inheritance में दो या दो से अधिक Derived Classes एक ही Base Class से inherit करती हैं।
Multiple Inheritance in C++
Multiple Inheritance is a type of inheritance in which a single derived class inherits the accessible members of two or more base classes.
For example, a Result class may inherit student details from a Student class and examination details from an Examination class.
Multiple Inheritance में एक Derived Class दो या दो से अधिक Base Classes से inherit करती है। इससे एक class अलग-अलग Base Classes के accessible members का उपयोग कर सकती है।
उदाहरण के लिए, Result class, Student class से student details और Examination class से examination details inherit कर सकती है।
Structure of Multiple Inheritance
Student Examination\ /\ /Result
Here, Student and Examination are base classes, while Result is the derived class.
इस उदाहरण में Student और Examination Base Classes हैं, जबकि Result Derived Class है।
Syntax of Multiple Inheritance
class Base1 {// Members of Base1};class Base2 {// Members of Base2};class Derived : public Base1, public Base2 {// Members of Derived};
A derived class can specify multiple base classes in its inheritance declaration. The access mode can be specified separately for each base class.
Derived Class की inheritance declaration में एक से अधिक Base Classes लिखी जा सकती हैं। प्रत्येक Base Class के लिए access mode अलग से निर्धारित किया जा सकता है।
Program: Multiple Inheritance in C++
The following program demonstrates multiple inheritance using Student, Examination and Result classes.
निम्नलिखित program में Student, Examination और Result classes की सहायता से Multiple Inheritance को प्रदर्शित किया गया है।
#include <iostream>#include <string>using namespace std;class Student {public:int rollNo;string name;void getStudentDetails() {cout << "Enter roll number: ";cin >> rollNo;cout << "Enter name: ";getline(cin >> ws, name);}void displayStudentDetails() {cout << "Roll Number: " << rollNo << endl;cout << "Name: " << name << endl;}};class Examination {public:int marks;void getMarks() {cout << "Enter marks: ";cin >> marks;}void displayMarks() {cout << "Marks: " << marks << endl;}};class Result : public Student, public Examination {public:void displayResult() {displayStudentDetails();displayMarks();}};int main() {Result r;r.getStudentDetails();r.getMarks();cout << "\nStudent Result\n";r.displayResult();return 0;}
Output
Enter roll number: 101Enter name: Rahul SharmaEnter marks: 85Student ResultRoll Number: 101Name: Rahul SharmaMarks: 85
Explanation of the Program
The
Studentclass stores the student's roll number and name.The
Examinationclass stores examination marks.The
Resultclass inherits publicly from bothStudentandExamination.The
Resultclass can use the accessible public functions of both base classes.The object
rcan call the inherited functions and display the complete result.
इस program में:
Studentclass विद्यार्थी का roll number और name store करती है।Examinationclass परीक्षा के marks store करती है।Resultclass,StudentऔरExaminationदोनों classes को publicly inherit करती है।Resultclass दोनों Base Classes के accessible public functions का उपयोग कर सकती है।Object
rinherited functions को call करके पूरा result display करता है।
Hierarchical Inheritance in C++
Hierarchical Inheritance is a type of inheritance in which two or more derived classes inherit from the same base class.
For example, Student and Teacher classes can inherit common information such as name and age from a Person class.
Hierarchical Inheritance में दो या दो से अधिक Derived Classes एक ही Base Class से inherit करती हैं।
उदाहरण के लिए, Student और Teacher classes, Person class से name और age जैसी common information inherit कर सकती हैं।
Structure of Hierarchical Inheritance
Person/ \/ \Student Teacher
Here, Person is the common base class, while Student and Teacher are derived classes.
इस उदाहरण में Person एक common Base Class है, जबकि Student और Teacher Derived Classes हैं।
Syntax of Hierarchical Inheritance
class Base {// Common members};class Derived1 : public Base {// Members of Derived1};class Derived2 : public Base {// Members of Derived2};
Multiple derived classes can independently inherit from the same base class.
एक ही Base Class से कई Derived Classes independently inherit कर सकती हैं।
Program: Hierarchical Inheritance in C++
The following program demonstrates hierarchical inheritance using a common Person class and two derived classes, Student and Teacher.
निम्नलिखित program में एक common Person class तथा दो Derived Classes Student और Teacher की सहायता से Hierarchical Inheritance को प्रदर्शित किया गया है।
#include <iostream>#include <string>using namespace std;class Person {public:string name;int age;void getPersonDetails() {cout << "Enter name: ";getline(cin >> ws, name);cout << "Enter age: ";cin >> age;}void displayPersonDetails() {cout << "Name: " << name << endl;cout << "Age: " << age << endl;}};class Student : public Person {public:int rollNo;void getStudentDetails() {cout << "Enter roll number: ";cin >> rollNo;}void displayStudentDetails() {displayPersonDetails();cout << "Roll Number: " << rollNo << endl;}};class Teacher : public Person {public:string subject;void getTeacherDetails() {cout << "Enter subject: ";getline(cin >> ws, subject);}void displayTeacherDetails() {displayPersonDetails();cout << "Subject: " << subject << endl;}};int main() {Student s;Teacher t;cout << "Enter Student Details\n";s.getPersonDetails();s.getStudentDetails();cout << "\nEnter Teacher Details\n";t.getPersonDetails();t.getTeacherDetails();cout << "\nStudent Information\n";s.displayStudentDetails();cout << "\nTeacher Information\n";t.displayTeacherDetails();return 0;}
Output
Enter Student DetailsEnter name: Amit SharmaEnter age: 20Enter roll number: 101Enter Teacher DetailsEnter name: Rajesh VermaEnter age: 35Enter subject: Computer ScienceStudent InformationName: Amit SharmaAge: 20Roll Number: 101Teacher InformationName: Rajesh VermaAge: 35Subject: Computer Science
Explanation of the Program
The
Personclass contains common data membersnameandage.The
Studentclass inherits fromPersonand addsrollNo.The
Teacherclass also inherits fromPersonand addssubject.Both derived classes can use the accessible public members of
Person.The objects
sandtindependently store student and teacher information.
इस program में:
Personclass में common data membersnameऔरageहैं।Studentclass,Personसे inherit करकेrollNoजोड़ती है।Teacherclass भीPersonसे inherit करकेsubjectजोड़ती है।दोनों Derived Classes,
Personके accessible public members का उपयोग कर सकती हैं।Objects
sऔरtविद्यार्थी तथा शिक्षक की जानकारी को independently store करते हैं।
Difference Between Multiple and Hierarchical Inheritance
| Basis | Multiple Inheritance | Hierarchical Inheritance |
|---|---|---|
| Meaning | One derived class inherits from multiple base classes. | Multiple derived classes inherit from one base class. |
| Base Classes | Two or more base classes. | One common base class. |
| Derived Classes | One derived class in the basic structure. | Two or more derived classes. |
| Structure | Multiple base classes → One derived class | One base class → Multiple derived classes |
| Main Purpose | Combine features of multiple classes. | Share common features among multiple classes. |
| Example | Student + Examination → Result | Person → Student and Teacher |
Multiple Inheritance का उपयोग तब किया जाता है जब एक class को कई Base Classes की सुविधाओं की आवश्यकता होती है। Hierarchical Inheritance का उपयोग तब किया जाता है जब कई Derived Classes में समान properties और functions की आवश्यकता होती है।
Advantages of Multiple and Hierarchical Inheritance
Advantages of Multiple Inheritance
It allows a class to combine features from multiple base classes.
It supports code reuse from different classes.
It helps represent objects that have multiple roles or capabilities.
Advantages of Hierarchical Inheritance
It allows multiple classes to reuse common base-class functionality.
It reduces duplication of common code.
It provides a clear class hierarchy.
Changes to common functionality can be made in the base class.
Multiple Inheritance के लाभ
एक class कई Base Classes की सुविधाओं को combine कर सकती है।
अलग-अलग classes के code को reuse किया जा सकता है।
यह ऐसे objects को represent करने में मदद करता है जिनकी कई roles या capabilities होती हैं।
Hierarchical Inheritance के लाभ
कई classes common Base Class की functionality को reuse कर सकती हैं।
Common code की duplication कम होती है।
Classes की hierarchy स्पष्ट रहती है।
Common functionality में बदलाव Base Class में किया जा सकता है।
Frequently Asked Questions (FAQs)
What is Multiple Inheritance in C++?
Multiple Inheritance is a type of inheritance in which one derived class inherits from two or more base classes.
Multiple Inheritance में एक Derived Class दो या दो से अधिक Base Classes से inherit करती है।
What is Hierarchical Inheritance in C++?
Hierarchical Inheritance is a type of inheritance in which multiple derived classes inherit from a single base class.
Hierarchical Inheritance में कई Derived Classes एक ही Base Class से inherit करती हैं।
What is the main difference between Multiple and Hierarchical Inheritance?
Multiple Inheritance has multiple base classes and one derived class, whereas Hierarchical Inheritance has one common base class and multiple derived classes.
Multiple Inheritance में कई Base Classes और एक Derived Class होती है, जबकि Hierarchical Inheritance में एक common Base Class और कई Derived Classes होती हैं।
Can C++ support Multiple Inheritance?
Yes. C++ supports multiple inheritance by allowing a derived class to inherit from more than one base class.
हाँ। C++ में एक Derived Class को एक से अधिक Base Classes से inherit करने की सुविधा मिलती है।
What is an ambiguity problem in Multiple Inheritance?
An ambiguity problem occurs when a derived class receives members with the same name from multiple base classes and the compiler cannot determine which member is intended.
जब एक Derived Class को कई Base Classes से समान नाम वाले members प्राप्त होते हैं और compiler यह तय नहीं कर पाता कि किस member का उपयोग करना है, तब Ambiguity Problem उत्पन्न होती है।
Which inheritance type allows multiple classes to share common features?
Hierarchical Inheritance allows multiple derived classes to share common features from one base class.
Hierarchical Inheritance में कई Derived Classes एक ही Base Class से common features प्राप्त करती हैं।
Multiple Inheritance and Hierarchical Inheritance are useful features of C++ that support code reusability and flexible class design. Multiple Inheritance combines features from multiple base classes into one derived class, while Hierarchical Inheritance allows multiple derived classes to share common functionality from one base class.
C++ में Multiple Inheritance और Hierarchical Inheritance code reusability तथा flexible class design के लिए उपयोगी हैं। Multiple Inheritance में एक Derived Class कई Base Classes की सुविधाओं को combine करती है, जबकि Hierarchical Inheritance में कई Derived Classes एक ही Base Class की common functionality का उपयोग करती हैं।
Comments
Post a Comment