CBSE Class 12 Computer Science C++ NCERT Solutions Chapter 5: Inheritance
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
| Board | CBSE |
|---|---|
| Class | Class 12 |
| Subject | Computer Science (C++) |
| Session | 2026 |
| Language | English |
| Type | NCERT Solutions |
| Chapter | Chapter 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.
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(); };
- Which type of Inheritance out of the following is illustrated in the above example? Single Level Inheritance, Multi Level Inheritance, Multiple Inheritance
- Write the names of all the data members, which are directly accessible from the member functions of class SALEPOINT.
- Write the names of all the member functions, which are directly accessible by an object of class SALEPOINT.
- What will be the order of execution of the constructors, when an object of class SALEPOINT is declared?
-
The example illustrates Multiple Inheritance. This is because the class
SALEPOINTis derived from two base classes,ITEMandTRADER, 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). -
The data members directly accessible from the member functions of class
SALEPOINTare:Name[20]andLocation[20]: These are public members of theSALEPOINTclass itself.Qty: This is a protected member of the base classITEM. SinceSALEPOINTinherits publicly fromITEM, its protected members are accessible to the member functions ofSALEPOINT.DCodeandManager[20]: These members are declared in theTRADERclass. However,SALEPOINTinherits privately fromTRADER. 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 bySALEPOINT's member functions.
Therefore, the directly accessible data members are
Name,Location, andQty. -
The member functions directly accessible by an object of class
SALEPOINTare:EnterAll()andViewAll(): These are public member functions of theSALEPOINTclass itself.Enter()andView()(fromITEM): These are public members of the base classITEM. SinceSALEPOINTinherits publicly fromITEM, these functions are accessible.Enter()andView()(fromTRADER): These are public members of the base classTRADER. However, becauseSALEPOINTinherits privately fromTRADER, these functions become private toSALEPOINTand are not directly accessible by an object ofSALEPOINT.
Thus, the directly accessible member functions are
EnterAll(),ViewAll(),Enter(), andView()(inherited fromITEM). -
When an object of class
SALEPOINTis declared, the constructors are executed in the following order:ITEM(): The constructor of the first base class listed in the inheritance declaration (public ITEM).TRADER(): The constructor of the second base class (private TRADER).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:
- Which type of inheritance is used between University and College?
- Which type of inheritance is used between College and Department?
- Write the names of all the data members of class University, which are accessible by the member functions of class Department.
- Write the names of all the data members of class College, which are accessible by the member functions of class Department.
- Write the names of all the member functions of class University, which are accessible by the member functions of class Department.
-
The inheritance used between
UniversityandCollegeis Protected Inheritance. This is because the classCollegeis defined asprotected 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. -
The inheritance used between
CollegeandDepartmentis Public Inheritance. This is indicated by the definitionclass 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. -
The data members of class
Universitythat are accessible by the member functions of classDepartmentare:name[20]: This is a public member ofUniversity. However, sinceCollegeinherits privately (effectively, as protected inheritance makes public members protected) fromUniversity,namebecomes protected inCollege. Then, sinceDepartmentinherits publicly fromCollege, this protected member remains accessible as protected withinDepartment.vc[20]: This is a protected member ofUniversity. Due to protected inheritance fromUniversitytoCollege, it remains protected inCollege. Subsequently, due to public inheritance fromCollegetoDepartment, it remains accessible as protected withinDepartment.
Therefore, both
nameandvcare accessible. -
The data members of class
Collegethat are accessible by the member functions of classDepartmentare:regno: This is a public member ofCollege. SinceDepartmentinherits publicly fromCollege,regnois directly accessible.no_of_students: This is also a public member ofCollegeand is directly accessible byDepartmentdue to public inheritance.principal[20]: This is a protected member ofCollege. Due to public inheritance, it remains protected and is accessible by the member functions ofDepartment.name[20](from University): As explained in the previous point, this member fromUniversitybecomes protected inCollegeand remains accessible as protected inDepartment.vc[20](from University): Similarly, this member fromUniversityis protected inCollegeand remains accessible as protected inDepartment.
Thus,
regno,no_of_students,principal, and the inherited membersnameandvcare accessible. -
The member functions of class
Universitythat are accessible by the member functions of classDepartmentare:estd(): This is a public member function ofUniversity. Through protected inheritance toCollege, it becomes protected inCollege. Then, through public inheritance toDepartment, it remains accessible as a protected member function withinDepartment.inputdata(): Similar toestd(), this public function ofUniversitybecomes protected inCollegeand remains accessible as protected inDepartment.outputdata(): This public function ofUniversityalso becomes protected inCollegeand remains accessible as protected inDepartment.
Therefore,
estd(),inputdata(), andoutputdata()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?
Explanation: Multiple Inheritance is the type where a derived class inherits properties from more than one base class, as illustrated in the example with SALEPOINT inheriting from both ITEM and TRADER.
Q2. In the given SALEPOINT class example, which data member is directly accessible from its member functions?
Explanation: Qty is a protected member of the base class ITEM. Since SALEPOINT inherits publicly from ITEM, protected members of ITEM are accessible to SALEPOINT's member functions.
Q3. What is the order of constructor execution when an object of SALEPOINT is declared?
Explanation: Constructors are called in the order of their base class declaration. SALEPOINT inherits from ITEM first, then TRADER. Thus, ITEM() is called, followed by TRADER(), and finally SALEPOINT().
Q4. If a class 'C' inherits from class 'B', which in turn inherits from class 'A', what type of inheritance is this?
Explanation: This scenario describes Multi-Level Inheritance, where a class derives from another derived class, forming a chain of inheritance (A -> B -> C).
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.