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

Chapter-4(1) Inheritance, Defining derived class, modes of inheritance and types

The document provides an overview of inheritance in Object Oriented Programming, detailing its definition, advantages, and modes of inheritance. It explains the process of creating derived classes from base classes, emphasizing code reusability and reliability. Additionally, it outlines different types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid inheritance, along with examples and assessment questions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views28 pages

Chapter-4(1) Inheritance, Defining derived class, modes of inheritance and types

The document provides an overview of inheritance in Object Oriented Programming, detailing its definition, advantages, and modes of inheritance. It explains the process of creating derived classes from base classes, emphasizing code reusability and reliability. Additionally, it outlines different types of inheritance such as single, multiple, multilevel, hierarchical, and hybrid inheritance, along with examples and assessment questions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 28

• Defining derived class,

• modes of inheritance,
• types of inheritance,
CONTENTS • ambiguity in inheritance,
• virtual base class,
• Function overriding,
The capability of a class to derive properties and • Member Classes: Nesting of
characteristics from another class is called
Inheritance.
Classes.
Inheritance is one of the most important feature
of Object Oriented Programming

1
INHERITANCE
It is the Process of creating a New class from an existing class. The Existing class is called Base or
Parent class. The New class is called as Child or Derived Class.
Advantages
• It permits code reusability. So, save time and increase the program reliability.
• A programmer can use a class created by another person or company without modifying it derive
other classes from it that are suited to particular situations
• Improve Program Reliability
• It Permits code sharing
• It permits code reusability. So, save time and increase the program reliability.
• A programmer can use a class created by another person or company without modifying it derive
other classes from it that are suited to particular situations
• Improve Program Reliability
• It Permits code sharing
• The base class need not be changed but can be adapted to suit the requirements in different
applications.
2
INHERITANCE
It is the Process of creating a New class from an existing class. The Existing class is called Base or
Parent class. The New class is called as Child or Derived Class.

Base Derived
Methods Base class methods
and +
Properties Additional methods

3
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 same for all of the 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 in below figure:

Figure 4.1 Need of inheritance [1]

4
WHY AND WHEN TO USE
INHERITANCE?
• 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

Figure 4.2 Need of inheritance [1]

5
IMPLEMENTING INHERITANCE IN C+
+
• For creating a sub-class which is inherited from the base class we have to follow the below syntax.
• Syntax:
class subclass_name : access_mode base_class_name
{
//body of subclass
};
• Here, subclass_name is the name of the sub class, access_mode is the mode in which you want to
inherit this sub class for example: public, private etc. and base_class_name is the name of the
base class from which you want to inherit the sub class.

6
IMPLEMENTING INHERITANCE IN C+
+
• EXAMPLE

Figure 4.3 Example inheritance made on Dev C++ 7


IMPLEMENTING INHERITANCE IN C+
+
• EXAMPLE

Figure 4.4 Example inheritance made on Dev C++ 8


MODES OF INHERITANCE
• Modes of Inheritance
• Public mode: If we derive a sub class 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 derived class.
• Protected mode: If we derive a sub class from a Protected base class. Then both public member
and protected members of the base class will become protected in derived class.
• Private mode: If we derive a sub class from a Private base class. Then both public member and
protected members of the base class will become Private in derived class.

Figure 4.5 Modes of inheritance[1] 9


Types of Inheritance (Overview)

10
PRE-REQUISITES (PUBLIC, PRIVATE AND
PROTECTED INHERITANCE)

1. Public Inheritance
Consider the dummy code given below for inheritance.
class B : public A
{
}:
The line class B : public A tells the compiler that we are inheriting class A in class B in public followings :
(a) All the public members of class A becomes public members of class B.
(b) All the protected members of class A becomes protected members of class B.
(c) Private members are never inherited.

11
2. PRIVATE INHERITANCE

Consider the dummy code given below for inheritance:


class B : private A
{
}:
The line class B : private A tells the compiler that we are inheriting class A in class B in private mode. In private
mode inheritance note the following points:
(a) All the public members of class A becomes private members of the class B.
(b) All the protected members of the class A becomes private members of class B.
(c) Private members are never inherited.

12
The above dummy code can be written as too.
class B : A
{
}:

As the default inheritance mode is private mode.

13
PROTECTED INHERITANCE
Consider the dummy code given below for inheritance:
class B : protected A
{
}:
The line class B : protected A tells the compiler that we are inheriting class A in class B in protected mode. In
protected mode inheritance note the following points:
(a) All the public members of class A becomes protected members of class B.
(b) All the protected members of class A becomes protected members of class B.
(c) Private members are never inherited.

14
NOTE:
•Note that: A class A is inherited in class B in public mode, all protected members of class A becomes protected
for class B. Now if this class B is inherited to some new class C, then these protected members inherited from A
will be available from A will be available to class C, in whatever mode you inherit class B to class C.
•Initially let us assume that you inherited class A into class B in private mode, then all protected members of
class A becomes private for class B. They can be used inside the class B. But if you inherit class B into new class
C then these members won’t be available in C as private members are never inherited.

15
TYPES OF INHERITANCE

1. SINGLE INHERITANCE

3. MULTI-LEVEL INHERITANCE

2. MULTIPLE INHERITANCE

Figure 4.6 Types of inheritance[1] 16


TYPES OF INHERITANCE

5. HYBRID/VIRTUAL INHERITANCE

4. HIERARCHICAL INHERITANCE

Figure 4.7 Types of inheritance[1] 17


SINGLE INHERITANCE
CLASS

Figure 4.8 Program made on Dev-C++


18
MULTIPLE INHERITANCE
CLASS

19
Figure 4.8 Program made on Dev-C++
MULTIPLE INHERITANCE
CLASS

20
Figure 4.8 Program made on Dev-C++
MULTILEVEL INHERITANCE

Figure 4.9 Program made on Dev-C++ 21


MULTILEVEL INHERITANCE

Figure 4.10 Program made on Dev-C++ 22


MULTILEVEL INHERITANCE

Figure 4.10 Program made on Dev-C++ 23


HYBRID INHERITANCE

Figure 4.14 Program made on Dev-C++ 24


HYBRID INHERITANCE

Figure 4.15 Program made on Dev-C++ 25


HYBRID INHERITANCE

Figure 4.16 Program made on Dev-C++ 26


Assessment Questions
1. (a) Define ambiguity in inheritance.
(b) What are modes of inheritance?
(c) What is nesting of classes?
2. Can we call private constructor? If yes , then how?
3. Write a C++ program to differentiate between three modes of inheritance.
4. Write a C++ program to demonstrate hierarchical inheritance.

27
APPLICATIONS
• You use nested classes to reflect and to enforce the relationship between two classes. You should
define a class within another class when the nested class makes sense only in the context of its
enclosing class or when it relies on the enclosing class for its function. For example, a text cursor
might make sense only in the context of a text component.

28

You might also like