Introduction to Inheritance in C++ - Need, Advantages, Base Class and Derived Class
Inheritance is one of the most important features of Object-Oriented Programming (OOP) in C++. It allows a new class to acquire the properties and behaviors of an existing class. Inheritance helps programmers reuse existing code, reduce duplication, and create relationships between classes.
Inheritance, C++ में Object-Oriented Programming (OOP) का एक महत्वपूर्ण feature है। इसकी सहायता से एक नई class किसी existing class की properties और behaviors को प्राप्त कर सकती है। Inheritance programmers को existing code reuse करने, code duplication कम करने और classes के बीच relationships बनाने में सहायता करता है।
What is Inheritance in C++?
Inheritance is a mechanism in C++ through which one class can acquire the data members and member functions of another class. The existing class is called the Base Class, and the new class that inherits its features is called the Derived Class.
Inheritance C++ का एक mechanism है, जिसके माध्यम से एक class दूसरी class के data members और member functions को प्राप्त कर सकती है। जिस existing class से features प्राप्त किए जाते हैं, उसे Base Class कहते हैं और जो नई class इन features को inherit करती है, उसे Derived Class कहते हैं।
For example, consider a Person class containing common information such as name and age. We can create a Student class that inherits these properties from the Person class and adds student-specific information such as roll number and marks.
उदाहरण के लिए, मान लीजिए हमारे पास Person class है, जिसमें name और age जैसी सामान्य information है। हम एक Student class बना सकते हैं, जो Person class से इन properties को inherit करे और इसमें roll number तथा marks जैसी student-specific information भी जोड़ सके।
<AsyncImage query="C++ object oriented programming inheritance UML class diagram Person base class Student derived class simple educational diagram" aspectRatio="16:9" width="100%" />
In this example:
Personis the Base Class.Studentis the Derived Class.Studentinherits the accessible members ofPerson.Studentcan also define its own additional members.
इस उदाहरण में:
PersonBase Class है।StudentDerived Class है।Student,Personके accessible members को inherit करती है।Studentअपने additional members भी define कर सकती है।
Need of Inheritance in C++
Inheritance is needed when multiple classes share common properties and functions. Instead of writing the same code repeatedly in different classes, we can define common features in a base class and reuse them through derived classes.
जब कई classes में common properties और functions होते हैं, तब Inheritance की आवश्यकता होती है। अलग-अलग classes में एक जैसा code बार-बार लिखने के बजाय हम common features को एक Base Class में define कर सकते हैं और Derived Classes के माध्यम से उनका पुनः उपयोग कर सकते हैं।
Consider an application that manages students, teachers, and employees. All these entities may have common information such as name, age, and address. Instead of defining these members separately in every class, we can create a common Person base class.
मान लीजिए एक application में students, teachers और employees का management किया जाता है। इन सभी में name, age और address जैसी common information हो सकती है। प्रत्येक class में इन members को अलग-अलग define करने के बजाय हम एक common Person Base Class बना सकते हैं।
<AsyncImage query="UML inheritance hierarchy Person base class Student Teacher Employee derived classes educational diagram" aspectRatio="16:9" width="100%" />
This approach reduces repeated code and makes the program easier to maintain.
इस approach से repeated code कम होता है और program को maintain करना आसान हो जाता है।
Advantages of Inheritance in C++
Inheritance provides several advantages in object-oriented programming.
Object-Oriented Programming में Inheritance के कई लाभ हैं।
Code Reusability
Inheritance allows derived classes to reuse accessible data members and member functions of the base class. This reduces the need to write the same code again.
Inheritance के माध्यम से Derived Classes, Base Class के accessible data members और member functions को reuse कर सकती हैं। इससे एक ही code को बार-बार लिखने की आवश्यकता कम होती है।
Reduction in Code Duplication
Common properties and functions can be defined once in a base class and shared by multiple derived classes.
Common properties और functions को एक Base Class में एक बार define करके कई Derived Classes में उपयोग किया जा सकता है।
Easy Maintenance
When common functionality is defined in a base class, changes to that shared functionality can be managed centrally, reducing maintenance effort.
जब common functionality को Base Class में define किया जाता है, तब उस shared functionality में बदलाव को एक ही स्थान से manage किया जा सकता है। इससे maintenance का कार्य आसान होता है।
Extensibility
New classes can be created from existing classes by adding additional properties and functions without rewriting all the existing code.
Existing classes से नई classes बनाकर उनमें additional properties और functions जोड़े जा सकते हैं, बिना पूरे existing code को दोबारा लिखे।
Supports Hierarchical Classification
Inheritance helps represent real-world relationships in which different specialized classes share common features.
Inheritance वास्तविक जीवन के उन relationships को represent करने में मदद करता है, जिनमें अलग-अलग specialized classes कुछ common features साझा करती हैं।
Supports Polymorphism
Inheritance provides the foundation for runtime polymorphism through virtual functions. A derived class can override virtual functions of its base class.
Inheritance, virtual functions के माध्यम से Runtime Polymorphism का आधार प्रदान करता है। Derived Class, Base Class के virtual functions को override कर सकती है।
Base Class in C++
A Base Class is an existing class whose accessible members are inherited by another class. It is also known as a Parent Class or Superclass.
Base Class वह existing class होती है जिसके accessible members को दूसरी class inherit करती है। इसे Parent Class या Superclass भी कहा जाता है।
For example:
उदाहरण:
class Person {public:string name;int age;void displayPerson() {cout << "Name: " << name << endl;cout << "Age: " << age << endl;}};
In this example, Person is the base class. It contains two data members, name and age, and one member function, displayPerson().
इस उदाहरण में Person Base Class है। इसमें दो data members name और age तथा एक member function displayPerson() है।
Derived Class in C++
A Derived Class is a new class that inherits accessible members from an existing base class. It is also known as a Child Class or Subclass.
Derived Class वह नई class होती है जो किसी existing Base Class से accessible members को inherit करती है। इसे Child Class या Subclass भी कहा जाता है।
For example:
उदाहरण:
class Student : public Person {public:int rollNo;void displayStudent() {cout << "Roll Number: " << rollNo << endl;}};
Here, Student is the derived class, and Person is the base class. The public keyword specifies the inheritance mode.
यहाँ Student Derived Class है और Person Base Class है। public keyword inheritance mode को specify करता है।
The Student class can use the accessible members inherited from Person and can also define its own members, such as rollNo and displayStudent().
Student class, Person से inherit किए गए accessible members का उपयोग कर सकती है और अपने members जैसे rollNo तथा displayStudent() भी define कर सकती है।
Syntax of Inheritance in C++
The general syntax of inheritance in C++ is:
C++ में Inheritance का सामान्य syntax निम्नलिखित है:
class DerivedClassName : access_specifier BaseClassName {// Members of derived class};
Here:
classis the keyword used to declare a class.DerivedClassNameis the name of the new class.access_specifierdefines the inheritance mode, such aspublic,private, orprotected.BaseClassNameis the name of the existing class being inherited.
यहाँ:
classएक class declare करने के लिए उपयोग किया जाने वाला keyword है।DerivedClassNameनई class का नाम है।access_specifierinheritance mode को define करता है, जैसेpublic,privateयाprotected।BaseClassNameउस existing class का नाम है जिससे inheritance प्राप्त किया जाता है।
C++ Program to Demonstrate Single Inheritance
The following program demonstrates how a Student class inherits the accessible members of a Person class.
निम्नलिखित program में दिखाया गया है कि किस प्रकार Student class, Person class के accessible members को inherit करती है।
Source Code
#include <iostream>#include <string>using namespace std;class Person {public:string name;int age;void getPersonDetails() {cout << "Enter Name: ";cin >> name;cout << "Enter Age: ";cin >> age;}void displayPersonDetails() {cout << "Name: " << name << endl;cout << "Age: " << age << endl;}};class Student : public Person {public:int rollNo;float marks;void getStudentDetails() {cout << "Enter Roll Number: ";cin >> rollNo;cout << "Enter Marks: ";cin >> marks;}void displayStudentDetails() {cout << "Roll Number: " << rollNo << endl;cout << "Marks: " << marks << endl;}};int main() {Student s1;cout << "Enter Student Details:" << endl;s1.getPersonDetails();s1.getStudentDetails();cout << "\nStudent Information:" << endl;s1.displayPersonDetails();s1.displayStudentDetails();return 0;}
Output
Enter Student Details:Enter Name: RahulEnter Age: 20Enter Roll Number: 101Enter Marks: 85.5Student Information:Name: RahulAge: 20Roll Number: 101Marks: 85.5
Explanation of the Program
The
Personclass is created as the base class.It contains the data members
nameandage.The
Studentclass inherits thePersonclass usingpublicinheritance.The
Studentclass adds its own data members,rollNoandmarks.The
Studentobjects1can call the accessible member functions of both classes.The program accepts and displays complete student information.
Personclass को Base Class के रूप में बनाया गया है।इसमें
nameऔरagedata members हैं।Studentclass,publicinheritance का उपयोग करकेPersonclass को inherit करती है।Studentclass अपने data membersrollNoऔरmarksभी जोड़ती है।Studentobjects1, दोनों classes के accessible member functions को call कर सकता है।यह program student की पूरी information input और display करता है।
Difference Between Base Class and Derived Class
Base Class and Derived Class are two fundamental concepts of inheritance in C++.
Base Class और Derived Class, C++ Inheritance के दो fundamental concepts हैं।
| Base Class | Derived Class |
|---|---|
| It is the existing class. | It is the new class that inherits another class. |
| It provides common features. | It reuses and extends inherited features. |
| Also called Parent Class or Superclass. | Also called Child Class or Subclass. |
| It can be inherited by one or more derived classes. | It can inherit from one or more base classes. |
| It defines general properties and behavior. | It can define specialized properties and behavior. |
| Base Class | Derived Class |
|---|---|
| यह existing class होती है। | यह नई class होती है जो दूसरी class को inherit करती है। |
| यह common features प्रदान करती है। | यह inherited features को reuse और extend करती है। |
| इसे Parent Class या Superclass भी कहते हैं। | इसे Child Class या Subclass भी कहते हैं। |
| इसे एक या अधिक Derived Classes inherit कर सकती हैं। | यह एक या अधिक Base Classes को inherit कर सकती है। |
| यह general properties और behavior define करती है। | यह specialized properties और behavior define कर सकती है। |
Frequently Asked Questions (FAQs)
1. What is inheritance in C++?
Inheritance is an OOP feature that allows one class to acquire accessible members of another class.
Inheritance एक OOP feature है, जो एक class को दूसरी class के accessible members प्राप्त करने की सुविधा देता है।
2. What is a base class?
A base class is an existing class whose accessible members are inherited by another class. It is also called a parent class.
Base Class वह existing class होती है जिसके accessible members को दूसरी class inherit करती है। इसे Parent Class भी कहते हैं।
3. What is a derived class?
A derived class is a class that inherits accessible members from another class. It is also called a child class.
Derived Class वह class होती है जो दूसरी class से accessible members inherit करती है। इसे Child Class भी कहते हैं।
4. What is the main purpose of inheritance?
The main purpose of inheritance is code reusability, reducing code duplication, and establishing relationships between classes.
Inheritance का मुख्य उद्देश्य code reusability, code duplication को कम करना और classes के बीच relationships स्थापित करना है।
5. Can a derived class have its own data members and functions?
Yes. A derived class can have its own data members and member functions in addition to the accessible members inherited from its base class.
हाँ। Derived Class, Base Class से inherit किए गए accessible members के अतिरिक्त अपने data members और member functions भी रख सकती है।
6. What is the difference between inheritance and composition?
Inheritance represents an IS-A relationship, whereas composition represents a HAS-A relationship. For example, a Student IS-A Person, while a Car HAS-A Engine.
Inheritance, IS-A relationship को दर्शाता है, जबकि Composition, HAS-A relationship को दर्शाता है। उदाहरण के लिए, Student एक Person है, जबकि Car के पास एक Engine होता है।
7. Does a derived class inherit private members of a base class?
Private members are part of the base class subobject, but they cannot be accessed directly by the derived class. They can be accessed through suitable public or protected member functions provided by the base class.
Private members Base Class के subobject का हिस्सा होते हैं, लेकिन Derived Class उन्हें सीधे access नहीं कर सकती। उन्हें Base Class द्वारा उपलब्ध कराए गए उपयुक्त public या protected member functions के माध्यम से access किया जा सकता है।
Inheritance is a fundamental feature of C++ that promotes code reusability and supports the development of organized, extensible programs. A base class provides common features, while a derived class inherits accessible members and adds specialized functionality. Understanding these concepts is essential before studying the different types of inheritance in C++.
Inheritance C++ का एक fundamental feature है, जो code reusability को बढ़ावा देता है और organized तथा extensible programs बनाने में सहायता करता है। Base Class common features प्रदान करती है, जबकि Derived Class accessible members को inherit करके specialized functionality जोड़ती है। C++ में Inheritance के विभिन्न types को समझने से पहले इन concepts का अध्ययन करना आवश्यक है।
Comments
Post a Comment