CBSE Class 12 Computer Science C++ NCERT Solutions Chapter 5: Inheritance

NCERT Solutions PDF Class 12 PDF

This chapter provides NCERT Solutions for Class 12 Computer Science (C++) focusing on Inheritance. It covers fundamental concepts of inheritance, including single-level, multi-level, and multiple inheritance. The solutions explain how to define base and derived classes, access specifiers (public, protected, private), and the order of constructor and destructor execution. Key topics include understanding the relationships between classes, accessing members of base classes from derived classes, and applying inheritance to model real-world scenarios. These solutions are designed to help students grasp the intricacies of object-oriented programming principles in C++ and prepare effectively for their board examinations by offering clear explanations and step-by-step problem-solving.

Quick info

BoardCBSE
ClassClass 12
SubjectComputer Science (C++)
Session2026
LanguageEnglish
TypeNCERT Solutions
ChapterChapter 5

Chapter summary

Chapter 5 on Inheritance in CBSE Class 12 Computer Science (C++) NCERT Solutions delves into the core principles of object-oriented programming. It covers different types of inheritance like single, multi-level, and multiple, and explains how derived classes inherit properties from base classes. The solutions focus on understanding member accessibility, constructor/destructor execution order, and practical application through example problems. This chapter is crucial for building complex software systems by promoting code reusability and establishing relationships between classes.

Learning outcomes

  • Understand the concept of Inheritance in C++.
  • Identify different types of inheritance: Single, Multi-level, and Multiple.
  • Determine the accessibility of data members and member functions in derived classes.
  • Explain the order of constructor and destructor calls in inheritance.
  • Analyze and solve problems related to inheritance using C++ syntax.

Topics covered

Paper topics

  • Inheritance
  • Base Class
  • Derived Class
  • Single Level Inheritance
  • Multi Level Inheritance
  • Multiple Inheritance
  • Access Specifiers (Public, Protected, Private)
  • Constructor Execution Order
  • Member Accessibility

Important topics

  • Multiple Inheritance
  • Multi Level Inheritance
  • Access Specifiers in Inheritance
  • Constructor Execution Order

PDF preview

Read page by page below. PDF is streamed from the official NCERT website — no download button on this page.

Loading document …
Page of
Loading page …

Questions and Solutions

Question 1

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

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. Which type of Inheritance out of the following is illustrated in the above example? Single Level Inheritance, Multi Level Inheritance, Multiple Inheritance
  2. Write the names of all the data members, which are directly accessible from the member functions of class SALEPOINT.
  3. Write the names of all the member functions, which are directly accessible by an object of class SALEPOINT.
  4. What will be the order of execution of the constructors, when an object of class SALEPOINT is declared?
Solution:
  1. The example illustrates Multiple Inheritance. This is because the class SALEPOINT is derived from two base classes, ITEM and TRADER, simultaneously. In Single Level Inheritance, a class derives from only one base class. In Multi Level Inheritance, a class derives from a class that itself has derived from another class (forming a chain).

  2. The data members directly accessible from the member functions of class SALEPOINT are:

    • Name[20] and Location[20]: These are public members of the SALEPOINT class itself.
    • Qty: This is a protected member of the base class ITEM. Since SALEPOINT inherits publicly from ITEM, its protected members are accessible to the member functions of SALEPOINT.
    • DCode and Manager[20]: These members are declared in the TRADER class. However, SALEPOINT inherits privately from TRADER. Private inheritance means that protected and public members of the base class (TRADER) become private members of the derived class (SALEPOINT), and thus are not directly accessible by SALEPOINT's member functions.

    Therefore, the directly accessible data members are Name, Location, and Qty.

  3. The member functions directly accessible by an object of class SALEPOINT are:

    • EnterAll() and ViewAll(): These are public member functions of the SALEPOINT class itself.
    • Enter() and View() (from ITEM): These are public members of the base class ITEM. Since SALEPOINT inherits publicly from ITEM, these functions are accessible.
    • Enter() and View() (from TRADER): These are public members of the base class TRADER. However, because SALEPOINT inherits privately from TRADER, these functions become private to SALEPOINT and are not directly accessible by an object of SALEPOINT.

    Thus, the directly accessible member functions are EnterAll(), ViewAll(), Enter(), and View() (inherited from ITEM).

  4. When an object of class SALEPOINT is declared, the constructors are executed in the following order:

    1. ITEM(): The constructor of the first base class listed in the inheritance declaration (public ITEM).
    2. TRADER(): The constructor of the second base class (private TRADER).
    3. SALEPOINT(): The constructor of the derived class itself.

    So, the order is: ITEM(), TRADER(), SALEPOINT().

Question 2

Given the following class definitions, answer the questions that follow:

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); };

Answer the following questions:

  1. Which type of inheritance is used between University and College?
  2. Which type of inheritance is used between College and Department?
  3. Write the names of all the data members of class University, which are accessible by the member functions of class Department.
  4. Write the names of all the data members of class College, which are accessible by the member functions of class Department.
  5. Write the names of all the member functions of class University, which are accessible by the member functions of class Department.
Solution:
  1. The inheritance used between University and College is Protected Inheritance. This is because the class College is defined as protected University. In protected inheritance, public members of the base class become protected in the derived class, and protected members of the base class remain protected in the derived class.

  2. The inheritance used between College and Department is Public Inheritance. This is indicated by the definition class Department : public College. In public inheritance, public members of the base class remain public in the derived class, and protected members of the base class remain protected in the derived class.

  3. The data members of class University that are accessible by the member functions of class Department are:

    • name[20]: This is a public member of University. However, since College inherits privately (effectively, as protected inheritance makes public members protected) from University, name becomes protected in College. Then, since Department inherits publicly from College, this protected member remains accessible as protected within Department.
    • vc[20]: This is a protected member of University. Due to protected inheritance from University to College, it remains protected in College. Subsequently, due to public inheritance from College to Department, it remains accessible as protected within Department.

    Therefore, both name and vc are accessible.

  4. The data members of class College that are accessible by the member functions of class Department are:

    • regno: This is a public member of College. Since Department inherits publicly from College, regno is directly accessible.
    • no_of_students: This is also a public member of College and is directly accessible by Department due to public inheritance.
    • principal[20]: This is a protected member of College. Due to public inheritance, it remains protected and is accessible by the member functions of Department.
    • name[20] (from University): As explained in the previous point, this member from University becomes protected in College and remains accessible as protected in Department.
    • vc[20] (from University): Similarly, this member from University is protected in College and remains accessible as protected in Department.

    Thus, regno, no_of_students, principal, and the inherited members name and vc are accessible.

  5. The member functions of class University that are accessible by the member functions of class Department are:

    • estd(): This is a public member function of University. Through protected inheritance to College, it becomes protected in College. Then, through public inheritance to Department, it remains accessible as a protected member function within Department.
    • inputdata(): Similar to estd(), this public function of University becomes protected in College and remains accessible as protected in Department.
    • outputdata(): This public function of University also becomes protected in College and remains accessible as protected in Department.

    Therefore, estd(), inputdata(), and outputdata() are accessible.

Common mistakes

  • Confusing different types of inheritance.
  • Incorrectly determining the accessibility of inherited members.
  • Errors in predicting the order of constructor/destructor execution.
  • Misunderstanding the implications of access specifiers (public, protected, private) in derived classes.

Revision tips

  • Review the definitions and examples of each inheritance type.
  • Pay close attention to the access specifiers and their impact on member visibility.
  • Trace the execution flow of constructors and destructors for different inheritance scenarios.
  • Practice solving problems that involve creating hierarchies of classes.

Practice MCQs

Q1. Which type of inheritance allows a class to inherit from multiple base classes?

Q2. In the given SALEPOINT class example, which data member is directly accessible from its member functions?

Q3. What is the order of constructor execution when an object of SALEPOINT is declared?

Q4. If a class 'C' inherits from class 'B', which in turn inherits from class 'A', what type of inheritance is this?

Frequently asked questions

What is Inheritance in C++?

Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows a new class (derived class) to inherit properties (data members and member functions) from an existing class (base class). This promotes code reusability and establishes a relationship between classes.

What are the different types of inheritance covered in this chapter?

This chapter covers Single Level Inheritance (one base class to one derived class), Multi Level Inheritance (a chain of inheritance, e.g., A -> B -> C), and Multiple Inheritance (one derived class inheriting from multiple base classes).

How does the order of constructors work in inheritance?

When an object of a derived class is created, the constructors are called in the order of their base class declarations. For example, if class D inherits from B and C, the constructors will be called in the order: B's constructor, C's constructor, and then D's constructor.

Which members are directly accessible by the member functions of a derived class?

Member functions of a derived class can directly access public members of its base class(es) and protected members of its base class(es). Private members of the base class are not directly accessible.

Why is understanding inheritance important for Class 12 Computer Science students?

Understanding inheritance is crucial as it's a core OOP principle. It helps in designing efficient and modular programs, understanding complex class structures, and is frequently tested in board examinations.

Content reviewed by the NCERT Help team. Editorial Team and update policy

NCERT Solutions PDF PDF on NCERT Help. URL unchanged for search indexing.