Chapter III. Object-Oriented Programming in C++
Chapter III. Object-Oriented Programming in C++
Hà Nội, 2020 1
Chapter III. Object-oriented programming in C++
2
Chapter III. Object-oriented programming in C++
3
Introduction to the object-oriented world
❖ Basics
❖ Basic principles
❖ An object-oriented example code
4
Introduction to the object-oriented world
❖ Basics
▪ Class
• A class determines the abstract features of a object, including its
features (attributes, fields, properties) and its behaviour (what
the thing can do, methods, operations and functions).
• We can say that a class is a scheme describing the nature of
something.
• Both the integrated properties and the methods of a class are
called class members.
5
Introduction to the object-oriented world
❖ Basics
▪ Object
• An object is a scheme (an example) of a class.
▪ Instance
• Instance means an actual object created at runtime: myCar is an
instance of the class Truck.
• The set of the property values of the actual object is called
the state of that object.
6
Introduction to the object-oriented world
❖ Basics
▪ Method
• Methods are responsible for the capabilities of objects: the
methods of myCar: Brake(), Ignition(), ...
• In C++, methods are rather called member functions.
▪ Message passing
• Message passing is the process during which an object sends
data to another object or "asks" another object to execute one
of its methods.
• On the code level, message passing is realised by calling a
method in C++. 7
Introduction to the object-oriented world
❖ Basics
❖ Basic principles
❖ An object-oriented example code
8
Introduction to the object-oriented world
❖ Basic principles
▪ Encapsulation, data hiding
• Classes principally consist of features (state) and methods
(behaviour).
• There are some features and methods that we hide from other
objects. These are internal (private or protected) states and
behaviour.
• However, the others are made public.
• According to the basic principles of OOP, the state features have
to be private while most of the methods may be public.
9
Introduction to the object-oriented world
❖ Basic principles
▪ Inheritance
• Inheritance means creating specific versions of a class that inherit the
features and behaviour of their parent class (base class) and use them as if
they were of their own. The classes created in this way are called subclasses
or derived classes.
Inheritance 10
Introduction to the object-oriented world
❖ Basic principles
▪ Inheritance
• Actually, inheritance is an is-a relation: myCar is a HeavyTruck,
a HeavyTruck is a Truck. So myCar has the methods of both
HeavyTruck and Truck.
• Both derived classes have one direct parent class, namely Truck.
This inheritance method is called single inheritance.
• Multiple inheritance means that a derived class inherits the
members of more direct parent classes.
11
Introduction to the object-oriented world
❖ Basic principles
▪ Inheritance
Multiple inheritance
12
Introduction to the object-oriented world
❖ Basic principles
▪ Abstraction
• Abstraction simplifies complex reality by modelling problems
with their corresponding classes and it has its effects on the level
of inheritance appropriate for these problems.
• Abstraction can be achieved through composition.
• An interface determines how to send element or receive from
element messages and it gives information about the interaction
between the components of the class.
13
Introduction to the object-oriented world
❖ Basic principles
▪ Abstraction
14
Introduction to the object-oriented world
❖ Basic principles
▪ Polymorphism
• Polymorphism makes it possible to replace the content of some
inherited (deprecated) behaviour forms (methods) with a new
one in the derived class and to treat the new, replaced methods
as the members of the parent class.
15
Introduction to the object-oriented world
❖ Basics
❖ Basic principles
❖ An object-oriented example code
16
Introduction to the object-oriented world
17
Chapter III. Object-oriented programming in C++
18
Classes and objects
19
Classes and objects
20
Classes and objects
21
Classes and objects
22
Classes and objects
23
Classes and objects
24
Classes and objects
25
Classes and objects
26
Classes and objects
27
Classes and objects
28
Classes and objects
29
Classes and objects
30
Classes and objects
31
Classes and objects
32
Classes and objects
33
Classes and objects
34
Classes and objects
36
Classes and objects
37
Classes and objects
38
Classes and objects
39
Classes and objects
40
Classes and objects
41
Classes and objects
42
Classes and objects
43
Classes and objects
44
Classes and objects
45
Classes and objects
46
Classes and objects
47
Classes and objects
48
Classes and objects
49
Classes and objects
51
Classes and objects
52
Classes and objects
53
Classes and objects
❖ Operator overloading
• An operator function can be used if one of its parameters is a
class of type class or struct. General declaration of operator
functions:
❖ Operator overloading
• The following operators cannot be overloaded : member
selection (.), indirect member selection (.*), scope (::),
conditional (?:) and the operators sizeof and typeid since their
overloading would result in undesired side effects.
• The assignment (=), the "address of" (&) and the comma (,)
operations can be applied to objects without overloading.
• Overloading operators does not result in modifying the operator
precedence and associativity, and it is not possible to introduce
new operations.
55
Classes and objects
❖ Operator overloading
▪ Creating operator functions
Expression Operator( ♣ ) Member function External function
+-*&!~
♣a A::operator ♣ () operator ♣ (A)
++ --
a♣ ++ -- A::operator ♣ (int) operator ♣ (A, int)
+-*/%^&
a♣b | < > == != <= A::operator ♣ (B) operator ♣ (A, B)
>= << >> && || ,
= += -= *= /=
a♣b %= ^= &= |= <<= A::operator ♣ (B) -
>>= []
a(b, c...) () A::operator()(B, C...) -
a->b -> A::operator->() -
56
Classes and objects
❖ Operator overloading
▪ Creating operator functions
• The operators =, (), [] and -> can only be overloaded by non-
static member functions.
• The operators new and delete are overloaded with static
member functions.
• All other operator functions can be created as member functions
or external (in general friend) functions.
57
Classes and objects
❖ Operator overloading
▪ Creating operator functions
• Binary operands:
58
Classes and objects
❖ Operator overloading
▪ Creating operator functions
• Unary operands:
59
Classes and objects
❖ Operator overloading
▪ Creating operator functions
60
Classes and objects
❖ Operator overloading
▪ Creating operator functions
61
Classes and objects
❖ Operator overloading
▪ Creating operator functions
62
Classes and objects
❖ Operator overloading
▪ Using type conversion operator functions
63
Classes and objects
❖ Operator overloading
▪ Using type conversion operator functions
64
Classes and objects
❖ Operator overloading
▪ Extending classes with input/output operations
• To "teach" I/O data streams based on classes to handle the
objects of user-defined classes.
65
Classes and objects
❖ Operator overloading
▪ Extending classes with input/output operations
66
Classes and objects
❖ Operator overloading
▪ Extending classes with input/output operations
67
Chapter III. Object-oriented programming in C++
68
Inheritance (derivation)
69
Inheritance (derivation)
• However:
➢ already existing classes may be extended with a new class,
➢ new data members and member functions may be defined
➢ or inherited member functions may be reinterpreted (replaced) if
they become deprecated concerning their functioning
(polymorphism).
70
Inheritance (derivation)
71
Inheritance (derivation)
class ClassA {
// ...
};
72
Inheritance (derivation)
73
Inheritance (derivation)
❖ Derivation of classes
❖ Initialising base class(es)
❖ Accessing class members in case of inheritance
❖ Virtual base classes in case of multiple inheritance
❖ Inheritance and/or composition?
75
Inheritance (derivation)
❖ Derivation of classes
• A derived (descendant) class is a class that inherits its data
members and member functions from one or more already
defined class(es).
• The class from which a derived class inherits is called base class
(ancestor class).
• A derived class inherits all the members of its base class;
however, it only have access to the public and protected
members of its base class as its own.
76
Inheritance (derivation)
❖ Derivation of classes
• The place where a derivation is indicated in a program code is
the class header where the mode of derivation (public,
protected, private) is indicated before the names of base classes:
77
Inheritance (derivation)
❖ Derivation of classes
78
Inheritance (derivation)
❖ Derivation of classes
79
Inheritance (derivation)
❖ Derivation of classes
• The keywords public, protected and private used in a derivation
list restrict the access of inherited (public and protected)
members in their new classes:
Mode of inheritance Access in the base class Access in the derived class
public public
public
protected protected
public protected
protected
protected protected
public private
private
protected private
80
Inheritance (derivation)
❖ Derivation of classes
• The access of any member (the access type of which is protected
or public in the base class) can be manually set directly.
81
Inheritance (derivation)
❖ Derivation of classes
❖ Initialising base class(es)
❖ Accessing class members in case of inheritance
❖ Virtual base classes in case of multiple inheritance
❖ Inheritance and/or composition?
82
Inheritance (derivation)
83
Inheritance (derivation)
❖ Derivation of classes
❖ Initialising base class(es)
❖ Accessing class members in case of inheritance
❖ Virtual base classes in case of multiple inheritance
❖ Inheritance and/or composition?
84
Inheritance (derivation)
85
Inheritance (derivation)
87
Inheritance (derivation)
❖ Derivation of classes
❖ Initialising base class(es)
❖ Accessing class members in case of inheritance
❖ Virtual base classes in case of multiple inheritance
❖ Inheritance and/or composition?
88
Inheritance (derivation)
89
Inheritance (derivation)
91
Inheritance (derivation)
92
Inheritance (derivation)
❖ Derivation of classes
❖ Initialising base class(es)
❖ Accessing class members in case of inheritance
❖ Virtual base classes in case of multiple inheritance
❖ Inheritance and/or composition?
93
Inheritance (derivation)
94
Inheritance (derivation)
95
Inheritance (derivation)
96
Inheritance (derivation)
97
Inheritance (derivation)
98
Inheritance (derivation)
99
Inheritance (derivation)
100
Chapter III. Object-oriented programming in C++
101
Polymorphism
103
Polymorphism
104
Polymorphism
105
Polymorphism
106
Polymorphism
107
Polymorphism
108
Polymorphism
109
Polymorphism
110
Polymorphism
111
Polymorphism
112
Polymorphism
113
Polymorphism
114
Polymorphism
115
Polymorphism
116
Polymorphism
117
Polymorphism
119
Polymorphism
❖ Virtual destructors
120
Polymorphism
121
Polymorphism
122
Polymorphism
123
Polymorphism
124
Polymorphism
125
Polymorphism
126
Polymorphism
127
Polymorphism
128
Polymorphism
129
Polymorphism
130
Chapter III. Object-oriented programming in C++
131
Class templates
132
Class templates
133
Class templates
134
Class templates
135
Class templates
• The template created from that can be used also for storing
character sequences and objects.
136
Class templates
137
Class templates
138
Class templates
139
Class templates
140
Class templates
141
Class templates
142
Class templates
143
Class templates
144
Class templates
145
Class templates
146
Class templates
147
Class templates
148
Class templates
149
Class templates
150
Class templates
151
Class templates
152
Class templates
153
Class templates
154
Class templates
155
Class templates
156
Class templates
157
Class templates
158
Class templates
159
Class templates
161
Class templates
162
Class templates
163
Class templates
165