Inheritance
Chapter 16 · Tamil Nadu State Board
- 10 min read
- 5-question quiz
Every key point below shows the sentence from your textbook it comes from. Book back answers are solved and checked against the book; answers we could not prove are left out. How we check content
In short
This lesson explains how inheritance creates new classes from existing classes and supports code reusability. It introduces single, multiple, hierarchical, multilevel and hybrid inheritance, and shows how visibility modes affect inherited members. It also explains the order of constructors and destructors and how scope resolution can identify a base class function. Understanding these ideas helps students write and examine C++ programs that use inheritance.
Written by AI from this lesson. How we check it
Key points
- Inheritance lets a new class take properties from an existing class; the existing class is the base class and the new one is the derived class.
From your textbook
“inheritance enables new class and its objects to take on the properties of the existing classes”
- Inheritance supports code reuse, represents real-world relationships and supports transitivity.
From your textbook
“It represents real world relationships well • It provides reusability of code • It supports transitivity”
- The five types described are single, multiple, hierarchical, multilevel and hybrid inheritance.
From your textbook
“Single Inheritance, Multiple inheritance, Multilevel inheritance, hybrid inheritance and hierarchical inheritance.”
- Visibility modes control inherited-member access; the modes are private, protected and public.
From your textbook
“The three visibility modes are private, protected and public.”
- Base-class private members cannot be accessed by the derived class, regardless of inheritance visibility mode.
From your textbook
“the private members of the base class are not inherited they are only visible i.e continue to exist in derived classes, and cannot be accessed”
- When a derived-class object is created, constructors run from base to derived; destructors run in reverse order.
From your textbook
“The constructors are executed in the order of inherited class i.e., from base constructor to derived. The destructors are executed in the reverse order.”
- A derived function with the same name hides the base function; the scope resolution operator can call the base function.
From your textbook
“the derived class member function shadows/hides the base class’s inherited function”
Formulas in this chapter
- Derived class formclass derived_class_name : visibility_mode base_class_name { // members of derivedclass };Explained here
- Size of a derived-class objectsize of derived class object = size of all base class data members + size of all data members in derived classExplained here
- Gross pricegross price = net price + VAT at 21%Explained here
Inheritance in C++
Definition
- Inheritance
- Inheritance lets a new class take properties and code from an existing class.
Base class and derived class
| Base class | Derived class |
|---|---|
| The existing class used as the basis for inheritance; also called a superclass or parent class. | The new class that inherits from a base class; also called a subclass or child class. |
Inheritance supports code reuse. A derived class can also add attributes and methods of its own. It can inherit all the code of the base class except members declared as private.
Advantages of inheritance
- It represents real world relationships well.
- It provides reusability of code.
- It supports transitivity.
Types of Inheritance
How inheritance types differ
| Type | Meaning |
|---|---|
| Single inheritance | One derived class inherits from one base class. |
| Multiple inheritance | One derived class inherits from multiple base classes. |
| Hierarchical inheritance | More than one derived class is created from one base class. |
| Multilevel inheritance | A class is derived from another class that is already derived. |
| Hybrid inheritance | A combination of more than one type of inheritance. |
Multilevel inheritance passes properties through levels of classes. For example, result is derived from exam, and exam is derived from student. Hybrid inheritance combines types such as multilevel and multiple inheritance.
Remember: In multilevel inheritance, a derived class can inherit properties that came from an earlier base class through another derived class.
Defining a Derived Class
A derived class definition names the class it inherits from. Write the keyword class, the derived class name, a colon, the visibility mode, and the base class name. If there is more than one base class, separate their names with commas.
class derived_class_name : visibility_mode base_class_name { // members of derivedclass };
derived_class_name is the new class, visibility_mode is private, public or protected, and base_class_name is the class being inherited from.
If no visibility mode is specified, it is private by default. In the single inheritance example, exam is declared as a public derived class of student. Its object can call public functions inherited from student, but it cannot access student members declared as private.
Worked example
In the multilevel example, how does result obtain student properties?
Try it first, then show the solutionSolution
- exam is derived from student.
- result is derived from exam.
- The inheritance chain passes student properties through exam to result.
Answer: result can use inherited, non-private members from the inheritance chain, including public functions such as acceptname().
Inheritance Visibility Modes
Visibility modes decide how inherited base-class members can be accessed through the derived class. The three modes are private, protected and public. The default visibility mode is private.
Access specifiers and visibility modes
| Access specifiers | Visibility modes |
|---|---|
| Control access to members within a class. | Control access to inherited members within a class. |
What happens to base-class members
| Inheritance mode | Protected members become | Public members become |
|---|---|---|
| private | private | private |
| protected | protected | protected |
| public | protected | public |
Private members of the base class are not inherited as accessible members in any visibility mode. They continue to exist in derived classes, but the derived class cannot access them directly.
When to use each visibility mode
- Use private inheritance when base-class features should be available to the derived class, but not to classes derived from it.
- Use protected inheritance when base-class features should be available to derived-class members, but not to the outside world.
- Use public inheritance when base-class features should be available to derived-class members and to the outside world.
Remember: Private base-class members are not directly accessible in the derived class, regardless of the inheritance visibility mode.
Public Inheritance Example
In the Shape example, width and height are protected, so Rectangle can use them in getArea(). The public functions setWidth() and setHeight() remain public because Shape is inherited publicly. The private member count cannot be accessed directly by Rectangle.
Worked example
A Rectangle object is given width 5 and height 7. What area does getArea() return?
Try it first, then show the solutionSolution
- getArea() returns width × height.
- Substitute the values: 5 × 7.
Answer: Total area: 35
Order of Constructors and Destructors
When a derived-class object is created, the base-class constructor runs first. Then constructors run in order through the derived classes. When the object is destroyed, destructors run in reverse order, starting with the most derived class.
Order in the derived1 example
- Constructors: base, derived, derived1.
- Destructors: derived1, derived, base.
size of derived class object = size of all base class data members + size of all data members in derived class
The object's size includes the data members from its base classes and its derived class.
Remember: Constructors run from base to derived; destructors run from derived to base.
Function Overriding in Inheritance
A base class and a derived class can have member functions with the same name. When called through a derived class object, the derived class function takes priority and hides the inherited base class function. This is called function overriding.
Definition
- Function overriding
- Function overriding occurs when a derived class has a member function with the same name as a member function in its base class.
Use the scope resolution operator (::) with the base class name to call the base class function. For example, Employee :: display() calls display() from Employee, even when display() is also defined in staff.
Using the base and derived class functions
- A derived class can define its own version of a base class member function.
- The derived class function hides the inherited function with the same name.
- Write the base class name, followed by :: and the function name, to call the base class function.
Remember: To call a hidden base class function, use BaseClassName :: functionName().
Inheritance and Its Types
Definition
- Inheritance
- Inheritance is the mechanism of creating a new class from an existing class.
The existing class is the base class, and the new class is the derived class. Inheritance supports code reusability. A derived class can use inherited properties and add its own attributes and methods.
Types of inheritance
| Type | How the classes are related |
|---|---|
| Single inheritance | A derived class inherits from one base class. |
| Multiple inheritance | A derived class inherits from multiple base classes. |
| Multilevel inheritance | A class is derived from a class that is itself derived from another class. |
| Hierarchical inheritance | More than one derived class is created from a single base class. |
| Hybrid inheritance | The inheritance structure combines more than one type of inheritance. |
Remember: Multilevel inheritance shows the transitive nature of inheritance.
Visibility Modes and Inherited Members
What happens to inherited members
| Derivation mode | Base class public and protected members in the derived class |
|---|---|
| Public | Public members remain public; protected members remain protected. |
| Private | Public and protected members become private. |
| Protected | Public and protected members become protected. |
A class can be derived in public, private or protected mode. Private members of a class cannot be inherited. The visibility mode affects how the base class's public and protected members are treated in the derived class.
Other inheritance facts
- In multiple inheritance, base classes are constructed in the order in which they appear in the derived class declaration.
- Base class constructors and destructors are not inherited.
- When a derived class object is created, base class constructors are automatically invoked.
- Destructors are invoked in reverse order: derived class destructors first, then base class destructors.
- The size of a derived class object is the size of all base class data members plus the size of all derived class data members.
- The this pointer refers to the current object's members.
Inheritance in C++
Class B inherits from class A using public inheritance. Class A has a protected data member, x. Class B can use x and can add its own protected data member, y.
What the classes contain
- Class A has the protected data member x and the public method show().
- Class B adds the protected data member y and has a constructor that sets x and y.
- Class B defines its own show() method to display both x and y.
In main(), objB is created with the values 30 and 20. The program then calls objB.show(), which displays x and y using the method defined in class B.
Remember: A derived class can use protected members of its base class. Here, class B uses x from class A.
Debugging a Derived Class Program
The program is intended to define class A with data members and a getdata() method, then define class B as derived from A. Class B's func() method uses the inherited data and prints values. The code contains errors in its declarations, punctuation, and function calls.
Main corrections needed
- Use valid class and access-section syntax, such as class A { public: ... }.
- Declare variables with commas between their names, not colons.
- Write getdata() and func() with parentheses for function declarations and calls.
- Use a valid derived-class declaration, such as class B : public A.
- Call the method with the correct object name and member-access operator.
Remember: Check class syntax, separators, brackets, and function-call parentheses when debugging C++ inheritance code.
Stock Class Case Study
A Stock object stores its net price. Its constructor sets that price. The get_Price() method calculates and returns the gross price, which includes VAT at 21%.
gross price = net price + VAT at 21%
The gross price includes the Stock's net price and VAT at 21%.
Remember: The get_Price() method must return the price after VAT at 21% is included.
Questions students ask
Short, direct answers to what students search for in this chapter.
What is inheritance in C++?
Inheritance in C++ lets a new class take properties and code from an existing class. The existing class is the base class, and the new class is the derived class. The derived class can also add its own attributes and methods.
What is the difference between a base class and a derived class?
A base class is the existing class used as the basis for inheritance. A derived class is the new class that inherits from it. The derived class can use inherited members that are not private and can add its own attributes and methods.
What are the types of inheritance in C++?
The types are single, multiple, hierarchical, multilevel and hybrid inheritance. Single uses one base class. Multiple uses multiple base classes. Hierarchical creates several derived classes from one base class. Multilevel forms a chain, while hybrid combines types.
How do you define a derived class in C++?
Use the keyword class, write the derived class name, a colon, the visibility mode, and the base class name. The visibility mode can be private, public or protected. If it is omitted, it is private by default.
What are visibility modes in inheritance?
Visibility modes control how inherited base-class members can be accessed through a derived class. The three modes are private, protected and public. The default visibility mode is private.
What happens to public members in private inheritance?
When a base class is inherited using private visibility, its public members become private members of the derived class. Its protected members also become private. Private base-class members are not directly accessible to the derived class.
Show 12 more questions
What happens to members in public inheritance?
In public inheritance, public members of the base class remain public in the derived class. Protected members remain protected. Private members of the base class are not directly accessible to the derived class.
What is the order of constructors and destructors in inheritance?
Constructors run from the base class to the derived class. Destructors run in the reverse order, from the most derived class back to the base class. For base, derived and derived1, construction follows that order and destruction reverses it.
How is the size of a derived class object determined?
The size of a derived class object is the size of all base-class data members plus the size of all data members in the derived class.
What is function overriding in C++ inheritance?
Function overriding occurs when a derived class defines a member function with the same name as a member function in its base class. The derived class function hides the inherited base class function. Use the base class name and the scope resolution operator (::) to call the base class version.
How do you call a hidden base class function in C++?
Use the base class name, followed by the scope resolution operator (::) and the function name. For example, Employee :: display() calls the display() function from Employee when a derived class has a function with the same name.
What are the five types of inheritance?
The five types are single inheritance, multiple inheritance, multilevel inheritance, hierarchical inheritance and hybrid inheritance. They describe different ways that base and derived classes are related, such as one derived class having multiple base classes or several derived classes sharing one base class.
What is the difference between multiple and multilevel inheritance?
In multiple inheritance, one derived class inherits from multiple base classes. In multilevel inheritance, a class is derived from another class that is already derived from a base class. Multilevel inheritance reflects the transitive nature of inheritance.
What is the order of constructor and destructor calls in inheritance?
When an object of a derived class is created, the base class constructors are automatically invoked first. Destructors run in reverse order: the derived class destructors are invoked first, followed by the base class destructors.
What does class B inherit from class A in the given C++ program?
Class B publicly inherits from class A. It can use class A's protected data member x. Class B also has its own protected data member y and defines a show() method that displays both x and y.
What does objB.show() display in the inheritance program?
objB.show() calls the show() method defined in class B. That method displays the values of x and y. The object is created with 30 and 20, so x is set to 30 and y is set to 20.
What is the purpose of the get_Price() method in the Stock class?
The get_Price() method calculates and returns the gross price of a Stock. The gross price includes VAT at 21%. The Stock object stores its net price, which is set by its constructor.
What syntax errors should be checked when debugging the derived class program?
Check that class declarations and access sections use valid syntax, variable names are separated by commas, and functions have parentheses. Also check the derived-class declaration, brackets, and the object name and member-access operator used when calling func().
Book back answers
21 textbook exercises, solved and checked against the book. Questions we could not prove are left out.
SECTION – A — Choose the correct answer
Which of the following derives a class student from the base class school
- (a) school: student
- (b) class student : public school
- (c) student : public school
- (d) class school : public student
Try it, then show the answerAnswer
(b) class student : public school
A derived-class declaration gives the derived class name, a colon, the visibility mode, and the base class name.
From the book: “class derived_class_name :visibility_mode base_class_name”
The type of inheritance that reflects the transitive nature is
- (A) Single Inheritance
- (B) Multiple Inheritance
- (C) Multilevel Inheritance
- (D) Hybrid Inheritance
Try it, then show the answerAnswer
(C) Multilevel Inheritance
The chapter identifies multilevel inheritance as the form reflecting inheritance’s transitive nature.
From the book: “The transitive nature of inheritance is reflected by this form of inheritance.”
Which visibility mode should be used when you want the features of the base class to be available to the derived class but not to the classes that are derived from the derived class?
- (A) Private
- (B) Public
- (C) Protected
- (D) All of these
Try it, then show the answerAnswer
(A) Private
Private inheritance makes base-class features available to the derived class, but not to classes derived from it.
From the book: “Private inheritance should be used when you want the features of the base class to be available to the derived class but not to the classes that are derived from the derived class.”
Inheritance is a process of creating new class from
- (A) Base class
- (B) abstract
- (C) derived class
- (D) Function
Try it, then show the answerAnswer
(A) Base class
Inheritance creates a derived class from an existing base class.
From the book: “It is a process of creating new classes called derived classes, from the existing or base classes.”
Practice
What is the main purpose of inheritance described in this lesson?
Exam questions
Practice questions with model answers. These are not from an official paper.
What is inheritance, and name one advantage it provides.2 marks
Inheritance creates a new class from an existing class by taking on its properties. One advantage is that it allows code reusability.
Distinguish between single and hierarchical inheritance.2 marks
In single inheritance, one derived class inherits from one base class. In hierarchical inheritance, more than one derived class is created from a single base class.
How do constructors and destructors run when an object of a derived class is created and later expires?2 marks
The base-class constructor runs first, followed by the derived-class constructor. When the object expires, the derived-class destructor runs first, followed by the base-class destructor.
Explain how a derived-class function can call a base-class function with the same name.2 marks
The derived-class function hides the base-class function with the same name. Use the base-class name followed by the scope resolution operator and the function name, as in BaseClass::function().
Watch out
- Do not assume that base-class private members can be accessed by the derived class; the lesson says they cannot be accessed.
- Do not reverse the constructor and destructor order: constructors run base to derived, while destructors run in reverse.
Still stuck on this lesson?
Get an AI answer grounded in your actual textbook — with the exact page reference.
Ask AI about this topic →