Types of Inheritance in C++ with Examples - Single Inheritance and Multilevel Inheritance
Inheritance is an important feature of Object-Oriented Programming (OOP) in C++. It allows a class to acquire accessible properties and behaviors of another class. Based on the relationship between base and derived classes, inheritance is classified into different types. C++ supports five major types of inheritance: Single, Multiple, Multilevel, Hierarchical and Hybrid Inheritance.
C++ में Inheritance, Object-Oriented Programming (OOP) का एक महत्वपूर्ण feature है। इसकी सहायता से एक class दूसरी class के accessible properties और behaviors को प्राप्त कर सकती है। Base और Derived classes के relationship के आधार पर Inheritance को अलग-अलग प्रकारों में बाँटा जाता है। C++ में Inheritance के पाँच प्रमुख प्रकार हैं: Single, Multiple, Multilevel, Hierarchical और Hybrid Inheritance।
Types of Inheritance in C++
The five major types of inheritance in C++ are:
| Type of Inheritance | Description |
|---|---|
| Single Inheritance | One derived class inherits from one base class. |
| Multiple Inheritance | One derived class inherits from two or more base classes. |
| Multilevel Inheritance | A class is derived from another derived class. |
| Hierarchical Inheritance | Multiple derived classes inherit from one base class. |
| Hybrid Inheritance | A combination of two or more types of inheritance. |
C++ में Inheritance के पाँच प्रमुख प्रकार होते हैं। प्रत्येक प्रकार में Base Class और Derived Class के बीच अलग-अलग संबंध होता है।
Single Inheritance in C++
Single Inheritance is a type of inheritance in which one derived class inherits from only one base class. It is the simplest form of inheritance in C++. For example, a Student class can inherit properties and functions from a Person class.
Single Inheritance में एक Derived Class केवल एक Base Class से inherit करती है। यह C++ में Inheritance का सबसे सरल प्रकार है। उदाहरण के लिए, Student class, Person class के accessible members को inherit कर सकती है।
Structure of Single Inheritance
Person||Student
Here, Person is the base class and Student is the derived class.
इस उदाहरण में Person Base Class है और Student Derived Class है।
Syntax of Single Inheritance
class BaseClass {// Members of base class};class DerivedClass : public BaseClass {// Members of derived class};
The public keyword specifies the inheritance access mode. It controls how the accessible public and protected members of the base class are inherited.
public keyword Inheritance का access mode निर्धारित करता है। यह Base Class के accessible public और protected members की accessibility को नियंत्रित करता है।
Program: Single Inheritance in C++
The following program demonstrates single inheritance using Person and Student classes.
निम्नलिखित program में Person और Student classes की सहायता से Single Inheritance को प्रदर्शित किया गया है।
#include <iostream>#include <string>using namespace std;class Person {public:string name;int age;void getPersonDetails() {cout << "Enter name: ";getline(cin, 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;}};int main() {Student s;s.getPersonDetails();s.getStudentDetails();cout << "\nStudent Details\n";s.displayStudentDetails();return 0;}
Output
Enter name: Rahul SharmaEnter age: 20Enter roll number: 101Student DetailsName: Rahul SharmaAge: 20Roll Number: 101
Explanation of the Program
The
Personclass contains the data membersnameandage, along with functions to input and display personal details.The
Studentclass publicly inherits thePersonclass.The
Studentclass adds its own data memberrollNo.The object
sof theStudentclass can call the accessible public functions of both classes.
इस program में:
Personclass मेंnameऔरagedata members तथा personal details को input और display करने वाले functions हैं।Studentclass,Personclass को publicly inherit करती है।Studentclass अपना अतिरिक्त data memberrollNoरखती है।Studentclass का objects, दोनों classes के accessible public functions को call कर सकता है।
Multilevel Inheritance in C++
Multilevel Inheritance is a type of inheritance in which a class is derived from another derived class. In this structure, inheritance takes place through multiple levels.
For example, a Student class can inherit from Person, and a Result class can inherit from Student.
Multilevel Inheritance में एक class दूसरी Derived Class से inherit करती है। इसमें inheritance के एक से अधिक levels होते हैं।
उदाहरण के लिए, Student class, Person class से inherit कर सकती है और Result class, Student class से inherit कर सकती है।
Structure of Multilevel Inheritance
Person||Student||Result
Here:
Personis the base class.Studentis derived fromPerson.Resultis derived fromStudent.
इस उदाहरण में:
PersonBase Class है।Student,Personसे Derived Class है।Result,Studentसे Derived Class है।
Therefore, the Result class can access the accessible inherited members of both Student and Person, subject to the inheritance access rules.
इस प्रकार Result class, Inheritance के access rules के अनुसार Student और Person से प्राप्त accessible members का उपयोग कर सकती है।
Program: Multilevel Inheritance in C++
The following program demonstrates multilevel inheritance using three classes: Person, Student and Result.
निम्नलिखित program में Person, Student और Result नाम की तीन classes की सहायता से Multilevel Inheritance को प्रदर्शित किया गया है।
#include <iostream>#include <string>using namespace std;class Person {public:string name;void getName() {cout << "Enter student name: ";getline(cin, name);}void displayName() {cout << "Name: " << name << endl;}};class Student : public Person {public:int rollNo;void getRollNo() {cout << "Enter roll number: ";cin >> rollNo;}void displayRollNo() {cout << "Roll Number: " << rollNo << endl;}};class Result : public Student {public:int marks;void getMarks() {cout << "Enter marks: ";cin >> marks;}void displayResult() {displayName();displayRollNo();cout << "Marks: " << marks << endl;}};int main() {Result r;r.getName();r.getRollNo();r.getMarks();cout << "\nStudent Result\n";r.displayResult();return 0;}
Output
Enter student name: Amit VermaEnter roll number: 102Enter marks: 85Student ResultName: Amit VermaRoll Number: 102Marks: 85
Explanation of the Program
The
Personclass stores the student's name.The
Studentclass inherits fromPersonand stores the roll number.The
Resultclass inherits fromStudentand stores marks.The
Resultobject can use the accessible public functions inherited through both levels.This demonstrates how a class can acquire accessible members through a chain of inheritance.
इस program में:
Personclass विद्यार्थी का नाम store करती है।Studentclass,Personसे inherit करके roll number store करती है।Resultclass,Studentसे inherit करके marks store करती है।Resultclass का object दोनों inheritance levels से प्राप्त accessible public functions का उपयोग कर सकता है।यह program दिखाता है कि एक class inheritance की chain के माध्यम से members प्राप्त कर सकती है।
Difference Between Single and Multilevel Inheritance
| Basis | Single Inheritance | Multilevel Inheritance |
|---|---|---|
| Meaning | One derived class inherits from one base class. | A class inherits from another derived class. |
| Levels | One inheritance level. | Two or more inheritance levels. |
| Classes | Generally two classes are involved. | Generally three or more classes are involved. |
| Structure | Base → Derived | Base → Derived → Further Derived |
| Complexity | Simple structure. | Comparatively more complex structure. |
| Example | Person → Student | Person → Student → Result |
Single Inheritance में केवल एक inheritance level होता है, जबकि Multilevel Inheritance में inheritance की chain बनती है।
Single Inheritance is suitable for a direct relationship between two classes, whereas Multilevel Inheritance is useful when a class hierarchy requires multiple levels.
Frequently Asked Questions (FAQs)
What are the five types of inheritance in C++?
The five major types are Single, Multiple, Multilevel, Hierarchical and Hybrid Inheritance.
C++ में Inheritance के पाँच प्रमुख प्रकार Single, Multiple, Multilevel, Hierarchical और Hybrid Inheritance हैं।
What is Single Inheritance in C++?
Single Inheritance occurs when one derived class inherits from one base class.
जब एक Derived Class केवल एक Base Class से inherit करती है, तो इसे Single Inheritance कहते हैं।
What is Multilevel Inheritance in C++?
Multilevel Inheritance occurs when a class is derived from another derived class, creating multiple levels of inheritance.
जब एक class किसी दूसरी Derived Class से inherit करती है और inheritance के कई levels बनते हैं, तो इसे Multilevel Inheritance कहते हैं।
Can a derived class access private members of a base class directly?
No. Private members of a base class are not directly accessible in its derived class. They can be accessed indirectly through suitable public or protected member functions provided by the base class.
नहीं। Base Class के private members को Derived Class में directly access नहीं किया जा सकता। इन्हें Base Class के उपयुक्त public या protected member functions के माध्यम से indirectly access किया जा सकता है।
Which is the simplest type of inheritance in C++?
Single Inheritance is the simplest type because one derived class inherits from only one base class.
Single Inheritance सबसे सरल प्रकार है क्योंकि इसमें एक Derived Class केवल एक Base Class से inherit करती है।
C++ में Inheritance code reusability को बढ़ावा देता है और classes के बीच संबंध स्थापित करने में मदद करता है। Single Inheritance में एक Base Class और एक Derived Class होती है, जबकि Multilevel Inheritance में कई classes के माध्यम से inheritance की chain बनती है। इन दोनों प्रकारों को समझना अन्य जटिल Inheritance structures सीखने के लिए उपयोगी है।
Comments
Post a Comment