Class 11-12

Class 11 Computer Science

BSEB 100-Day Study Plan
Notes, MCQs, and Solutions

Explore Class 11

Class 12 Computer Science

Advanced Topics & Practical Guide
Board Exam Preparation

Explore Class 12

Day 50: Inheritance in C++ – Types, Examples, and Advantages | Class 11, Chapter 5

Learn inheritance in C++ with Day 50 of Class 11 Computer Science, Chapter 5. Covers types, syntax, examples, and practical applications with outputs.

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)

  1. Which keyword is used for inheritance in C++?
    (a) extend | (b) inherit | (c) : (colon) | (d) None
  2. 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
  3. Which access specifier allows members to be inherited as they are?
    (a) Public | (b) Private | (c) Protected | (d) None
  4. Which type of inheritance forms a hierarchy of classes?
    (a) Single | (b) Multiple | (c) Hierarchical | (d) None
  5. Which symbol is used to implement inheritance?
    (a) . | (b) -> | (c) : | (d) None
  6. Which type of inheritance combines two or more types?
    (a) Single | (b) Hybrid | (c) Hierarchical | (d) None
  7. Which of the following is not a type of inheritance?
    (a) Single | (b) Multiple | (c) Polymorphic | (d) None
  8. What is the main advantage of inheritance?
    (a) Faster execution | (b) Code reusability | (c) Memory optimization | (d) None
  9. Which access specifier allows derived classes to access members?
    (a) Public | (b) Protected | (c) Private | (d) None
  10. 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

  1. What is inheritance in C++?
    Answer: Inheritance allows a class to acquire properties and behaviors from another class.
  2. Write the syntax for inheritance in C++.
    Answer:
    class DerivedClass : AccessSpecifier BaseClass {
        // Body
    };
    
  3. What are the types of inheritance in C++?
    Answer: Single, Multiple, Multilevel, Hierarchical, and Hybrid.
  4. What is the purpose of the protected access specifier in inheritance?
    Answer: It allows members to be accessed in the derived class but not outside.
  5. 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

  1. Explain the concept of inheritance with examples of multiple and hierarchical inheritance.
  2. Write a program to demonstrate multilevel inheritance in C++.
  3. Discuss the advantages and disadvantages of inheritance in object-oriented programming.

Post a Comment