Class 12 Computer Science (C++) – Chapter 5 Inheritance

Chapter 5 explores the powerful object-oriented programming concept of Inheritance in C++. Inheritance enables classes to derive properties and behaviors from other classes, promoting code reusability, modularity, and logical hierarchy in software development. It allows new classes to adopt the characteristics of existing ones, forming a foundation for real-world modeling and hierarchical relationships in programs.

This chapter provides students with a deep understanding of how to implement inheritance in C++, including syntax, types of inheritance, access specifiers, and the concept of base and derived classes.

What You Will Learn in Chapter 5

This chapter equips students with the knowledge to reuse and extend existing class functionality using inheritance. By mastering inheritance, students can model complex relationships and reduce redundancy in code through class hierarchies.

Key Topics Covered

Introduction to Inheritance

  • Definition and importance of inheritance in object-oriented programming.

  • Real-life analogies of inheritance (e.g., Parent → Child, Animal → Dog).

  • Reusability of data members and member functions of a base class.

Base Class and Derived Class

  • Syntax for deriving a class:
    class Derived : access_specifier Base { };

  • Understanding how members of the base class are inherited.

  • Role of access specifiers: public, protected, and private inheritance.

  • Visibility of base class members in the derived class based on inheritance type.

Types of Inheritance

  • Single Inheritance: One base class, one derived class.

  • Multiple Inheritance: One derived class inherits from two or more base classes.

  • Multilevel Inheritance: Derived class acts as base for another derived class.

  • Hierarchical Inheritance: One base class with multiple derived classes.

  • Hybrid Inheritance: Combination of more than one type of inheritance.

Access Specifiers and Member Visibility

  • Public, private, and protected members in inheritance.

  • Behavior of each access specifier when used in different types of inheritance.

  • How visibility rules affect inheritance and accessibility.

Constructor and Destructor in Inheritance

  • Order of constructor and destructor calls in base and derived classes.

  • Constructor invocation in multilevel inheritance.

  • Use of initialization list in constructor of derived classes.

Overriding and Scope Resolution

  • Function overriding in derived classes.

  • Use of the scope resolution operator (::) to access base class members.

  • Difference between function overloading and overriding.

Ambiguity in Multiple Inheritance

  • The Diamond Problem: A case of ambiguity in multiple inheritance.

  • Role of virtual base classes to resolve ambiguity.

  • Syntax for virtual inheritance:
    class Derived : virtual public Base { };

Download Chapter 5 Solutions PDF – Inheritance

Our comprehensive PDF includes:

  • NCERT textbook solutions for all exercises and intext questions.

  • Syntax guides and inheritance structure diagrams.

  • Output-based questions and answers.

  • Sample programs using single, multiple, and multilevel inheritance.

  • Code illustrations of constructor/destructor behavior in inheritance.

Highlights of Our NCERT Solutions

  • Easy-to-follow inheritance diagrams with class structures.

  • Real C++ code examples for each inheritance type.

  • Concept clarification for access specifiers and function overriding.

  • Debugging tips for inheritance-related errors and scope confusion.

  • Comparison charts for different inheritance types and visibility rules.

Recommended Preparation Tips

  • Practice writing inheritance programs using various combinations (single, multilevel, etc.).

  • Memorize visibility rules of access specifiers in inheritance.

  • Understand the sequence of constructor/destructor execution.

  • Explore overriding and virtual base classes with practical examples.

  • Solve previous year questions and output prediction exercises.

Additional Study Resources

  • Flashcards: Types of inheritance, access specifiers, visibility rules.

  • Worksheets: Identify output, error correction, and concept mapping.

  • Inheritance tree diagrams for visualization.

  • CBSE PYQs and sample paper questions on inheritance.

  • Code debugging exercises based on multiple and hybrid inheritance.

Mastering Chapter 5 – Inheritance

Mastering inheritance empowers students to build scalable and maintainable object-oriented applications. It strengthens their ability to design real-world software solutions that use class hierarchies, modularity, and abstraction.

By understanding inheritance deeply, students are well-prepared to move into more advanced concepts such as polymorphism, runtime binding, and file handling in later chapters of Class 12 C++ programming.

Class 12 Computer Science (C++) – Chapter 5 Inheritance

Long Answer Type Questions

Question 1

Answer the questions (i) to (iv) based on the following:

class ITEM {
    int ID;
    char IName[20];
protected:
    float Qty;
public:
    ITEM();
    void Enter();
    void View();
};
class TRADER {
    int DCode;
protected:
    char Manager[20];
public:
    TRADER();
    void Enter();
    void View();
};
class SALEPOINT : public ITEM, private TRADER {
    char Name[20], Location[20];
public:
    SALEPOINT();
    void EnterAll();
    void ViewAll();
};
  1. Type of Inheritance:
    Multiple Inheritance
  2. Data members directly accessible from SALEPOINT member functions:
    Name, Location, Manager, Qty
  3. Member functions directly accessible by an object of SALEPOINT:
    EnterAll(), ViewAll()
  4. Order of constructor execution:
    ITEM(), TRADER(), SALEPOINT()

Question 2

Given the following class definition, answer the questions:

class University {
    char name[20];
protected:
    char vc[20];
public:
    void estd();
    void inputdata();
    void outputdata();
};
class College : protected University {
    int regno;
protected:
    char principal[20];
public:
    int no_of_students;
    void readdata();
    void dispdata();
};
class Department : public College {
    char name[20];
    char HOD[20];
public:
    void fetchdata(int);
    void displaydata();
};
  1. Base class of College:
    University
  2. Derived class of College:
    Department
  3. Data members accessible from function displaydata():
    name, principal, no_of_students, vc
  4. Type of Inheritance:
    Multilevel Inheritance
  5. Size of object of class Department:
    85 bytes

Question 3

Answer the questions (i) to (iv) based on the following:

class Exterior {
    int orderId;
    char Address[20];
protected:
    float Advance;
public:
    Exterior();
    void Book();
    void View();
};
class Paint : public Exterior {
    int WallArea, ColorCode;
protected:
    char Type;
public:
    Paint();
    void PBook();
    void PView();
};
class Bill : public Paint {
    float Charges;
    void Calculate();
public:
    Bill();
    void Billing();
    void Print();
};
  1. Type of Inheritance:
    Multilevel Inheritance
  2. Data members accessible from class Paint:
    WallArea, ColorCode, Type, Advance
  3. Member functions accessible by an object of class Bill:
    Billing(), Print(), PBook(), PView(), Book(), View()
  4. Constructor execution order:
    Exterior(), Paint(), Bill()

Question 4

Definition of class Photo:

class Photo {
    int Pno;
    char Category[20];
    char Exhibit[20];
    void FixExhibit();
public:
    void Register();
    void ViewAll();
};
void Photo::FixExhibit() {
    if (strcmpi(Category, \"Antique\") == 0)
        strcpy(Exhibit, \"Zaveri\");
    else if (strcmpi(Category, \"Modern\") == 0)
        strcpy(Exhibit, \"Johnsen\");
    else if (strcmpi(Category, \"Classic\") == 0)
        strcpy(Exhibit, \"Terenida\");
}
void Photo::Register() {
    cin >> Pno;
    gets(Category);
    FixExhibit();
}
void Photo::ViewAll() {
    cout << Pno << \" \" << Category << \" \" << Exhibit << endl;
}

Category to Exhibit Mapping Table:

CategoryExhibit
AntiqueZaveri
ModernJohnsen
ClassicTerenida

Question 5

Answer the questions (i) to (iv) based on the following:

class Interior {
    int OrderId;
    char Address[20];
protected:
    float Advance;
public:
    Interior();
    void Book();
    void View();
};
class Painting : public Interior {
    int WallArea, ColorCode;
protected:
    char Type;
public:
    Painting();
    void PBook();
    void PView();
};
class Billing : public Painting {
    float Charges;
    void Calculate();
public:
    Billing();
    void Bill();
    void BillPrint();
};
  1. Type of Inheritance:
    Multilevel Inheritance
  2. Data members accessible from class Painting:
    WallArea, ColorCode, Type, Advance
  3. Member functions accessible by an object of class Billing:
    Bill(), BillPrint(), PBook(), PView(), Book(), View()
  4. Constructor execution order:
    Interior(), Painting(), Billing()

Question 6:

Answer the questions (i) to (iv) based on the following C++ code:

class AC {
    char Model[10];
    char Date_of_purchase[10];
    char Company[20];
public:
    AC();
    void enter_car_detail();
    void show_car_detail();
};
class Accessories : protected AC {
protected:
    char Stabilizer[30];
    char AC_cover[20];
public:
    float Price;
    Accessories();
    void enter_accessories_details();
    void show_accessories_details();
};
class Dealer : public Accessories {
    int No_of_dealers;
    char dealers_name[20];
    int No_of_products;
public:
    Dealer();
    void enter_details();
    void show_details();
};

Answers:

  1. How many bytes will be required by an object of class Dealer and class Accessories?
    ClassEstimated Memory
    Dealer118 bytes
    Accessories98 bytes
  2. Which type of inheritance is illustrated?

    Multilevel Inheritance

    Base Class: AC
    Derived Class: Dealer

  3. Names of all the members accessible from objects of class Dealer:
    • enter_details()
    • show_details()
    • Price
    • enter_accessories_details()
    • show_accessories_details()
  4. Members accessible from member functions of class Dealer:
    • No_of_dealers
    • dealers_name
    • No_of_products
    • Stabilizer
    • AC_cover
    • Price
    • enter_car_detail()
    • show_car_detail()
    • enter_accessories_details()
    • show_accessories_details()
    • enter_details()
    • show_details()

Question 7:

Consider the following class definition and derive another class:

class State {
protected:
    int tp; // number of tourist places
public:
    State() { tp = 0; }
    void inctp() { tp++; }
    int gettp() { return tp; }
};

Requirement:

Create a derived class District publicly from State with the following:

Data MembersType
distnamechar[50]
populationlong
Member FunctionsDescription
dinput()To input distname and population
doutput()To display distname and population

Answer:

class District : public State {
private:
    char distname[50];
    long population;
public:
    void dinput() {
        cout << \"Enter district name: \";
        cin.getline(distname, 50);
        cout << \"Enter population: \"; cin >> population;
    }
    void doutput() {
        cout << \"District Name: \" << distname << endl;
        cout << \"Population: \" << population << endl;
    }
};

Question 8:

Define a class DanceAcademy in C++ with the following specifications:

Private Members:

  • Enrollno (int)
  • Name (string)
  • Style (string)
  • Fee (float)
  • chkfee() – To assign fee based on Style:
    StyleFee (INR)
    Classical10000
    Western8000
    Freestyle11000

Public Members:

  • enrollment() – To input details and call chkfee()
  • display() – To show all details

Answer:

class DanceAcademy {
    int Enrollno;
    char Name[20];
    char Style[20];
    float Fee;
    void chkfee() {
        if (strcmpi(Style, \"Classical\") == 0)
            Fee = 10000;
        else if (strcmpi(Style, \"Western\") == 0)
            Fee = 8000;
        else if (strcmpi(Style, \"Freestyle\") == 0)
            Fee = 11000;
    }
public:
    void enrollment() {
        cout << \"Enter Enroll No, Name, and Style:n\"; cin >> Enrollno;
        cin.ignore();
        cin.getline(Name, 20);
        cin.getline(Style, 20);
        chkfee();
    }
    void display() {
        cout << \"nEnroll No: \" << Enrollno
             << \"nName: \" << Name
             << \"nStyle: \" << Style
             << \"nFee: \" << Fee << endl;
    }
};

Question 9:

Consider the following C++ code and answer:

class Campus {
    long Id;
    char City[20];
protected:
    char Country[20];
public:
    Campus();
    void Register();
    void Display();
};
class Dept : private Campus {
    long DCode[10];
    char HOD[20];
protected:
    double Budget;
public:
    Dept();
    void Enter();
    void Show();
};
class Applicant : public Dept {
    long RegNo;
    char Name[20];
public:
    Applicant();
    void Enroll();
    void View();
};

Answers:

  1. Type of Inheritance: Multilevel Inheritance
  2. Member functions accessible from objects of class Applicant:
    • Enroll()
    • View()
    • Enter()
    • Show()
  3. Data members accessible within class Applicant:
    • RegNo
    • Name
    • Budget
  4. Can function Display() of class Campus be called from object of Dept?
    No

Question 10:

Analyze the following class design:

class ITEM {
    char ICode[10];
protected:
    char IName[20];
public:
    ITEM();
    void Enter();
    void Display();
};
class SUPPLIER {
    char SCode[10];
protected:
    char SName[25];
public:
    SUPPLIER();
    void TEnter();
    void TDisplay();
};
class SHOP : private SUPPLIER, public ITEM {
    char SHOPADDRESS[15], SEmail[25];
public:
    SHOP();
    void Enter();
    void Display();
};

Answers:

  1. Type of Inheritance: Multiple Inheritance
  2. Member functions accessible from Enter() function of class SHOP:
    • Enter()
    • Display()
    • TEnter()
    • TDisplay()
  3. Member functions accessible from object of class SHOP:
    • Enter()
    • Display()
  4. Order of constructor execution when object of class SHOP is created:
    1. ITEM()
    2. SUPPLIER()
    3. SHOP()

Question 11

Consider the following C++ code and answer the questions from (i) to (iv):

class Personal {
    int Class, Rno;
    char Section;
protected:
    char Name[20];
public:
    Personal();
    void Pentry();
    void Pdisplayf();
};
class Marks : private Personal {
    float M[5];
protected:
    char Grade[5];
public:
    Marks();
    void Mentry();
    void Mdisplay();
};
class Result : public Marks {
    float Total, Agg;
public:
    char FinalGrade, Comments[20];
    Result();
    void Rcalculate();
    void Rdisplayt();
};

Answers:

  1. Type of Inheritance: Multilevel Inheritance
  2. Data members directly accessible from class Result objects:
    FinalGrade, Comments[20]
  3. Member functions directly accessible from class Result objects:
    Rcalculate(), Rdisplayt(), Mentry(), Mdisplay()
  4. Data members accessible within Mentry() of Marks class:
    M[5], Grade[5], Name[20] (inherited as private)

Question 12

class Student {
    int Class, Rno;
    char Section;
protected:
    char SName[20];
public:
    Student();
    void Stentry();
    void Stdisplay();
};
class Score : private Student {
    float Marks[5];
protected:
    char Grade[5];
public:
    Score();
    void Sentry();
    void Sdisplay();
};
class Report : public Score {
    float Total, Avg;
public:
    char OverallGrade, Remarks[20];
    Report();
    void Revaluate();
    void RPrint();
};

Answers:

  1. Type of Inheritance: Multilevel Inheritance
  2. Data members accessible from class Report objects:
    Total, Avg, OverallGrade, Remarks[20]
  3. Member functions accessible from Report objects:
    Revaluate(), RPrint(), Sentry(), Sdisplay()
  4. Data members accessible within Sentry():
    Marks[5], Grade[5], SName, Class, Rno, Section

Question 13

class COMPANY {
    char Location[20];
    double budget, income;
protected:
    void Accounts();
public:
    COMPANY();
    void Register();
    void Show();
};
class FACTORY : public COMPANY {
    char Location[20];
    int Workers;
protected:
    double salary;
    void Computer();
public:
    FACTORY();
    void Enter();
    void Show();
};
class SHOP : private COMPANY {
    char Location[20];
    float Area;
    double Sale;
public:
    SHOP();
    void Input();
    void Output();
};

Answers:

  1. Type of Inheritance: Hierarchical Inheritance
  2. Data members accessible within SHOP member functions:
    Location, Area, Sale
  3. Member functions accessible from FACTORY objects:
    Enter(), Show(), Register(), Accounts()
  4. Data members accessible from SHOP objects: None

Question 14

class ORGANIZATION {
    char Address[20];
    double budget, income;
protected:
    void Compute();
public:
    ORGANIZATION();
    void Get();
    void Show();
};
class WORKAREA : public ORGANIZATION {
    char Address[20];
    int staff;
protected:
    double pay;
    void Calculate();
public:
    WORKAREA();
    void Enter();
    void Display();
};
class SHOWROOM : private ORGANIZATION {
    char Address[20];
    float Area;
    double Sale;
public:
    SHOWROOM();
    void Enter();
    void Show();
};

Answers:

  1. Type of Inheritance: Hierarchical Inheritance
  2. Data members accessible from SHOWROOM member functions:
    Address, Area, Sale, budget, income
  3. Member functions accessible from WORKAREA objects:
    Enter(), Display(), Get(), Show()
  4. Accessible from SHOWROOM objects:
    Functions: Enter(), Show()
    Data Members: None

Question 15

class indoor_sports {
    int i_id;
    char i_name[20];
    char i_coach[20];
protected:
    int i_rank, i_fee;
    void get_ifee();
public:
    indoor_sports();
    void iEntry();
    void ishow();
};
class outdoor_sports {
    int o_id;
    char o_name[20];
    char o_coach[20];
protected:
    int orank, ofee;
    void get_ofee();
public:
    outdoor_sports();
    void oshow();
};
class sports : public indoor_sports, protected outdoor_sports {
    char rules[20];
public:
    sports();
    void registration();
    void showdata();
};

Answers:

  1. Type of Inheritance: Multiple Inheritance
  2. Members accessible from outdoor_sports objects:
    Functions: oshow()
    Data Members: None
  3. Member functions accessible from sports objects:
    registration(), showdata(), iEntry(), ishow()
  4. Size of indoor_sports object: 46 bytes

Question 16

Class Definition for DISTRICT

class DISTRICT {
    int DCode;
    char DName[20];
    long int DPop;
    float Area;
    float Density;
    void DenCal(); // Calculates Density = DPop / Area
public:
    void Input();
    void ShowALL();
};

Function Definitions:

void DISTRICT::Input() {
    cin >> DCode;
    cin.ignore();
    cin.getline(DName, 20);
    cin >> DPop;
    cin >> Area;
    DenCal();
}
void DISTRICT::ShowALL() {
    cout << DCode << \" \" << DName << \" \" << DPop << \" \" << Area << \" \" << Density; if (Density > 12000)
        cout << \"Highly Populated Area\";
}
void DISTRICT::DenCal() {
    Density = DPop / Area;
}

Question 17

class PACKAGE {
    int PCode;
    char PDes[20];
protected:
    float PQty;
public:
    PACKAGE();
    void In();
    void Disp();
};
class TRANSPORT {
    int TCode;
protected:
    char TName[20];
public:
    TRANSPORT();
    void Enter();
    void Display();
};
class DELIVERY : public PACKAGE, private TRANSPORT {
    char Address[40], Date[12];
public:
    DELIVERY();
    void Input();
    void Show();
};

Answers:

  1. Type of Inheritance: Multiple Inheritance
  2. Data members directly accessible within DELIVERY:
    PQty, TName, Address, Date
  3. Member functions accessible by DELIVERY object:
    Input(), Show(), In(), Disp()
  4. Order of Constructor Execution:
    1. PACKAGE()
    2. TRANSPORT()
    3. DELIVERY()