0% found this document useful (0 votes)
12 views14 pages

C++ Module 3

The document provides an overview of inheritance in C++, explaining its definition, types, and syntax. It details the roles of parent and child classes, access modes, and the benefits of using inheritance to avoid code duplication. Additionally, it outlines the five types of inheritance: single, multiple, multilevel, hierarchical, and hybrid inheritance.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views14 pages

C++ Module 3

The document provides an overview of inheritance in C++, explaining its definition, types, and syntax. It details the roles of parent and child classes, access modes, and the benefits of using inheritance to avoid code duplication. Additionally, it outlines the five types of inheritance: single, multiple, multilevel, hierarchical, and hybrid inheritance.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ASHWINI S OOPS with C++ 11-07-2023

MODULE- III
INHERITANCE, POINTERS

Ashwini S
Assistant Professor
ASHWINI S OOPS with C++ 11-07-2023

2 WHAT IS INHERITANCE IN C++?

• Inheritance is a method through which one class inherits the properties from its parent
class.
• Inheritance is a feature in which one new class is derived from the existing ones.
• The new class derived is termed a derived class, and the current class is termed a Parent
or base class.
ASHWINI S OOPS with C++ 11-07-2023

3 WHAT ARE CHILD AND PARENT CLASSES?

• Child class: The class that inherits the characteristics of another class is known as the
child class or derived class.
• The number of child classes that can be inherited from a single parent class is based upon
the type of inheritance.
i.e, derived class (child) - the class that inherits from another class

• Parent class: The class from which the child class inherits its properties is called the
parent class or base class.
i.e, base class (parent) - the class being inherited from
• To inherit from a class, use the : symbol
ASHWINI S OOPS with C++ 11-07-2023

4 THE SYNTAX FOR DEFINING THE CHILD CLASS AND PARENT CLASS
IN ALL TYPES OF INHERITANCE IN C++

class parent_class
{
//class definition of the parent class
};
class child_class : visibility_mode parent_class
{
//class definition of the child class
};
Syntax Description
parent_class: Name of the base class or the parent class.
child_class: Name of the derived class or the child class.
visibility_mode: Type of the visibility mode (i.e., private, protected, and public) that specifies how the data members
of the child class inherit from the parent class.
ASHWINI S OOPS with C++ 11-07-2023

5 WHY AND WHEN TO USE INHERITANCE?

• Consider a group of vehicles. You need to create classes for Bus, Car, and Truck.
• The methods fuelAmount(), capacity(), applyBrakes() will be the same for all three classes.
If we create these classes avoiding inheritance then we have to write all of these
functions in each of the three classes as shown below figure:
ASHWINI S OOPS with C++ 11-07-2023

• You can clearly see that the above process results in


duplication of the same code 3 times.
• This increases the chances of error and data
redundancy. To avoid this type of situation, inheritance is
used.
• If we create a class Vehicle and write these three
functions in it and inherit the rest of the classes from
the vehicle class, then we can simply avoid the
duplication of data and increase re-usability.
• Using inheritance, we have to write the functions only
one time instead of three times as we have inherited the
rest of the three classes from the base class (Vehicle).
ASHWINI S OOPS with C++ 11-07-2023

7 ACCESS MODES IN C++ INHERITANCE

• Public Mode: If we derive a subclass from a public base class. Then the public member of
the base class will become public in the derived class and protected members of the base
class will become protected in the derived class.
• Protected Mode: members and protected If we derive a subclass from a Protected base
class. Then both public members of the base class will become protected in the derived
class.
• Private Mode: If we derive a subclass from a Private base class. Then both public members
and protected members of the base class will become Private in the derived class.
ASHWINI S OOPS with C++ 11-07-2023

8
ASHWINI S OOPS with C++ 11-07-2023

9
ASHWINI S OOPS with C++ 11-07-2023

10 TYPES OF INHERITANCE IN C++

• There are five types of inheritance in C++ based upon how the derived class inherits its
features from the base class. These five types are as follows:
1. Single Inheritance
2. Multiple Inheritance
3. Multilevel Inheritance
4. Hierarchical Inheritance
5. Hybrid (Virtual) Inheritance
ASHWINI S OOPS with C++ 11-07-2023

11 SINGLE INHERITANCE

• In single inheritance, a class is allowed to inherit


from only one class. i.e. one subclass is inherited
by one base class only.
• All the data members of the base class are
accessed by the derived class according to the
visibility mode (i.e., private, protected, and
public) that is specified during the inheritance.
ASHWINI S OOPS with C++ 11-07-2023

12 SYNTAX

class base_class_1
{
// class definition
};
class derived_class: visibility_mode base_class_1
{
// class definition
};
ASHWINI S OOPS with C++ 11-07-2023

THE FOLLOWING EXAMPLE ILLUSTRATES SINGLE INHERITANCE IN C++:


13
ASHWINI S OOPS with C++ 11-07-2023

14

In the above example, the subclass Computer inherits the base


class electronic Device in a public mode. So, all the public and
protected member functions and data members of the class
electronic Device are directly accessible to the class Computer.
Since there is a single derived class inheriting a single base class,
this is Single Inheritance.

You might also like