0% found this document useful (0 votes)
14 views

Chapter5 Inheritance

Uploaded by

nour.amorri
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Chapter5 Inheritance

Uploaded by

nour.amorri
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Chapter 5 : Inheritance

Level: 2A / 2P
C++ team

Academic year: 2022-2023

1
Plan
Introduction

Inheritance

Inheritance and Constructor

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.

✔ Handle the construction of a derived object.

4
Introduction

Application for managing the employees of a company:

An employee can be a worker, an engineer or a department manager.

Classes: Worker, Engineer, Department_Chief Department Chief


Engineer -Name
Worker -First Name
-Name -Salary
-First Name -Bonus
-Name
-Salary -ServiceBonus
-First Name
-Bonus
-Salary
-Speciality +getName()
-Overtime
+getFirstName()
+getName() +getSalary()
+getName()
+getFirstName() +getBonus()
+getFirstName()
+getSalary() +getServiceBonus()
+getSalary()
+getBonus()
+getOvertime() 5
+getSpeciality()
Introduction
The 3 classes have attributes and methods in common Employee

Generalizationa class that groups fields and methods in -Name


-First Name
common -Salary

Only one level of hierarchy +getName()


+getFirstName()
+getSalary()

Worker

-Overtime
Department Chief
+getOvertime()
Engineer -Bonus
-ServiceBonus
-Bonus
-Speciality +getBonus()
+getServiceBonus()
+getBonus()
+getSpeciality()

6
Introduction
Introduction
Employee

-Name
-First Name
-Salary

It is possible to apply the generalization +getName()


+getFirstName()
+getSalary()
again by factoring the engineer and
department head class bonus.
Manager
We can say that an engineer is a -Bonus

manager and so is the head of department


Worker +getBonus()

-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
……. ……..

Class A: base class, parent class of B


Class B: derived class, child class of A
10
Inheritance
▪ Subclasses inherit the characteristics of their super Base Class
classes.
▪ The child class inherits the attributes and methods
Generalization Specification
declared in the parent class. Derived Class
Easily create new classes from existing classes
(code reusability)

⮲ 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

Public Protected Private

Public Public Protected Inaccessible

Mode of Protected Protected Protected Inaccessible


Inheritance
Private Private Private Inaccessible
13
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

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

Call for function display_e inherited from the base class


No information on how much overtime the worker has worked.

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

Three alternatives are possible to solve this problem:


1- Call the display function of the base class.
2- Call the getters of the attributes inherited from the base class.
3- Make the attributes of the base class protected (accessible to derived classes) 17
Inheritance
Use of the members of the base class in a derived class:
1- Call the display function of the base class

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

If the base class contains a constructor whose call is obligatory

⮲ 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.

Destructors are called in the reverse order of the call of constructors 25


Redefine of the attributes and methods

26
Redefine in the Derived Class
Redefine of the attribute member

⮚ For an object Obj of type Child_C, Obj.x will refer to the


attribute member x of type float of Child_C.
⮚ To access the member x of type int (inherited from
Parent_C) we use Child_C.C_Mere ::x.
⮚ the member x defined in Child_C is added to the member
x inherited from Parent_C it does not replace it.

27
Redefine in the Derived Class
Redefine of the functions
Example of classes Employee and Worker:

In Employe: a member function named display_e().

In Worker: a member function named display_w().

Two methods do the same action (display the data of an object)


Is it possible to keep the same name?
Yes it is possible in C++ to redefine a function.

28
Redefine in the Derived Class
Redefine of the functions

A redefinition: provide a new definition of a method of an ascending class. The function


will have the same name as the method of the parent class and especially the same
signature .

Warning: You have to distinguish between a redefinition and an overloading.


An overdefinition (or overload): allows you to use several methods with the same name
within the same class, with different signatures, so that the system can make the
distinction when calling.
29
Redefine in the Derived Class
Redefine of the functions
Example: Redefine of function display() in child class Worker

 Call of the display() method of the parent class in the display()


method of the child class.

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

• C/C++ course, Christian Casteyde 2003,


https://doc.lagout.org/programmation/C/cours_cpp.pdf
• C++ course, BEN ROMDHANE Mourad

32
Application

Exercise 3: TD5

33
THANK YOU
Any Questions ?

You might also like