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
, andprivate
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(); };
- Type of Inheritance:
Multiple Inheritance - Data members directly accessible from SALEPOINT member functions:
Name, Location, Manager, Qty - Member functions directly accessible by an object of SALEPOINT:
EnterAll(), ViewAll() - 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(); };
- Base class of College:
University - Derived class of College:
Department - Data members accessible from function displaydata():
name, principal, no_of_students, vc - Type of Inheritance:
Multilevel Inheritance - 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(); };
- Type of Inheritance:
Multilevel Inheritance - Data members accessible from class Paint:
WallArea, ColorCode, Type, Advance - Member functions accessible by an object of class Bill:
Billing(), Print(), PBook(), PView(), Book(), View() - 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:
Category | Exhibit |
---|---|
Antique | Zaveri |
Modern | Johnsen |
Classic | Terenida |
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(); };
- Type of Inheritance:
Multilevel Inheritance - Data members accessible from class Painting:
WallArea, ColorCode, Type, Advance - Member functions accessible by an object of class Billing:
Bill(), BillPrint(), PBook(), PView(), Book(), View() - 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:
- How many bytes will be required by an object of class Dealer and class Accessories?
Class Estimated Memory Dealer 118 bytes Accessories 98 bytes - Which type of inheritance is illustrated?
Multilevel Inheritance
Base Class:
AC
Derived Class:Dealer
- Names of all the members accessible from objects of class Dealer:
- enter_details()
- show_details()
- Price
- enter_accessories_details()
- show_accessories_details()
- 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 Members | Type |
---|---|
distname | char[50] |
population | long |
Member Functions | Description |
---|---|
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:
Style Fee (INR) Classical 10000 Western 8000 Freestyle 11000
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:
- Type of Inheritance: Multilevel Inheritance
- Member functions accessible from objects of class Applicant:
- Enroll()
- View()
- Enter()
- Show()
- Data members accessible within class Applicant:
- RegNo
- Name
- Budget
- 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:
- Type of Inheritance: Multiple Inheritance
- Member functions accessible from
Enter()
function of class SHOP:- Enter()
- Display()
- TEnter()
- TDisplay()
- Member functions accessible from object of class SHOP:
- Enter()
- Display()
- Order of constructor execution when object of class SHOP is created:
- ITEM()
- SUPPLIER()
- 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:
- Type of Inheritance: Multilevel Inheritance
- Data members directly accessible from class
Result
objects:FinalGrade
,Comments[20]
- Member functions directly accessible from class
Result
objects:Rcalculate()
,Rdisplayt()
,Mentry()
,Mdisplay()
- Data members accessible within
Mentry()
ofMarks
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:
- Type of Inheritance: Multilevel Inheritance
- Data members accessible from class
Report
objects:Total
,Avg
,OverallGrade
,Remarks[20]
- Member functions accessible from
Report
objects:Revaluate()
,RPrint()
,Sentry()
,Sdisplay()
- 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:
- Type of Inheritance: Hierarchical Inheritance
- Data members accessible within
SHOP
member functions:Location
,Area
,Sale
- Member functions accessible from
FACTORY
objects:Enter()
,Show()
,Register()
,Accounts()
- 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:
- Type of Inheritance: Hierarchical Inheritance
- Data members accessible from
SHOWROOM
member functions:Address
,Area
,Sale
,budget
,income
- Member functions accessible from
WORKAREA
objects:Enter()
,Display()
,Get()
,Show()
- 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:
- Type of Inheritance: Multiple Inheritance
- Members accessible from
outdoor_sports
objects:
Functions:oshow()
Data Members: None - Member functions accessible from
sports
objects:registration()
,showdata()
,iEntry()
,ishow()
- 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:
- Type of Inheritance: Multiple Inheritance
- Data members directly accessible within
DELIVERY
:PQty
,TName
,Address
,Date
- Member functions accessible by
DELIVERY
object:Input()
,Show()
,In()
,Disp()
- Order of Constructor Execution:
- PACKAGE()
- TRANSPORT()
- DELIVERY()