CE144
OBJECT ORIENTED PROGRAMMING
WITH C++
UNIT-8
Inheritance
N. A. Shaikh
[email protected]
Topics to be covered
Introduction Hierarchical Inheritance
Defining a derived class Hybrid Inheritance
Single Inheritance Virtual Base Class
Public & private inheritance Abstract class
Multilevel inheritance nesting of classes
Multiple inheritance constructors in derived classes
Unit 8: Inheritance Prepared By: Nishat Shaikh
2
Introduction
Reusability is an important feature of OOPC. C++
strongly supports the concept of reusability.
Reusing existing code saves time and money and
increases a program’s reliability
The mechanism of deriving a new class from an old one
is called inheritance /derivation / extending classes.
The old class is referred to as base
class/superclass/parent class. The new class is called
the derived class /subclass/child class.
Unit 8: Inheritance Prepared By: Nishat Shaikh
3
Introduction
The derived class inherits some or all of the traits from the base
class.
Unit 8: Inheritance Prepared By: Nishat Shaikh
4
Types of Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
5
Types of Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
6
Types of Inheritance
Single inheritance: process of deriving a class from only one
base class
Multilevel Inheritance: process of deriving a class from
another derived class
Multiple Inheritance: process of deriving a class from two or
more base classes.
Hierarchical Inheritance: process of deriving more than one
class from a one base class.
Hybrid Inheritance: combination of more than one type of
inheritance.
Unit 8: Inheritance Prepared By: Nishat Shaikh
7
Defining Derived Classes
A derived class can be defined by specifying its
relationship with the base class in addition to its own
details. The colon indicates that the derived-class-
name is derived from the base-class-name
The visibility mode is optional
and , if present, may be either
private or public.
The default visibility mode is
private.
Visibility mode specifies whether
the features of the base class are
derived privately or publicly.
Unit 8: Inheritance Prepared By: Nishat Shaikh
8
Defining Derived Classes
Unit 8: Inheritance Prepared By: Nishat Shaikh
9
Making Private Member Inheritable
Private member of a base class cannot be accessible in
derived class directly.
By modifying the visibility limit of the private member
to public would make it accessible to all the other
functions of the program, thus taking away the
advantage of data hiding.
Unit 8: Inheritance Prepared By: Nishat Shaikh
10
Making Private Member Inheritable
C++ provides a third visibility modifier, protected, which
is accessible by the member functions within its class
and any class immediately derived from it.
It can not be accessed by the functions outside these
two classes.
Unit 8: Inheritance Prepared By: Nishat Shaikh
11
Inheritance and Accessibility
Unit 8: Inheritance Prepared By: Nishat Shaikh
12
Making Private Member Inheritable
The keywords private, However, the normal
protected and public may practice is to use them as
appear in any order and follows:
any number of times.
NOTE: It is also possible to inherit base class in protected mode
(known as protected derivation)
Unit 8: Inheritance Prepared By: Nishat Shaikh
13
Effect of inheritance on visibility of members
Unit 8: Inheritance Prepared By: Nishat Shaikh
14
Visibility of inherited members
Note: A derived class doesn’t inherit access to private
data members. However, it does inherit a full
parent object, which contains any private
members which that class declares.
Unit 8: Inheritance Prepared By: Nishat Shaikh
15
Unit 8: Inheritance Prepared By: Nishat Shaikh
16
Unit 8: Inheritance Prepared By: Nishat Shaikh
17
Unit 8: Inheritance Prepared By: Nishat Shaikh
18
Access Control to Data Members
Functions that can have access to the private and
protected members of a class:
A function that is a friend of the class.
A member function of a class that is a friend of the
class.
A member function of a derived class.
Unit 8: Inheritance Prepared By: Nishat Shaikh
19
Access Control to Data Members
Unit 8: Inheritance Prepared By: Nishat Shaikh
20
Single inheritance: public derivation
Unit 8: Inheritance Prepared By: Nishat Shaikh
21
Single inheritance: public derivation
Unit 8: Inheritance Prepared By: Nishat Shaikh
22
Single inheritance: private derivation
Unit 8: Inheritance Prepared By: Nishat Shaikh
23
Single inheritance: private derivation
Unit 8: Inheritance Prepared By: Nishat Shaikh
24
Practical 30:Single Inheritance
Define a Base Class Vegetable having data member Color
and member function getdata() which takes color as an
input and putdata() which print the color as an output.
Vegetable Class has one subclass named Tomato having
data members weight and size and member function
gtdata() which takes weight and size as an input and
ptdata() which prints weight and size as output. Write a C++
Program which inherits the data of Vegetable class in
Tomato class using Single Inheritance.
Unit 8: Inheritance Prepared By: Nishat Shaikh
25
Practical 30:Single Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
26
Practical 30:Single Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
27
Multilevel inheritance
The class A serves as a base
class for the derived class
B, which in turn serves as a
base class for the derived
class C.
The class B is known as
intermediate base class
since it provides a link for
the inheritance between A
and C.
The chain ABC is known as
inheritance path.
NOTE: The process can be extended to any number of levels
Unit 8: Inheritance Prepared By: Nishat Shaikh
28
Multilevel inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
29
Multilevel inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
30
Practical 34: Multilevel Inheritance
Create a class shape having data member shape_name and
member function to get and print shape_name. Derive a
Class Circle which is inherited publicly from class shape and
having data members radius of a circle and member
function to get and print radius of a circle. Derive a Class
Area which is inherited publicly from Class Circle and having
data members area_of_circle and member function display
() which displays area of a circle. Use object of class Area in
main () function and get and display all the information.
Use the concepts of Multilevel Inheritance.
Unit 8: Inheritance Prepared By: Nishat Shaikh
31
Practical 34: Multilevel Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
32
Practical 34: Multilevel Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
33
Multiple Inheritance
A class can inherit the attributes of two or more classes
is known as multiple inheritance.
It allows us to combine the features of several existing
classes as a starting point for defining new classes.
It is like a child inheriting the physical features of one
parent and the intelligence of another.
Unit 8: Inheritance Prepared By: Nishat Shaikh
34
Multiple Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
35
Ambiguity Resolution in Inheritance
Ambiguity may arise in inheritance application, when
the function in derived class overrides the inherited
function.
A call to function with same name in base class and
derived class by derived class object will invoke
function defined in derived class only.
However, we may invoke the function defined in base
class by using the scope resolution operator.
Unit 8: Inheritance Prepared By: Nishat Shaikh
36
Ambiguity Resolution in Inheritance
In Single Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
37
Ambiguity Resolution in Inheritance
We may also face a problem in using the multiple
inheritance, when a function with the same name
appears in more than one base class.
We can solve this problem by defining a named
instance within derived class, using the class resolution
operator with the function.
Unit 8: Inheritance Prepared By: Nishat Shaikh
38
Ambiguity Resolution in Inheritance
In Multiple Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
39
Constructors in Derived Classes
We know that constructor is invoked implicitly when an
object is created
In inheritance, when we create object of derived class,
what will happen?
Unit 8: Inheritance Prepared By: Nishat Shaikh
40
Constructors in Derived Classes
Compiler’s Default Constructor in case of inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
41
Constructors in Derived Classes
User’s Default Constructor in case of inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
42
Constructors in Derived Classes
NOTE:
As long as no base class constructor takes any arguments, the
derived class need not have a constructor function.
However, if any base class contains a constructor with one or
more arguments, then it is mandatory for the derived class to
have a constructor, call base class’s constructor explicitly and
pass the arguments to the base class constructors.
Unit 8: Inheritance Prepared By: Nishat Shaikh
43
Constructors in Derived Classes
Unit 8: Inheritance Prepared By: Nishat Shaikh
44
Constructors in Derived Classes
NOTE: In case of multiple inheritance, the base
class are constructed in the order in which they
appear in the declaration of the derived class.
Unit 8: Inheritance Prepared By: Nishat Shaikh
45
Constructors in Derived Classes
NOTE: In case of multilevel inheritance,
the constructors will be executed in the
order of inheritance.
Unit 8: Inheritance Prepared By: Nishat Shaikh
46
Constructors in Derived Classes
The constructors for virtual base classes are invoked
before any non-virtual base classes.
If there are multiple virtual base classes, they are invoked
in the order in which they are declared. Any non-virtual
bases are then constructed before the derived class
constructor is executed.
Method of inheritance Order of execution
A(); base constructor
B(); derived constructor
B(); base(first)
C(); base(second)
A(); derived
C() ; virtual base
B(); ordinary base
A(); derived
Unit 8: Inheritance Prepared By: Nishat Shaikh
47
Constructors in Derived Classes
C++ supports another method of initializing the class
objects.
This method uses what is known as initialization list in the
constructor function.
We can use initialization-section to provide initial values
to the base constructor and also to initialize its own class
members.
We can use either/both section(assignment as well as
initialization) to initialize the data members of the
constructor.
Unit 8: Inheritance Prepared By: Nishat Shaikh
48
Constructors in Derived Classes
Unit 8: Inheritance Prepared By: Nishat Shaikh
49
Destructor in inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
50
Practical 32: Multiple Inheritance
Create a Class alpha having data member: int x and one argument
constructor which initializes the value of x. It also has member
function which displays the value of x. Create another class beta
which contains data member: float y and one argument constructor
which initializes the value of y. It also has member function which
displays the value of y. Create a Class Gamma which publicly inherits
from class alpha and class beta and has two data members: int m, n
and a constructor which passes argument to the base class
constructor as well as initializes its own data members. Class Gamma
also has member function to print the values of m and n. Write main
function which creates object of class Gamma which passes values of
base class constructor as well as derived class constructor.
Use the concept of Multiple Inheritance and Constructor in Derived
Class.
Unit 8: Inheritance Prepared By: Nishat Shaikh
51
Practical 32: Multiple Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
52
Practical 32: Multiple Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
53
Hierarchical Inheritance
Many Programming problems can
be cast into a hierarchy where
certain features of one level are
shared by many others below that
level
The base class will include all the
features that are common to the
subclasses.
A subclass can be constructed by
inheriting the properties of the
base class.
A subclass can serve as a base class
for the lower level classes and so
on
Unit 8: Inheritance Prepared By: Nishat Shaikh
54
Hierarchical Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
55
Practical 31:Hierarchical Inheritance
Write a program to create a class Medicine which stores type
of medicine, name of company, date of manufacturing. Class
Tablet is inherited from Medicine. Tablet class has name of
tablet, quantity per pack, price of one tablet as members.
Class Syrup is also inherited from Medicine and it has
quantity per bottle, dosage unit as members. Both the
classes contain necessary member functions for input and
output data. Write a main( ) that enter data for tablet and
syrup, also display the data.
Use the concepts of Hierarchical Inheritance.
Unit 8: Inheritance Prepared By: Nishat Shaikh
56
Practical 31:Hierarchical Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
57
Practical 31:Hierarchical Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
58
Practical 31:Hierarchical Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
59
Practical 31:Hierarchical Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
60
Hybrid Inheritance
There could be situations where we need to apply two
or more types of inheritance.
For instance, consider the following Fig. where result will
have both multilevel and multiple inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
61
Hybrid Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
62
Hybrid Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
63
Virtual Base Classes
Consider the situation where we have one class
Grandparent .This class is inherited by two other classes
Parent1 and Parent2. Both these class are inherited into
a new class child as shown in figure below.
As we can see from the figure
that data members/function of
class Grandparent are inherited
twice to class child. One through
class Parent1 and second through
class Parent1.
Unit 8: Inheritance Prepared By: Nishat Shaikh
64
Virtual Base Classes
When any data / function member of class Grandparent
is accessed by an object of class child, ambiguity arises
as to which data/function member would be called?
One inherited through Parent1 or the other inherited
through Parent2. This confuses compiler and it displays
error.
Unit 8: Inheritance Prepared By: Nishat Shaikh
65
Virtual Base Classes
Unit 8: Inheritance Prepared By: Nishat Shaikh
66
Virtual Base Classes
To resolve this ambiguity when class Grandparent is
inherited in both class Parent1 and class Parent2, it is
declared as virtual base class by placing a keyword virtual
as :
Note:
virtual can be written before or after the public.
Now only one copy of data/function member will be
copied and class Grandparent becomes the virtual base
class.
Unit 8: Inheritance Prepared By: Nishat Shaikh
67
Virtual Base Classes
NOTE:
When a class is made virtual base class, C++ takes
necessary care to see that only one copy of that class is
inherited, regardless of how many inheritance path exist
between the virtual base class and a derived class
Unit 8: Inheritance Prepared By: Nishat Shaikh
68
Virtual Base Classes
Unit 8: Inheritance Prepared By: Nishat Shaikh
69
Virtual Base Classes
Unit 8: Inheritance Prepared By: Nishat Shaikh
70
Practical 33: Hybrid Inheritance
Define a class Hospital having rollno and name as data
members and member function to get and print data. Derive a
class Ward from class Hospital having data members: ward
number and member function to get and print data. Derive
another class Room from Hospital having data member bed
number and nature of illness and member function to get and
print data. Derive class Patient from Class Ward and Class
Room. In main () declare 5 object of Class Patient and get and
display all the information.
Use the concept of Virtual Base Class and Hybrid Inheritance.
Unit 8: Inheritance Prepared By: Nishat Shaikh
71
Practical 33: Hybrid Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
72
Practical 33: Hybrid Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
73
Practical 33: Hybrid Inheritance
Unit 8: Inheritance Prepared By: Nishat Shaikh
74
Abstract Classes
An abstract class is one that is not used to create objects.
It is designed only to act as a base class(to be inherited by
other classes)
It is a design concept in program development and
provides a base upon which other classes may built.
Unit 8: Inheritance Prepared By: Nishat Shaikh
75
Member Classes: Nesting of Classes
Is-a relationship: Inheritance is a Is-a relationship
Ex: BMW is-a car [Here, BMW & car are classes]
Has-a relationship: Composition is a Has-a relationship
Ex: Car has-a engine [Here, Car & engine are classes]
What is Nested class?
A nested class is a class that is declared in another class
It implements the concept of “has-a” relationship.
Unit 8: Inheritance Prepared By: Nishat Shaikh
76
Member Classes: Nesting of Classes
Syntax 1 Syntax 2
Unit 8: Inheritance Prepared By: Nishat Shaikh
77
Member Classes: Nesting of Classes
Defining Nested class inside the Enclosing class
Nested class as private
Nested class as public
Defining Nested class outside the Enclosing class
Unit 8: Inheritance Prepared By: Nishat Shaikh
78
Defining Nested class inside the Enclosing class
Nested class as public
Unit 8: Inheritance Prepared By: Nishat Shaikh
79
Defining Nested class inside the Enclosing class
Nested class as public
Unit 8: Inheritance Prepared By: Nishat Shaikh
80
Defining Nested class inside the Enclosing class
Nested class as private
Unit 8: Inheritance Prepared By: Nishat Shaikh
81
Defining Nested class outside the Enclosing class
C++ supports another way of inheriting classes:
An object can be collection of many other
objects (A class can contain objects of other
classes as its members)
All objects of gamma class will
contain the objects a and b.
This kind of relationship is called
containership or nesting
Unit 8: Inheritance Prepared By: Nishat Shaikh
82
Defining Nested class outside the Enclosing class
Creation of an object that contains another object is very
different than the creation of an independent object.
An independent object is created by its constructor
when it is declared with arguments.
A nested object is created in two stages:
o The member objects are created using their
respective constructors.
o Then ordinary members are created.
Unit 8: Inheritance Prepared By: Nishat Shaikh
83
Defining Nested class outside the Enclosing class
This means Constructors of all the member objects
should be called before its own constructor body is
executed.
Unit 8: Inheritance Prepared By: Nishat Shaikh
84
Defining Nested class outside the Enclosing class
Unit 8: Inheritance Prepared By: Nishat Shaikh
85
End of Unit-8