Day 50 – Inheritance in C++
Class 11 Computer Science – Chapter 5
What is Inheritance? (Inheritance क्या है?)
Inheritance in C++ allows a class (called the derived class) to acquire the properties and behaviors of another class (called the base class). This promotes code reuse and simplifies program maintenance.
C++ में Inheritance एक क्लास (डेरिव्ड क्लास) को दूसरी क्लास (बेस क्लास) के गुण और व्यवहार प्राप्त करने की अनुमति देता है। यह कोड पुन: उपयोग और प्रोग्राम रखरखाव को सरल बनाता है।
Types of Inheritance (Inheritance के प्रकार)
- Single Inheritance: One derived class inherits from one base class.
सिंगल इनहेरिटेंस: एक डेरिव्ड क्लास एक बेस क्लास से इनहेरिट करता है। - Multiple Inheritance: One derived class inherits from multiple base classes.
मल्टीपल इनहेरिटेंस: एक डेरिव्ड क्लास कई बेस क्लास से इनहेरिट करता है। - Multilevel Inheritance: A class inherits from a derived class, forming a chain.
मल्टीलेवल इनहेरिटेंस: एक क्लास डेरिव्ड क्लास से इनहेरिट करता है, जिससे एक चेन बनती है। - Hierarchical Inheritance: Multiple derived classes inherit from a single base class.
हाइरार्किकल इनहेरिटेंस: कई डेरिव्ड क्लास एक बेस क्लास से इनहेरिट करते हैं। - Hybrid Inheritance: A combination of two or more types of inheritance.
हाइब्रिड इनहेरिटेंस: दो या अधिक प्रकार के इनहेरिटेंस का संयोजन।
Syntax of Inheritance (Inheritance की संरचना)
Inheritance is implemented using the : symbol followed by the access specifier (public, protected, or private).
Inheritance : प्रतीक का उपयोग करके लागू किया जाता है, जिसके बाद एक्सेस स्पेसिफायर (पब्लिक, प्रोटेक्टेड, या प्राइवेट) होता है।
Syntax:
class DerivedClass : AccessSpecifier BaseClass {
// Body of the derived class
};
Examples of Inheritance in C++ (C++ में Inheritance के उदाहरण)
Example 1: Single Inheritance
#include <iostream>
using namespace std;
class Base {
public:
void displayBase() {
cout << "This is the Base Class." << endl;
}
};
class Derived : public Base {
public:
void displayDerived() {
cout << "This is the Derived Class." << endl;
}
};
int main() {
Derived obj;
obj.displayBase();
obj.displayDerived();
return 0;
}
Output:
This is the Base Class. This is the Derived Class.
Example 2: Multilevel Inheritance
#include <iostream>
using namespace std;
class Grandparent {
public:
void displayGrandparent() {
cout << "This is the Grandparent Class." << endl;
}
};
class Parent : public Grandparent {
public:
void displayParent() {
cout << "This is the Parent Class." << endl;
}
};
class Child : public Parent {
public:
void displayChild() {
cout << "This is the Child Class." << endl;
}
};
int main() {
Child obj;
obj.displayGrandparent();
obj.displayParent();
obj.displayChild();
return 0;
}
Output:
This is the Grandparent Class. This is the Parent Class. This is the Child Class.
Advantages of Inheritance (Inheritance के लाभ)
- Promotes code reusability.
कोड पुन: उपयोग को बढ़ावा देता है। - Makes programs modular and easy to maintain.
प्रोग्राम्स को मॉड्यूलर और रखरखाव में आसान बनाता है। - Facilitates the addition of new features to existing code.
मौजूदा कोड में नई सुविधाएँ जोड़ने की सुविधा देता है।
Practice Questions
Multiple Choice Questions (MCQs)
- Which keyword is used for inheritance in C++?
(a) extend | (b) inherit | (c) : (colon) | (d) None - What is multilevel inheritance?
(a) A class inherits from multiple base classes
(b) A class inherits from another derived class
(c) A single class inherits from a base class
(d) None - Which access specifier allows members to be inherited as they are?
(a) Public | (b) Private | (c) Protected | (d) None - Which type of inheritance forms a hierarchy of classes?
(a) Single | (b) Multiple | (c) Hierarchical | (d) None - Which symbol is used to implement inheritance?
(a) . | (b) -> | (c) : | (d) None - Which type of inheritance combines two or more types?
(a) Single | (b) Hybrid | (c) Hierarchical | (d) None - Which of the following is not a type of inheritance?
(a) Single | (b) Multiple | (c) Polymorphic | (d) None - What is the main advantage of inheritance?
(a) Faster execution | (b) Code reusability | (c) Memory optimization | (d) None - Which access specifier allows derived classes to access members?
(a) Public | (b) Protected | (c) Private | (d) None - What is the output of the following code?
class Base { public: void show() { cout << "Base Class"; } }; class Derived : public Base { }; int main() { Derived obj; obj.show(); return 0; }(a) Base Class | (b) Derived Class | (c) Error | (d) None
Answers to MCQs:
1: (c), 2: (b), 3: (a), 4: (c), 5: (c), 6: (b), 7: (c), 8: (b), 9: (b), 10: (a)
Short Answer Questions
- What is inheritance in C++?
Answer: Inheritance allows a class to acquire properties and behaviors from another class. - Write the syntax for inheritance in C++.
Answer:class DerivedClass : AccessSpecifier BaseClass { // Body }; - What are the types of inheritance in C++?
Answer: Single, Multiple, Multilevel, Hierarchical, and Hybrid. - What is the purpose of the
protectedaccess specifier in inheritance?
Answer: It allows members to be accessed in the derived class but not outside. - Write a program to demonstrate single inheritance in C++.
Answer:class Parent { public: void display() { cout << "Parent Class"; } }; class Child : public Parent { }; int main() { Child obj; obj.display(); return 0; }
Long Answer Questions
- Explain the concept of inheritance with examples of multiple and hierarchical inheritance.
- Write a program to demonstrate multilevel inheritance in C++.
- Discuss the advantages and disadvantages of inheritance in object-oriented programming.
Post a Comment