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 InheritanceDescription
Single InheritanceOne derived class inherits from one base class.
Multiple InheritanceOne derived class inherits from two or more base classes.
Multilevel InheritanceA class is derived from another derived class.
Hierarchical InheritanceMultiple derived classes inherit from one base class.
Hybrid InheritanceA 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 Sharma
Enter age: 20
Enter roll number: 101
Student Details
Name: Rahul Sharma
Age: 20
Roll Number: 101

Explanation of the Program

  • The Person class contains the data members name and age, along with functions to input and display personal details.

  • The Student class publicly inherits the Person class.

  • The Student class adds its own data member rollNo.

  • The object s of the Student class can call the accessible public functions of both classes.

इस program में:

  • Person class में name और age data members तथा personal details को input और display करने वाले functions हैं।

  • Student class, Person class को publicly inherit करती है।

  • Student class अपना अतिरिक्त data member rollNo रखती है।

  • Student class का object s, दोनों 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:

  • Person is the base class.

  • Student is derived from Person.

  • Result is derived from Student.

इस उदाहरण में:

  • Person Base 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 Verma
Enter roll number: 102
Enter marks: 85
Student Result
Name: Amit Verma
Roll Number: 102
Marks: 85

Explanation of the Program

  • The Person class stores the student's name.

  • The Student class inherits from Person and stores the roll number.

  • The Result class inherits from Student and stores marks.

  • The Result object 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 में:

  • Person class विद्यार्थी का नाम store करती है।

  • Student class, Person से inherit करके roll number store करती है।

  • Result class, Student से inherit करके marks store करती है।

  • Result class का object दोनों inheritance levels से प्राप्त accessible public functions का उपयोग कर सकता है।

  • यह program दिखाता है कि एक class inheritance की chain के माध्यम से members प्राप्त कर सकती है।

Difference Between Single and Multilevel Inheritance

BasisSingle InheritanceMultilevel Inheritance
MeaningOne derived class inherits from one base class.A class inherits from another derived class.
LevelsOne inheritance level.Two or more inheritance levels.
ClassesGenerally two classes are involved.Generally three or more classes are involved.
StructureBase → DerivedBase → Derived → Further Derived
ComplexitySimple structure.Comparatively more complex structure.
ExamplePerson → StudentPerson → 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

Popular posts from this blog

Important Questions (IMP) of Programming Methodologies and Data structures using C++ with OOP's Concepts

Complete C++ Programming Course - OOP and Modern C++

OOP's using CPP Syllabus AND Programming Methodologies and Data Structures Using C++ (NEP 2020)