Chapter5 Inheritance
Chapter5 Inheritance
Level: 2A / 2P
C++ team
1
Plan
Introduction
Inheritance
Redefinition
2
Introduction
3
Introduction
Objectives:
✔ To know how to put into practice simple inheritance in OOP and apply it in
C++.
✔ Introduce the access rights of a derived class to the members of the base
class.
4
Introduction
Worker
-Overtime
Department Chief
+getOvertime()
Engineer -Bonus
-ServiceBonus
-Bonus
-Speciality +getBonus()
+getServiceBonus()
+getBonus()
+getSpeciality()
6
Introduction
Introduction
Employee
-Name
-First Name
-Salary
-Overtime
+getOvertime()
Engineer
Department Chief
-Speciality
-ServiceBonus
+getSpeciality()
+getServiceBonus()
7
Inheritance
8
Inheritance
Definition
Inheritance is a feature or a process in which, new classes are created from the existing
classes.
The new class created is called “derived class” ,“child class” or “Sub class”and the existing
class is known as the “base class” ,“parent class”or “Super Class”
The derived class now is said to be inherited from the base class.
9
Inheritance
Graphical representation in UML (Unified Modeling Language)
Class A Class B
- attribut_A1: type -> pub - attribut_B1: type
- arrtibut_A2:type -> priv - arrtibut_B2:type
- arrtibut_A3:type -> pro - ……
+ method_A1(): return type + method_B1(): return type
+method_A2(): return type +method_B2(): return type
……. ……..
⮲ A class can inherit either from only one class (single inheritance) or from
more than one classes (multiple inheritance)
11
Inheritance
Single Inheritance
The derived class inherits the attributes and methods of one base class only.
Example:
A Worker is an employee with additional characteristics.
class Base_C
{ Access_specifier: -optional- to indicate the nature
//
of the inheritance: private, protected or public.
};
class Derived_C: <access_specifier> ⮲ If neither is specified, PRIVATE is taken as
Base_C
default
{
//
} 12
Inheritance
Access Control during Inheritance
Status of the members of the derived class according to the status of the members of the
base class and the derivation mode:
Base Class members Access Specifier
Employee
Worker
- name: string - overtime: int
- firstName: string
- salary: float
+ Worker()
+ Employee() +~Worker()
+~Employee() +setOvertime(int):void
+set_e():void
+display_e():void
Implement the following two classes Employee and Worker and test the main function 14
Inheritance
Use of the members of the base class in a derived class:
Example: Implementation of the following two classes Employee and
Worker with the main function
Worker.h
Worker.cpp
Employee.h
Employee.cpp 15
Inheritance
Use of the members of the base class in a derived class:
Example: main function to test the Worker class
16
Inheritance
Use of the members of the base class in a derived class:
Example: main function to test the Worker class
A first way to improve this situation is to write a new public display member function for
the Worker class
Worker.cpp
Main.cpp
Worker.cpp
18
Inheritance
Use of the members of the base class in a derived class:
2- Call the getters of the attributes inherited from the base class
19
Inheritance
Use of the members of the base class in a derived class:
2- Call the getters of the attributes inherited from the base class
Worker.cpp
20
Inheritance
Use of the members of the base class in a derived class:
3- Make the attributes of the base class protected (accessible to derived classes)
Worker.cpp
21
Inheritance and Constructor
22
Inheritance and Constructor
⮲ The constructor of the derived class must call the constructor of the base class
⮲ The name and parameters of the base class's constructor must be specified
after the prototype of the derived class's constructor
23
Inheritance and Constructor
Worker.h
Worker.cpp
Employee.cpp 24
Inheritance and Constructor
Hierarchy /Order of calls:
To create an object of type Worker:
Calling the Employee class constructor, then calling the Worker class constructor.
To destroy an object of type Worker:
automatic call of the destructor of Worker, then call the destructor of Employer.
26
Redefine in the Derived Class
Redefine of the attribute member
27
Redefine in the Derived Class
Redefine of the functions
Example of classes Employee and Worker:
28
Redefine in the Derived Class
Redefine of the functions
30
To Remember
✔ Inheritance: a technique allowing to build a class from an existing class by adding one or several other characteristics
(attributes, methods).
✔ A child class inherits the attributes and methods of its parent class.
It is recommended to declare the attributes of the parent class as "protected" otherwise you have to go through the methods to
manipulate them.
✔ If the parent class contains a constructor with parameters whose call is mandatory, the constructor of the derived class must
call that of the parent class.
✔ A child class can redefine a method of the parent class: keep the same signature and modify the implementation
of the function.
31
References
32
Application
Exercise 3: TD5
33
THANK YOU
Any Questions ?