Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,320 --> 00:00:04,720
hi everyone and welcome my name is
2
00:00:04,719 --> 00:00:09,199
and i make programming related videos on
3
00:00:09,199 --> 00:00:12,718
and in this video i want to talk about
4
00:00:12,718 --> 00:00:18,239
object oriented programming this course
5
00:00:15,599 --> 00:00:20,399
will start on a beginner basic level
6
00:00:18,239 --> 00:00:21,919
and then as the course progresses we
7
00:00:21,920 --> 00:00:26,400
advanced concepts i will explain those
8
00:00:24,480 --> 00:00:27,920
advanced concepts and i will show you
9
00:00:27,920 --> 00:00:32,399
all of those concepts in code you can
10
00:00:30,640 --> 00:00:34,320
find the full content of this course
11
00:00:32,399 --> 00:00:35,759
in the description of this video so make
12
00:00:35,759 --> 00:00:39,119
and then if you want to see some
13
00:00:37,119 --> 00:00:41,199
different examples if you want to learn
14
00:00:39,119 --> 00:00:42,238
more c plus and you want to upgrade your
15
00:00:42,238 --> 00:00:46,479
i have a lot more videos like this one
16
00:00:44,079 --> 00:00:47,920
on my channel so i invite you to visit
17
00:00:47,920 --> 00:00:51,760
and i'm going to see you there in some
18
00:00:51,759 --> 00:00:55,920
with this video so the first thing that
19
00:00:59,359 --> 00:01:02,879
oop is programming paradigm which means
20
00:01:02,878 --> 00:01:09,759
it is a set of rules and ideas and
21
00:01:06,640 --> 00:01:10,560
concepts it is basically a standard in
22
00:01:10,560 --> 00:01:14,240
that we use to solve a specific type of
23
00:01:14,239 --> 00:01:17,599
now besides object-oriented paradigm
24
00:01:17,599 --> 00:01:20,879
programming paradigms as well you may
25
00:01:24,959 --> 00:01:29,280
and our computers help us solve those
26
00:01:29,280 --> 00:01:33,439
and if one paradigm is good to solve one
27
00:01:33,438 --> 00:01:36,559
that does not necessarily mean that it
28
00:01:36,560 --> 00:01:40,000
all other types of problems that we have
29
00:01:40,000 --> 00:01:43,519
the idea behind object-oriented paradigm
30
00:01:43,519 --> 00:01:46,879
we want to be able to represent
31
00:01:46,879 --> 00:01:51,438
objects real-life entities together with
32
00:01:51,438 --> 00:01:56,000
attributes and their behaviors we want
33
00:01:56,000 --> 00:02:00,000
those objects to our computer and to
34
00:02:00,000 --> 00:02:03,200
in our program and that is exactly what
35
00:02:03,200 --> 00:02:08,959
is used for so how does this work on a
36
00:02:06,078 --> 00:02:09,919
real life example let's use the example
37
00:02:09,919 --> 00:02:14,079
so a car as an entity in real life has
38
00:02:15,120 --> 00:02:19,680
characteristics and some of those are
39
00:02:19,680 --> 00:02:25,520
is that car a volvo a ferrari or
40
00:02:22,878 --> 00:02:26,959
a ford or some other type of car and
41
00:02:26,959 --> 00:02:31,519
a collar it has price it has max speed
42
00:02:31,519 --> 00:02:34,800
and many more characteristics i'm sure
43
00:02:34,800 --> 00:02:38,080
many more characteristics of a car than
44
00:02:38,080 --> 00:02:42,959
so those would be some of the attributes
45
00:02:42,959 --> 00:02:47,519
a car in real life and then some of the
46
00:02:47,519 --> 00:02:51,519
are for example a car can drive a car
47
00:02:51,519 --> 00:02:56,239
uh stop you can open the door of a car
48
00:02:53,840 --> 00:02:58,560
you can tank your car so you can put
49
00:02:56,239 --> 00:03:00,400
fuel in your car and then you should
50
00:03:00,400 --> 00:03:04,239
show how much fuel that car has left so
51
00:03:04,239 --> 00:03:10,480
behaviors that car has and as i said
52
00:03:08,000 --> 00:03:12,080
object oriented programming the idea of
53
00:03:12,080 --> 00:03:16,080
is to be able to represent that car from
54
00:03:16,080 --> 00:03:20,560
in your program so how do we do that how
55
00:03:20,560 --> 00:03:24,158
a real life entity like a car in my
56
00:03:24,158 --> 00:03:31,039
another entity like a game or a sport or
57
00:03:27,439 --> 00:03:33,359
student or a book or animal you know
58
00:03:31,039 --> 00:03:34,158
so the answer to this question is by
59
00:03:34,158 --> 00:03:38,639
classes now class is very important
60
00:03:38,639 --> 00:03:42,798
class is building block of object
61
00:03:42,799 --> 00:03:46,080
and um a class is basically user
62
00:03:46,080 --> 00:03:50,719
data type and in order to be able to
63
00:03:48,318 --> 00:03:51,439
understand what are user-defined data
64
00:03:51,439 --> 00:03:55,759
you first need to understand what are
65
00:03:53,519 --> 00:03:57,599
pre-defined data types what are built-in
66
00:03:57,598 --> 00:04:01,359
data types like integer and float and
67
00:04:01,360 --> 00:04:05,760
and so on and if you are not familiar
68
00:04:03,759 --> 00:04:06,798
with these i am going to refer you to my
69
00:04:06,799 --> 00:04:10,319
that i made on my channel related to
70
00:04:10,318 --> 00:04:13,359
data types so just search code beauty
71
00:04:13,360 --> 00:04:16,400
data types and you should be able to
72
00:04:16,399 --> 00:04:21,198
and then if you are familiar with these
73
00:04:19,199 --> 00:04:22,319
predefined data types i'm going to tell
74
00:04:22,319 --> 00:04:28,639
is a little bit more complex data type
75
00:04:26,079 --> 00:04:30,478
and in what way well let's say for
76
00:04:28,639 --> 00:04:31,280
example that in your program you want to
77
00:04:31,279 --> 00:04:34,959
username of your user in order to be
78
00:04:34,959 --> 00:04:38,879
you will just create a variable of type
79
00:04:38,879 --> 00:04:42,079
username and you should be able to store
80
00:04:42,079 --> 00:04:47,839
of your user in that variable but
81
00:04:45,680 --> 00:04:49,040
what happens if you want to store the
82
00:04:49,040 --> 00:04:55,040
in your program that user has many more
83
00:04:52,240 --> 00:04:55,600
characteristics than just username user
84
00:04:55,600 --> 00:05:00,240
his name as well and then gender and
85
00:05:00,240 --> 00:05:07,038
height and width and um i'm sorry wait
86
00:05:03,519 --> 00:05:09,599
he has weight and then he also has
87
00:05:07,038 --> 00:05:11,680
some other characteristics and all of
88
00:05:09,600 --> 00:05:14,400
these characteristics are going to be
89
00:05:11,680 --> 00:05:14,800
variables that have its own type and
90
00:05:14,800 --> 00:05:18,160
all of these variables together are
91
00:05:18,160 --> 00:05:21,840
which will be a class that represents
92
00:05:21,839 --> 00:05:25,519
one thing to keep in mind though is that
93
00:05:23,600 --> 00:05:27,919
you will need to store the information
94
00:05:25,519 --> 00:05:30,000
that is important for your specific
95
00:05:27,918 --> 00:05:31,519
program for example if we are talking
96
00:05:31,519 --> 00:05:35,519
then height and weight of that user is
97
00:05:35,519 --> 00:05:38,799
but if you are building a fitness
98
00:05:38,800 --> 00:05:43,120
height and weight of your user is
99
00:05:40,639 --> 00:05:44,879
extremely important so you should store
100
00:05:43,120 --> 00:05:45,519
the information that is important for
101
00:05:45,519 --> 00:05:50,000
specific program so let me show you now
102
00:05:50,000 --> 00:05:54,560
a class in c plus code and the ide that
103
00:05:54,560 --> 00:05:59,439
here is called visual studio and you can
104
00:05:57,439 --> 00:06:00,399
download community version of visual
105
00:06:00,399 --> 00:06:03,839
in case that you want to follow this
106
00:06:01,759 --> 00:06:07,520
tutorial and type at the same time
107
00:06:03,839 --> 00:06:09,599
together with me so let me show you now
108
00:06:09,600 --> 00:06:13,039
in c code the class that i want to
109
00:06:13,038 --> 00:06:16,719
is called employee so let's do that now
110
00:06:16,720 --> 00:06:20,000
employee class in this program here so
111
00:06:20,000 --> 00:06:25,918
thing is you type class keyword
112
00:06:23,759 --> 00:06:27,439
and then you give a name to your class
113
00:06:31,279 --> 00:06:35,918
and then you use these curly brackets
114
00:06:35,918 --> 00:06:41,839
curly brackets you need to put semicolon
115
00:06:39,759 --> 00:06:44,000
because if you don't if you forget this
116
00:06:41,839 --> 00:06:44,638
semicolon you will get a compile time
117
00:06:44,639 --> 00:06:49,918
and if i try to run this program now
118
00:06:48,240 --> 00:06:51,840
that is not going to be possible as you
119
00:06:53,038 --> 00:06:56,719
expected a semi column so you need to
120
00:06:56,720 --> 00:07:02,080
here okay now that we have created
121
00:06:59,839 --> 00:07:03,519
this employee class we see that this
122
00:07:03,519 --> 00:07:07,598
and what we need to put inside this
123
00:07:05,439 --> 00:07:08,319
class here so inside these curly
124
00:07:08,319 --> 00:07:11,680
because that is the body of our class we
125
00:07:11,680 --> 00:07:16,000
members of this class now the members of
126
00:07:16,000 --> 00:07:20,399
its attributes and its behaviors so what
127
00:07:20,399 --> 00:07:24,719
an employee has well an employee
128
00:07:22,720 --> 00:07:28,080
definitely has a name so let's put
129
00:07:24,720 --> 00:07:31,919
that in so i'm going to say std
130
00:07:31,918 --> 00:07:36,478
okay and then employee definitely has a
131
00:07:34,399 --> 00:07:37,359
company that he works for so let's add
132
00:07:42,839 --> 00:07:46,878
company like this okay and i want to
133
00:07:46,879 --> 00:07:50,400
uh string i'm going to include it here
134
00:07:50,399 --> 00:07:57,439
std string so that i don't need to type
135
00:07:54,800 --> 00:08:00,400
it every time that i'm using string
136
00:07:57,439 --> 00:08:01,439
okay and now i can remove this now it
137
00:08:01,439 --> 00:08:04,639
readable okay so so far we have added a
138
00:08:04,639 --> 00:08:08,478
and a company for our employee and let's
139
00:08:06,720 --> 00:08:09,280
add one more thing let's say that every
140
00:08:09,279 --> 00:08:15,598
age so inch age we are going to
141
00:08:12,319 --> 00:08:18,240
we are going to represent age as
142
00:08:15,598 --> 00:08:18,800
an integer number so as you can see at
143
00:08:18,800 --> 00:08:21,840
of each of these attributes you put
144
00:08:21,839 --> 00:08:25,598
as well okay so far our employee has
145
00:08:25,598 --> 00:08:30,478
name company and age and you can add
146
00:08:28,399 --> 00:08:31,439
many more attributes here if you want
147
00:08:31,439 --> 00:08:35,918
his title you can add his email you can
148
00:08:33,519 --> 00:08:38,240
add employment date his birth date
149
00:08:35,918 --> 00:08:39,360
uh and so many more but i'm not going to
150
00:08:39,360 --> 00:08:43,839
situation because i don't want to add a
151
00:08:41,120 --> 00:08:45,679
bunch of code that we will not be using
152
00:08:43,839 --> 00:08:48,240
because i'm going to add that code as we
153
00:08:48,240 --> 00:08:53,278
so this class here does not represent
154
00:08:50,879 --> 00:08:54,320
data this class here represents a
155
00:08:54,320 --> 00:08:59,040
so whenever you want to create an
156
00:08:56,399 --> 00:09:01,679
employee this class here will serve
157
00:08:59,039 --> 00:09:03,039
as a model for that employee so this
158
00:09:03,039 --> 00:09:06,879
hey your employee needs to have a name a
159
00:09:06,879 --> 00:09:11,200
and age so how do you create an instance
160
00:09:09,679 --> 00:09:12,639
of this class how do you create an
161
00:09:12,639 --> 00:09:17,759
let's show that now so how do you create
162
00:09:15,360 --> 00:09:18,159
a variable of type int for example well
163
00:09:21,200 --> 00:09:24,800
and then you give the name to that
164
00:09:24,799 --> 00:09:30,240
number okay and with this we have
165
00:09:27,759 --> 00:09:32,399
successfully created a variable of type
166
00:09:30,240 --> 00:09:34,000
int and it is going to be the same with
167
00:09:34,000 --> 00:09:37,759
employee so you first specify the name
168
00:09:37,759 --> 00:09:42,240
and the name is going to be employee so
169
00:09:45,278 --> 00:09:49,200
okay and then how do i want to name this
170
00:09:52,559 --> 00:09:55,919
and with this we have successfully
171
00:09:55,919 --> 00:09:59,120
of type employee this here is going to
172
00:09:59,120 --> 00:10:03,600
of this class here so let's remove this
173
00:10:01,600 --> 00:10:05,839
because we don't need it anymore
174
00:10:03,600 --> 00:10:06,959
it was just for demonstration purposes
175
00:10:06,958 --> 00:10:11,278
how do you access now these attributes
176
00:10:11,278 --> 00:10:14,958
of employee class well the approach
177
00:10:14,958 --> 00:10:22,078
you type the name of that object
178
00:10:18,240 --> 00:10:24,320
like this and then if i put dot here
179
00:10:22,078 --> 00:10:26,319
we should be able to see all of these
180
00:10:24,320 --> 00:10:26,720
members here but that is not happening
181
00:10:26,720 --> 00:10:31,040
why is that the case well there is one
182
00:10:31,039 --> 00:10:37,679
everything inside class in c plus plus
183
00:10:34,240 --> 00:10:40,320
is private by default which means that
184
00:10:37,679 --> 00:10:42,239
all of these members here are private
185
00:10:40,320 --> 00:10:44,879
and in order to be able to understand
186
00:10:42,240 --> 00:10:46,000
this and fix the problem that we have i
187
00:10:46,000 --> 00:10:52,399
story about access modifiers so in c
188
00:10:49,360 --> 00:10:53,200
plus plus we have three access modifiers
189
00:10:53,200 --> 00:10:56,720
private we have public and then we have
190
00:10:56,720 --> 00:11:00,480
and let's explain now what each of these
191
00:11:00,480 --> 00:11:04,399
private means that whatever is private
192
00:11:04,399 --> 00:11:08,480
that is not going to be accessible
193
00:11:06,559 --> 00:11:09,359
outside of your class that is basically
194
00:11:09,360 --> 00:11:14,159
hidden and that public means that
195
00:11:12,000 --> 00:11:16,480
whatever is public in your class
196
00:11:14,159 --> 00:11:18,559
you will be able to access that outside
197
00:11:16,480 --> 00:11:20,079
of your class as well so anyone else
198
00:11:18,559 --> 00:11:21,359
outside of your class is going to be
199
00:11:21,360 --> 00:11:24,720
public members of your class and then
200
00:11:24,720 --> 00:11:28,160
is somewhere in between private and
201
00:11:28,159 --> 00:11:31,439
certain rules to it so we will be
202
00:11:31,440 --> 00:11:35,120
protected access modifier when we start
203
00:11:35,120 --> 00:11:39,919
later in this course so for now you need
204
00:11:37,919 --> 00:11:41,759
to remember that there are three of them
205
00:11:39,919 --> 00:11:44,078
there is private there is public and
206
00:11:41,759 --> 00:11:45,200
protected so in this particular
207
00:11:48,399 --> 00:11:55,360
inside your class is private by default
208
00:11:51,759 --> 00:11:59,039
so this situation here is the same
209
00:11:59,039 --> 00:12:06,399
okay this is just explicit way to say
210
00:12:03,120 --> 00:12:08,839
hey everything beneath this private
211
00:12:06,399 --> 00:12:12,078
access modifier is going to be
212
00:12:08,839 --> 00:12:15,279
private so this situation is the same
213
00:12:12,078 --> 00:12:17,120
as this situation now let's return our
214
00:12:15,278 --> 00:12:19,200
private access modifier and let's see
215
00:12:17,120 --> 00:12:21,200
how we can fix the problem that we have
216
00:12:19,200 --> 00:12:23,200
and the problem is that we cannot access
217
00:12:21,200 --> 00:12:24,959
these members of our employee class and
218
00:12:24,958 --> 00:12:28,239
let's try to change this private access
219
00:12:28,240 --> 00:12:32,560
access modifier let's try protected for
220
00:12:36,240 --> 00:12:40,000
these members name company and age now
221
00:12:40,000 --> 00:12:44,078
as you can see nothing is happening so i
222
00:12:42,000 --> 00:12:46,720
cannot access these name company names
223
00:12:44,078 --> 00:12:50,000
if i try to type for example name
224
00:12:46,720 --> 00:12:52,720
i get an error which says member
225
00:12:52,720 --> 00:12:56,879
protected is not helping as well
226
00:12:54,799 --> 00:12:58,559
so let's try the third one let's try
227
00:12:58,559 --> 00:13:03,919
okay and now as you can see the error
228
00:13:03,919 --> 00:13:07,278
to do this once more we have all of
229
00:13:07,278 --> 00:13:11,039
members name company name and age we
230
00:13:11,039 --> 00:13:14,559
here they are offered to us so that
231
00:13:14,559 --> 00:13:18,958
by changing this access modifier to
232
00:13:18,958 --> 00:13:22,319
able to see all of these properties here
233
00:13:22,320 --> 00:13:25,760
let's now set the values of these
234
00:13:25,759 --> 00:13:30,799
let's say that the name of our employee
235
00:13:34,240 --> 00:13:42,000
and then let's copy this two more times
236
00:13:38,159 --> 00:13:45,439
like this so the company will be
237
00:13:42,000 --> 00:13:48,000
let's say for example youtube and then
238
00:13:45,440 --> 00:13:49,839
code beauty which is my channel let's
239
00:13:48,000 --> 00:13:50,958
say that that is the company that i work
240
00:13:54,639 --> 00:13:59,919
okay age is an integer variable so we
241
00:13:58,000 --> 00:14:03,360
are going to set it as an integer
242
00:14:03,360 --> 00:14:06,879
so with this here we have created an
243
00:14:06,879 --> 00:14:10,480
of type of type employee that object is
244
00:14:10,480 --> 00:14:15,920
employee 1 and here we have set
245
00:14:13,600 --> 00:14:17,360
values for the properties we have set
246
00:14:17,360 --> 00:14:20,959
company and age of this employee object
247
00:14:20,958 --> 00:14:24,719
but we also said that we can describe
248
00:14:24,720 --> 00:14:28,240
so how can we describe a behavior of an
249
00:14:28,240 --> 00:14:31,278
let's first think of a behavior that an
250
00:14:31,278 --> 00:14:34,480
let's say for example that he can
251
00:14:34,480 --> 00:14:37,519
so he comes to work and he says hello my
252
00:14:37,519 --> 00:14:40,639
so and so i work for this company and i
253
00:14:40,639 --> 00:14:47,919
25 years old so how can we describe
254
00:14:44,240 --> 00:14:50,560
that behavior in this class here well
255
00:14:47,919 --> 00:14:52,319
we can describe that with a class method
256
00:14:52,320 --> 00:14:56,720
it is basically a function so we are
257
00:14:56,720 --> 00:15:00,320
inside this class employee so let's do
258
00:15:00,320 --> 00:15:03,440
i'm going to create a function of return
259
00:15:03,440 --> 00:15:12,480
void and i'm going to call that function
260
00:15:12,480 --> 00:15:16,720
like this okay and then inside this
261
00:15:16,720 --> 00:15:21,278
what i want to do is you can write hello
262
00:15:19,278 --> 00:15:22,399
my name is and then you put this name
263
00:15:22,399 --> 00:15:26,399
this company and i am 20 years old for
264
00:15:26,399 --> 00:15:29,679
but i'm going to make it a little bit
265
00:15:27,919 --> 00:15:31,759
more formal so i'm going to say the
266
00:15:31,759 --> 00:15:36,240
c out and then i'm going to write out
267
00:15:36,240 --> 00:15:43,198
like this okay and then let's put
268
00:15:43,198 --> 00:15:46,559
i'm going to add endline and then i'm
269
00:15:46,559 --> 00:15:52,958
these two informations as well so
270
00:15:52,958 --> 00:15:58,078
paste here company and then age as well
271
00:15:58,399 --> 00:16:03,759
okay now how can i invoke this
272
00:16:01,519 --> 00:16:05,519
introduce yourself function well the
273
00:16:05,519 --> 00:16:09,519
i'm going to say employee and then i put
274
00:16:09,519 --> 00:16:12,639
and as you can see this introduce
275
00:16:12,639 --> 00:16:17,600
is available here so let's invoke that
276
00:16:17,600 --> 00:16:22,079
like this and now we should be able to
277
00:16:26,480 --> 00:16:31,440
as you can see here it says name salina
278
00:16:29,198 --> 00:16:35,838
company youtube code beauty and then age
279
00:16:31,440 --> 00:16:38,000
25 okay great and now if we needed to
280
00:16:35,839 --> 00:16:38,959
introduce our employee five times for
281
00:16:38,958 --> 00:16:44,078
we just copy this function five times
282
00:16:42,000 --> 00:16:46,078
like this and our user is going to
283
00:16:46,078 --> 00:16:49,519
and if we didn't have this class method
284
00:16:49,519 --> 00:16:53,679
we would have to copy this code five
285
00:16:52,240 --> 00:16:54,240
times so each time that we want to
286
00:16:54,240 --> 00:16:58,240
our user we would have to copy this code
287
00:16:58,240 --> 00:17:03,600
instead of doing that we can create a
288
00:17:00,320 --> 00:17:05,759
class method which represents a behavior
289
00:17:03,600 --> 00:17:07,838
and then we can invoke that class method
290
00:17:05,759 --> 00:17:11,279
whenever we need it and if i run
291
00:17:07,838 --> 00:17:12,798
this program this user should introduce
292
00:17:16,000 --> 00:17:22,798
and then oh sorry four and then five
293
00:17:22,798 --> 00:17:26,240
this because i don't need it anymore
294
00:17:24,720 --> 00:17:27,038
because i want to show you something
295
00:17:27,038 --> 00:17:31,519
so let's say that we want to create
296
00:17:29,519 --> 00:17:32,000
another employee in our program so how
297
00:17:32,000 --> 00:17:35,038
that well we would use the same approach
298
00:17:35,038 --> 00:17:38,798
so employee the name of my class and
299
00:17:38,798 --> 00:17:42,240
employee let's call it employee 2 which
300
00:17:45,440 --> 00:17:50,160
the values for these attributes for our
301
00:17:47,519 --> 00:17:53,839
second employee as well i'm going to say
302
00:17:53,839 --> 00:17:58,720
and his name is going to be john for
303
00:17:58,720 --> 00:18:05,360
like this let's copy this two more times
304
00:18:05,359 --> 00:18:12,879
amazon for example john works for amazon
305
00:18:16,079 --> 00:18:20,079
so now if i want to introduce john i'm
306
00:18:20,079 --> 00:18:28,399
employee 2 dot introduce yourself
307
00:18:24,798 --> 00:18:31,918
excellent so if i run this program now
308
00:18:28,400 --> 00:18:33,679
as you can see here is me salina youtube
309
00:18:31,919 --> 00:18:37,038
code beauty and then here is john
310
00:18:37,038 --> 00:18:40,480
now one problem that i already see here
311
00:18:40,480 --> 00:18:43,679
this part of the code and then this part
312
00:18:43,679 --> 00:18:47,200
what would happen if we wanted to create
313
00:18:47,200 --> 00:18:53,200
100 more users we would have to repeat
314
00:18:50,558 --> 00:18:54,960
this for every single user that we
315
00:18:54,960 --> 00:18:59,120
optimal because there is a better way
316
00:19:01,359 --> 00:19:05,519
and in order for you to be able to
317
00:19:03,119 --> 00:19:06,798
understand this i will have to tell you
318
00:19:06,798 --> 00:19:12,400
constructors so now you may wonder
319
00:19:10,000 --> 00:19:13,279
salvino what is a constructor well a
320
00:19:13,279 --> 00:19:16,798
is a special type of method that is
321
00:19:16,798 --> 00:19:23,519
each time that an object of a class
322
00:19:20,480 --> 00:19:24,000
is created so whenever you create an
323
00:19:24,000 --> 00:19:30,558
of a class a constructor is invoked
324
00:19:27,359 --> 00:19:31,119
so does that mean that here and here as
325
00:19:31,119 --> 00:19:34,239
a constructor is invoked the answer to
326
00:19:34,240 --> 00:19:38,880
yes now you may wonder okay we have not
327
00:19:36,960 --> 00:19:39,759
created any constructor you must be
328
00:19:39,759 --> 00:19:44,480
well let me demonstrate you something
329
00:19:44,480 --> 00:19:47,919
and now let's comment this code here as
330
00:19:47,919 --> 00:19:51,360
let's see what will happen if i run my
331
00:19:51,359 --> 00:19:54,639
so we are not assigning any values to
332
00:19:54,640 --> 00:19:58,080
attributes of this employee nor this
333
00:19:58,079 --> 00:20:02,639
if i run my program okay here is what
334
00:20:02,640 --> 00:20:06,960
as you can see this is our first user
335
00:20:04,558 --> 00:20:09,200
and this here is our second user
336
00:20:06,960 --> 00:20:11,200
so here we don't have anything and then
337
00:20:09,200 --> 00:20:13,360
here we here we also don't have anything
338
00:20:11,200 --> 00:20:16,159
and then here is a number which
339
00:20:13,359 --> 00:20:17,199
i am not going to try to read okay but
340
00:20:17,200 --> 00:20:20,960
this here is the work of default
341
00:20:20,960 --> 00:20:24,798
what is default constructor default
342
00:20:24,798 --> 00:20:30,079
to describe a constructor that is
343
00:20:27,440 --> 00:20:30,558
automatically generated by your compiler
344
00:20:30,558 --> 00:20:34,079
in case that you don't create a
345
00:20:34,079 --> 00:20:37,119
your compiler is going to automatically
346
00:20:37,119 --> 00:20:40,319
constructor which is called default
347
00:20:43,519 --> 00:20:46,558
let's see how we can create our own
348
00:20:45,119 --> 00:20:47,439
constructor because those values that
349
00:20:47,440 --> 00:20:51,038
that is something that we cannot work
350
00:20:49,119 --> 00:20:52,879
with i mean we don't want to work with
351
00:20:51,038 --> 00:20:53,599
those values we want to use our own
352
00:20:53,599 --> 00:20:58,240
we want to use this and this and then
353
00:20:56,319 --> 00:20:59,119
this here and for our second user we
354
00:20:59,119 --> 00:21:04,079
these values here so how can you create
355
00:21:04,079 --> 00:21:07,279
there are a few rules when it comes to
356
00:21:07,279 --> 00:21:10,798
there are three rules actually so the
357
00:21:10,798 --> 00:21:13,918
following we already said that a
358
00:21:13,919 --> 00:21:17,520
is just a method but unlike other
359
00:21:17,519 --> 00:21:20,798
a constructor does not have a return
360
00:21:24,079 --> 00:21:27,359
as you can see this method here
361
00:21:25,599 --> 00:21:28,558
introduce yourself this method has a
362
00:21:28,558 --> 00:21:32,240
void which is nothing but we still have
363
00:21:32,240 --> 00:21:36,000
but a constructor will not have this at
364
00:21:36,000 --> 00:21:40,400
so that's going to be the first rule the
365
00:21:38,159 --> 00:21:41,520
second rule is that a constructor has
366
00:21:41,519 --> 00:21:45,759
as the class that it belongs to so the
367
00:21:45,759 --> 00:21:49,599
employee will be called employee so that
368
00:21:49,599 --> 00:21:54,000
the second rule the third rule is that
369
00:21:54,000 --> 00:21:58,798
public now this i'm seeing as a rule at
370
00:21:57,119 --> 00:22:00,798
this level of knowledge because
371
00:21:58,798 --> 00:22:02,798
a constructor does not necessarily need
372
00:22:00,798 --> 00:22:05,038
to be public always there are
373
00:22:02,798 --> 00:22:06,400
certain situations specific situations
374
00:22:05,038 --> 00:22:07,359
when you would want to make your
375
00:22:07,359 --> 00:22:11,359
private but at this level of knowledge i
376
00:22:09,679 --> 00:22:12,159
would advise you to make sure that your
377
00:22:12,159 --> 00:22:16,640
are public because if you remember when
378
00:22:14,798 --> 00:22:18,798
we talked about access modifiers and
379
00:22:16,640 --> 00:22:20,320
when we said what private means
380
00:22:18,798 --> 00:22:22,319
that means that everything that is
381
00:22:22,319 --> 00:22:26,000
inside your class and you most
382
00:22:24,480 --> 00:22:29,360
definitely don't want to make your
383
00:22:26,000 --> 00:22:31,919
constructor hidden so uh third
384
00:22:29,359 --> 00:22:33,359
three rules uh the first one is that
385
00:22:33,359 --> 00:22:37,678
return type the second one is that
386
00:22:35,839 --> 00:22:41,038
constructor has the same name
387
00:22:37,679 --> 00:22:42,400
as the class so let's create constructor
388
00:22:42,400 --> 00:22:46,559
it will be called employee and i am
389
00:22:46,558 --> 00:22:52,558
that constructor in this public area
390
00:22:50,240 --> 00:22:54,480
because if i decided for some reason to
391
00:22:54,480 --> 00:22:58,720
if i decided to make this private let's
392
00:22:58,720 --> 00:23:02,079
let's make this private like this as you
393
00:23:02,079 --> 00:23:06,319
immediately here we get an error so our
394
00:23:06,319 --> 00:23:12,798
employees too this error says
395
00:23:09,359 --> 00:23:15,759
employee constructor is inaccessible
396
00:23:12,798 --> 00:23:18,240
and here as well because we have made
397
00:23:15,759 --> 00:23:20,720
this constructor private and we cannot
398
00:23:18,240 --> 00:23:22,319
access it outside of this class here and
399
00:23:22,319 --> 00:23:27,038
we want our constructor to be public so
400
00:23:24,798 --> 00:23:29,918
i'm going to remove this private access
401
00:23:27,038 --> 00:23:30,640
modifier and as you can see this error
402
00:23:30,640 --> 00:23:35,200
disappeared now so the job of this
403
00:23:33,599 --> 00:23:36,079
constructor here is going to be to
404
00:23:36,079 --> 00:23:40,079
the object of employee that means that
405
00:23:40,079 --> 00:23:43,678
employee we want to pass these three
406
00:23:43,679 --> 00:23:48,080
name company and age we want to pass
407
00:23:48,079 --> 00:23:51,119
here so we are going to receive that in
408
00:23:51,119 --> 00:23:54,798
employee constructor as parameters so
409
00:24:06,480 --> 00:24:11,440
okay now that we have received these
410
00:24:09,359 --> 00:24:13,759
three parameters here what i want to do
411
00:24:11,440 --> 00:24:15,679
with them is i want to assign them
412
00:24:13,759 --> 00:24:16,879
to these three properties here so i'm
413
00:24:16,880 --> 00:24:24,000
name of my employee will be this
414
00:24:24,000 --> 00:24:27,759
company that our employee works in it
415
00:24:34,880 --> 00:24:38,880
will be whatever we passed as this
416
00:24:38,880 --> 00:24:44,480
here okay and now if you look at our
417
00:24:42,159 --> 00:24:46,159
employee here we have an error again
418
00:24:44,480 --> 00:24:47,839
so let's check what their what that
419
00:24:47,839 --> 00:24:54,399
no default constructor exists for class
420
00:24:51,038 --> 00:24:57,038
employee so what this error here means
421
00:24:54,400 --> 00:24:57,840
it means that when we decided to create
422
00:24:57,839 --> 00:25:03,278
constructor at that moment we lost
423
00:25:01,278 --> 00:25:04,880
the default constructor that was
424
00:25:04,880 --> 00:25:08,960
for us so when you decide to create your
425
00:25:08,960 --> 00:25:13,120
you are going to lose that default
426
00:25:11,278 --> 00:25:13,679
constructor and you can fix that by
427
00:25:13,679 --> 00:25:16,960
making your own default constructor or
428
00:25:16,960 --> 00:25:22,159
providing here these three values that
429
00:25:20,079 --> 00:25:23,519
we have specified that our constructor
430
00:25:23,519 --> 00:25:29,440
so let's do that let's provide here name
431
00:25:26,960 --> 00:25:30,558
company name and age so how do we do
432
00:25:30,558 --> 00:25:34,079
let me show you how we can use this
433
00:25:34,079 --> 00:25:39,759
so in this line i'm going to say
434
00:25:37,200 --> 00:25:40,720
that my employee 1 is going to be equal
435
00:25:40,720 --> 00:25:46,159
and i'm going to invoke this constructor
436
00:25:46,159 --> 00:25:52,960
hey i am invoking this constructor here
437
00:25:49,440 --> 00:25:54,798
and here it expects to receive argument
438
00:25:54,798 --> 00:25:59,839
it is these three parameters so name
439
00:25:57,839 --> 00:26:00,558
company and age so here i'm going to
440
00:26:04,319 --> 00:26:09,119
and then company is going to be this
441
00:26:07,119 --> 00:26:10,798
company here so youtube code beauty i'm
442
00:26:10,798 --> 00:26:17,759
and then age is going to be 25
443
00:26:14,558 --> 00:26:20,000
okay so with this we have successfully
444
00:26:20,000 --> 00:26:24,240
object which means that i can delete
445
00:26:24,240 --> 00:26:29,679
and then if we do this same for our
446
00:26:27,119 --> 00:26:32,158
employee tool we will be able to delete
447
00:26:29,679 --> 00:26:33,919
this code here as well so let's do that
448
00:26:33,919 --> 00:26:43,038
employee and then here i want to pass
449
00:26:37,679 --> 00:26:48,480
these three values so name is john
450
00:26:43,038 --> 00:26:48,480
okay and then company is amazon
451
00:26:52,558 --> 00:26:58,879
excellent so i am able to delete
452
00:26:58,880 --> 00:27:03,520
check what is going to happen if i run
453
00:27:01,519 --> 00:27:06,400
my program i'm just going to format
454
00:27:03,519 --> 00:27:07,119
this so if i run my program nothing
455
00:27:07,119 --> 00:27:11,918
i should still be able to introduce this
456
00:27:09,679 --> 00:27:15,440
user and then this user as well so let's
457
00:27:11,919 --> 00:27:15,440
do that let's run our program
458
00:27:15,919 --> 00:27:20,000
okay and as you can see selena youtube
459
00:27:23,519 --> 00:27:27,759
so now we have managed to reduce the
460
00:27:25,519 --> 00:27:31,038
code that we had in our main function
461
00:27:27,759 --> 00:27:33,278
to only two lines per employee so
462
00:27:31,038 --> 00:27:34,079
the first line is to construct that
463
00:27:34,079 --> 00:27:38,798
second line is to introduce that user
464
00:27:37,038 --> 00:27:40,960
and we have managed to do that
465
00:27:38,798 --> 00:27:43,440
with our employee constructor which is
466
00:27:40,960 --> 00:27:45,600
now doing that work of constructing the
467
00:27:43,440 --> 00:27:46,840
object based on based on the parameters
468
00:27:46,839 --> 00:27:50,798
constructor that constructor receives
469
00:27:48,960 --> 00:27:52,640
those parameters here and then
470
00:27:50,798 --> 00:27:54,960
it constructs the object here it
471
00:27:52,640 --> 00:27:57,360
initializes the values of that
472
00:27:54,960 --> 00:27:59,038
object and then in our introduce
473
00:27:59,038 --> 00:28:02,319
invoke that function for an object of an
474
00:28:02,319 --> 00:28:06,480
that function is going to do this code
475
00:28:04,960 --> 00:28:07,519
here so it is going to basically
476
00:28:07,519 --> 00:28:12,639
employee so i hope that this part so far
477
00:28:10,880 --> 00:28:15,440
was understandable for you because
478
00:28:12,640 --> 00:28:17,200
here i'm going to take a quick break and
479
00:28:15,440 --> 00:28:18,720
i'm going to pause the video and then i
480
00:28:17,200 --> 00:28:20,720
might be back in a couple of minutes or
481
00:28:18,720 --> 00:28:22,558
in a couple of hours or even in a couple
482
00:28:20,720 --> 00:28:25,360
of days but that is going to be
483
00:28:22,558 --> 00:28:27,359
just a couple of seconds for you and i
484
00:28:25,359 --> 00:28:27,678
want to make a transition that i never
485
00:28:27,679 --> 00:28:31,038
on my channel i never did this on code
486
00:28:29,359 --> 00:28:34,959
beauty but i'm going to do it here
487
00:28:31,038 --> 00:28:34,960
and that is this transition here
488
00:28:35,839 --> 00:28:39,839
hello and welcome back and let's
489
00:28:39,839 --> 00:28:44,000
object oriented programming but before i
490
00:28:42,398 --> 00:28:45,439
continue i want to make a quick
491
00:28:44,000 --> 00:28:47,440
summary of the things that we have
492
00:28:47,440 --> 00:28:51,840
so first i have explained what is
493
00:28:50,000 --> 00:28:53,200
object-oriented programming and what is
494
00:28:53,200 --> 00:28:58,000
object-oriented paradigm and then i have
495
00:28:56,079 --> 00:28:59,839
explained what our classes and what our
496
00:28:58,000 --> 00:29:01,519
objects and how you can use those
497
00:29:01,519 --> 00:29:07,440
in order to represent real life entities
498
00:29:05,119 --> 00:29:08,319
together with their attributes and their
499
00:29:08,319 --> 00:29:11,439
in your programs describe them to your
500
00:29:11,440 --> 00:29:14,798
and then we also talked about access
501
00:29:14,798 --> 00:29:20,480
private public and protected and then we
502
00:29:18,240 --> 00:29:21,839
have seen what our constructors and how
503
00:29:21,839 --> 00:29:26,720
and use constructors and now the time
504
00:29:26,720 --> 00:29:31,679
four most important principles four
505
00:29:29,599 --> 00:29:33,519
pillars for most important concepts of
506
00:29:33,519 --> 00:29:39,839
and those are encapsulation abstraction
507
00:29:36,558 --> 00:29:41,440
inheritance and polymorphism so the
508
00:29:39,839 --> 00:29:43,918
first one that i want to talk about
509
00:29:45,119 --> 00:29:51,199
the idea of encapsulation is idea of
510
00:29:48,480 --> 00:29:52,880
bundling or tying together data and
511
00:29:52,880 --> 00:29:56,960
data so that they are grouped together
512
00:29:56,960 --> 00:30:01,440
and why do we do this well we do this
513
00:29:59,359 --> 00:30:04,479
with the purpose of preventing
514
00:30:01,440 --> 00:30:07,440
anyone or anything outside of our class
515
00:30:04,480 --> 00:30:10,159
to be able to directly access our data
516
00:30:07,440 --> 00:30:12,960
and to interact with it and to modify it
517
00:30:10,159 --> 00:30:13,679
so i am not saying that we don't want
518
00:30:13,679 --> 00:30:17,759
to access our data at all i'm just
519
00:30:15,679 --> 00:30:19,759
saying that i don't want that to happen
520
00:30:17,759 --> 00:30:21,599
directly i don't want that other class
521
00:30:21,599 --> 00:30:24,879
modify and change and interact with my
522
00:30:24,880 --> 00:30:28,000
because i want to provide my own way for
523
00:30:28,000 --> 00:30:31,119
to happen so how do i do that how do i
524
00:30:31,119 --> 00:30:35,599
a way for other classes to interact with
525
00:30:35,599 --> 00:30:39,359
well i provide very specific public
526
00:30:39,359 --> 00:30:44,000
that that other class can invoke and in
527
00:30:41,919 --> 00:30:47,600
that way interact with my data
528
00:30:44,000 --> 00:30:50,480
so again how do you access encapsulated
529
00:30:47,599 --> 00:30:50,879
properties of a class the answer is
530
00:30:50,880 --> 00:30:57,760
its methods and these methods are very
531
00:30:54,880 --> 00:30:58,320
often implemented as getters and setters
532
00:30:58,319 --> 00:31:02,079
now i'm going to show you in code how
533
00:31:02,079 --> 00:31:05,918
look like and how we can modify this
534
00:31:05,919 --> 00:31:10,240
so that we obey that rule of
535
00:31:10,240 --> 00:31:14,079
so the first thing that i want to do
536
00:31:14,079 --> 00:31:18,000
i want to make these three properties i
537
00:31:18,000 --> 00:31:21,440
private so i want to encapsulate these
538
00:31:21,440 --> 00:31:25,278
hide them within this class so let's do
539
00:31:25,278 --> 00:31:33,679
here i'm going to put private modifier
540
00:31:30,398 --> 00:31:35,839
so this here is private access modifier
541
00:31:33,679 --> 00:31:36,399
and we don't actually have to write this
542
00:31:36,398 --> 00:31:39,518
everything that does not have a modifier
543
00:31:39,519 --> 00:31:42,640
c plus plus is private by default within
544
00:31:42,640 --> 00:31:46,080
but i just find it more readable if i do
545
00:31:46,079 --> 00:31:49,918
here in this private area i am going to
546
00:31:49,919 --> 00:31:57,840
properties okay and then paste them here
547
00:31:54,798 --> 00:31:58,960
like this and now we have hidden these
548
00:31:58,960 --> 00:32:03,840
within this class called employee which
549
00:32:01,599 --> 00:32:06,240
means that now we will not be able
550
00:32:03,839 --> 00:32:07,918
to access these three properties anymore
551
00:32:07,919 --> 00:32:11,200
let me just collapse this and let me
552
00:32:14,240 --> 00:32:18,079
name or company or age on my employee
553
00:32:18,079 --> 00:32:21,439
i should not be able to do that now so
554
00:32:24,640 --> 00:32:29,120
and then i type this dot you can see
555
00:32:27,038 --> 00:32:30,879
that the only thing that we can access
556
00:32:29,119 --> 00:32:31,759
is this introduce your introduce
557
00:32:31,759 --> 00:32:35,679
method which is this one here and that
558
00:32:34,079 --> 00:32:39,519
is the only thing that is public
559
00:32:35,679 --> 00:32:41,679
besides this constructor here so
560
00:32:39,519 --> 00:32:44,720
how can we access these properties then
561
00:32:41,679 --> 00:32:45,519
well as i said we have to expose our own
562
00:32:45,519 --> 00:32:49,200
are going to be public and then by using
563
00:32:49,200 --> 00:32:52,720
other classes and other users will be
564
00:32:52,720 --> 00:32:56,399
interact with this data here with these
565
00:32:56,398 --> 00:33:00,319
so let's create getters and setters for
566
00:33:00,319 --> 00:33:03,359
let's first do it for for our name
567
00:33:03,359 --> 00:33:10,639
so i'm going to do it here uh let's say
568
00:33:10,640 --> 00:33:14,880
and then what this method what this
569
00:33:14,880 --> 00:33:22,080
well that is going to be a string
570
00:33:22,079 --> 00:33:25,599
and then once this function receives
571
00:33:25,599 --> 00:33:30,079
parameter called name what i want to do
572
00:33:27,599 --> 00:33:31,918
with this is i want to assign this value
573
00:33:30,079 --> 00:33:33,199
to this property here so i'm going to
574
00:33:36,558 --> 00:33:40,079
and then i want to create so this here
575
00:33:38,240 --> 00:33:41,759
is a setter and then i want to create a
576
00:33:41,759 --> 00:33:48,558
i am going to say string get name
577
00:33:46,720 --> 00:33:52,079
like this and then here i'm just going
578
00:33:55,119 --> 00:33:58,398
this method here receives one parameter
579
00:33:58,398 --> 00:34:03,599
name and then it sets the value of
580
00:34:01,599 --> 00:34:04,879
our property which is encapsulated which
581
00:34:04,880 --> 00:34:08,159
to that value that we have received in
582
00:34:08,159 --> 00:34:11,358
setter and then this method here get
583
00:34:11,358 --> 00:34:16,000
that method is going to return the value
584
00:34:13,918 --> 00:34:18,559
of our name property that is also
585
00:34:16,000 --> 00:34:20,000
encapsulated and that value is going to
586
00:34:20,000 --> 00:34:24,000
invokes this method and because these
587
00:34:24,000 --> 00:34:28,159
are public anyone outside of our class
588
00:34:28,159 --> 00:34:34,000
set name and get name so these
589
00:34:31,760 --> 00:34:35,679
are examples of setter and getter and
590
00:34:34,000 --> 00:34:37,918
now i'm going to do the same for our
591
00:34:35,679 --> 00:34:39,838
company property and age property and i
592
00:34:37,918 --> 00:34:43,279
will be back to show you that code
593
00:34:39,838 --> 00:34:44,320
in a moment so i have created getters
594
00:34:44,320 --> 00:34:48,000
our name property here so this is sether
595
00:34:48,000 --> 00:34:51,918
our name property and then here is a
596
00:34:49,918 --> 00:34:53,918
setter for our company and then
597
00:34:51,918 --> 00:34:54,960
getter for company property and then
598
00:34:54,960 --> 00:35:01,358
setter and getter methods for age and
599
00:34:58,159 --> 00:35:03,519
now because these methods here are
600
00:35:01,358 --> 00:35:05,679
public everyone outside of our class
601
00:35:03,519 --> 00:35:08,320
should be able to access these methods
602
00:35:05,679 --> 00:35:10,159
whereas these three properties are now
603
00:35:08,320 --> 00:35:11,760
encapsulated which means that they are
604
00:35:11,760 --> 00:35:15,440
within this class and no one else
605
00:35:15,440 --> 00:35:20,400
members of this class here are going to
606
00:35:18,239 --> 00:35:22,879
be able to access them directly
607
00:35:20,400 --> 00:35:25,119
so let's test now these getters and
608
00:35:25,119 --> 00:35:31,599
okay so let's say that employee 1
609
00:35:28,320 --> 00:35:33,920
and let's say set age and i'm going to
610
00:35:31,599 --> 00:35:37,280
say for example that my employee 1 now
611
00:35:43,519 --> 00:35:49,920
gutters i'm going to say for example std
612
00:35:53,440 --> 00:36:00,240
get name okay so our getter should work
613
00:35:57,358 --> 00:36:00,799
like this and then let's say employee
614
00:36:00,800 --> 00:36:04,960
is and i'm going to test if this value
615
00:36:04,960 --> 00:36:11,440
applied successfully so i'm going to say
616
00:36:31,679 --> 00:36:38,319
okay and now if i run this program
617
00:36:35,599 --> 00:36:40,160
we should get the result of this line
618
00:36:38,320 --> 00:36:42,800
here and we should be able to see if our
619
00:36:40,159 --> 00:36:46,319
getters and setters work as they should
620
00:36:46,320 --> 00:36:54,320
okay and now this last line of code here
621
00:36:50,559 --> 00:36:55,838
says salina is 39 years old okay so
622
00:36:54,320 --> 00:36:57,200
i shouldn't have done this for myself i
623
00:36:55,838 --> 00:36:58,559
should have done this for john for
624
00:36:58,559 --> 00:37:01,679
so i'm going to close this program and
625
00:37:01,679 --> 00:37:07,358
these get name and get age and also
626
00:37:05,519 --> 00:37:09,039
get company and then set their methods
627
00:37:09,039 --> 00:37:13,279
whereas the properties that they are
628
00:37:10,719 --> 00:37:14,799
hiding are hidden within the class and
629
00:37:14,800 --> 00:37:18,160
so the only way to access these three
630
00:37:16,639 --> 00:37:20,639
properties is going to be using
631
00:37:18,159 --> 00:37:22,399
these methods now what we can do with
632
00:37:20,639 --> 00:37:25,598
these methods is we can provide
633
00:37:22,400 --> 00:37:26,240
special rules to interact with this data
634
00:37:26,239 --> 00:37:31,279
for example let's say that employee
635
00:37:31,280 --> 00:37:36,960
older than 18 years old so here
636
00:37:34,719 --> 00:37:39,598
when actually here when i'm trying to
637
00:37:36,960 --> 00:37:41,440
set the age of our employee i'm going to
638
00:37:41,440 --> 00:37:45,039
so i'm going to add a validation rule
639
00:37:48,400 --> 00:37:52,240
this age that i have received in my
640
00:37:52,239 --> 00:37:56,399
greater than 18 years old or actually
641
00:37:56,400 --> 00:38:00,240
or equal to than 18 because person that
642
00:38:00,239 --> 00:38:04,879
of age only in the case that this
643
00:38:02,639 --> 00:38:05,440
property here is greater than 18 years
644
00:38:05,440 --> 00:38:08,639
only then i am going to assign that to
645
00:38:08,639 --> 00:38:13,519
property here in the case that our user
646
00:38:11,440 --> 00:38:16,720
anyone else decides to assign value
647
00:38:13,519 --> 00:38:19,920
which is less than 18 so 17 or 15 or 10
648
00:38:16,719 --> 00:38:22,959
or whatever i am not going to apply
649
00:38:19,920 --> 00:38:26,079
those changes so let's test this
650
00:38:26,079 --> 00:38:29,359
age to 39 i'm going to say that salina
651
00:38:29,358 --> 00:38:36,639
15 so let's test those changes
652
00:38:34,480 --> 00:38:38,880
okay and now as you can see it says
653
00:38:38,880 --> 00:38:43,358
why because this set age method here
654
00:38:43,358 --> 00:38:47,279
age is 18 or greater than 18 and only in
655
00:38:47,280 --> 00:38:51,760
those changes are going to be applied
656
00:38:51,760 --> 00:38:56,320
validation rules to your setter methods
657
00:38:54,480 --> 00:38:58,240
and then i'm going to leave to you to
658
00:38:56,320 --> 00:39:01,599
apply certain validation rules
659
00:38:58,239 --> 00:39:04,639
to your set company name and then set
660
00:39:01,599 --> 00:39:07,200
employee name this property here
661
00:39:04,639 --> 00:39:08,879
so you can do that as a homework you can
662
00:39:08,880 --> 00:39:12,079
only letters or you want to allow also
663
00:39:12,079 --> 00:39:15,280
and special characters as the name for
664
00:39:15,280 --> 00:39:20,400
as the name for your company okay so
665
00:39:17,920 --> 00:39:22,720
once more the idea of encapsulation is
666
00:39:22,719 --> 00:39:26,239
so the idea of encapsulation is to make
667
00:39:26,239 --> 00:39:30,799
properties private and then whoever
668
00:39:28,880 --> 00:39:33,119
wants to access these properties
669
00:39:30,800 --> 00:39:35,200
outside of this class will have to go
670
00:39:33,119 --> 00:39:38,559
through the methods that you
671
00:39:35,199 --> 00:39:39,199
expose that do have access to your
672
00:39:39,199 --> 00:39:46,639
properties okay so let's collapse this
673
00:39:46,639 --> 00:39:50,799
getters and setters for these three
674
00:39:50,800 --> 00:39:54,960
the second principle that i want to talk
675
00:39:54,960 --> 00:39:58,639
what is abstraction abstraction means
676
00:39:58,639 --> 00:40:02,960
complex things behind a procedure that
677
00:40:02,960 --> 00:40:06,880
simple so in order to explain this let's
678
00:40:06,880 --> 00:40:11,280
everyday life example let's use the
679
00:40:11,280 --> 00:40:15,599
so one of the main characteristics that
680
00:40:13,679 --> 00:40:16,399
smartphones have these days is that they
681
00:40:16,400 --> 00:40:20,720
pictures so how does that procedure of
682
00:40:20,719 --> 00:40:24,000
well it's pretty simple at least on your
683
00:40:24,000 --> 00:40:27,599
a button and you have taken a picture
684
00:40:27,599 --> 00:40:31,359
and then you make a call or you send a
685
00:40:31,358 --> 00:40:35,199
that is not really that simple because
686
00:40:35,199 --> 00:40:38,719
a button click but for the company that
687
00:40:38,719 --> 00:40:42,879
there is much more complex logic that
688
00:40:42,880 --> 00:40:46,480
in order for you to be able to press a
689
00:40:46,480 --> 00:40:53,599
take a picture so there is some sort of
690
00:40:50,318 --> 00:40:55,679
interface between you and then that
691
00:40:53,599 --> 00:40:56,079
company that produces those smartphones
692
00:40:56,079 --> 00:41:00,160
on your side everything is pretty simple
693
00:40:58,239 --> 00:41:01,919
and easy you just press a button and you
694
00:41:00,159 --> 00:41:03,118
make a call you press a button and you
695
00:41:03,119 --> 00:41:07,280
but then all of that complexity behind
696
00:41:07,280 --> 00:41:11,200
is hidden from you and that complexity
697
00:41:09,519 --> 00:41:14,318
is on the side of the company that
698
00:41:14,318 --> 00:41:18,239
now all of this complexity that is
699
00:41:16,559 --> 00:41:18,719
hidden from you you don't actually need
700
00:41:18,719 --> 00:41:23,039
anything about this in order to be able
701
00:41:20,800 --> 00:41:24,240
to use this site here in order to be
702
00:41:24,239 --> 00:41:30,639
and send messages and take pictures
703
00:41:27,679 --> 00:41:31,919
so what happens if another company comes
704
00:41:30,639 --> 00:41:33,598
and they decide that they want to
705
00:41:33,599 --> 00:41:37,440
what they have to do is they have to
706
00:41:37,440 --> 00:41:41,039
they have to provide you as a user with
707
00:41:41,039 --> 00:41:44,318
nice and beautiful and clean and simple
708
00:41:44,318 --> 00:41:47,519
so that you are able to press a button
709
00:41:47,519 --> 00:41:50,559
and then they have to take care on their
710
00:41:50,559 --> 00:41:56,000
to implement all of that complex
711
00:41:53,599 --> 00:41:58,400
logic which is hidden from you because
712
00:41:56,000 --> 00:42:01,119
if they decide to show you that
713
00:41:58,400 --> 00:42:02,880
ugly side that complex side chances are
714
00:42:01,119 --> 00:42:04,640
that no one is going to want to use
715
00:42:04,639 --> 00:42:08,480
and even if someone decides that they do
716
00:42:08,480 --> 00:42:13,199
they will probably not understand this
717
00:42:11,039 --> 00:42:15,199
complex logic well so that they will
718
00:42:13,199 --> 00:42:17,279
so they will make mistakes and they will
719
00:42:17,280 --> 00:42:22,079
ways so this process of hiding that
720
00:42:22,079 --> 00:42:25,920
as a user is called abstraction and in
721
00:42:25,920 --> 00:42:32,159
this very very complex system is hidden
722
00:42:29,199 --> 00:42:33,279
and this system here is represented as
723
00:42:33,280 --> 00:42:38,000
system by this contract so let's see how
724
00:42:38,000 --> 00:42:42,159
this contract here that makes one side
725
00:42:40,480 --> 00:42:44,639
look simple and then other side
726
00:42:42,159 --> 00:42:46,480
very complex let's implement that on the
727
00:42:44,639 --> 00:42:47,679
code and example that we have here in
728
00:42:47,679 --> 00:42:51,039
so let's take a look at this employee
729
00:42:51,039 --> 00:42:54,480
what kind of functionality complex
730
00:42:54,480 --> 00:42:58,079
implement on this class here let's say
731
00:42:58,079 --> 00:43:01,359
that every employee can ask for
732
00:43:01,358 --> 00:43:06,078
so he goes to his boss and he says hey
733
00:43:06,079 --> 00:43:09,359
and then his boss needs to go through
734
00:43:09,358 --> 00:43:12,799
complex thought process in order to
735
00:43:12,800 --> 00:43:18,880
this person deserves a promotion or not
736
00:43:16,400 --> 00:43:20,240
so his boss needs to consider many
737
00:43:20,239 --> 00:43:24,318
consider for example how long this
738
00:43:22,559 --> 00:43:26,400
person has been working for the company
739
00:43:24,318 --> 00:43:28,480
how much knowledge this person has
740
00:43:26,400 --> 00:43:30,079
and then what kind of relationship this
741
00:43:28,480 --> 00:43:31,760
person has with their colleagues how
742
00:43:30,079 --> 00:43:32,640
much this person is contributing to the
743
00:43:32,639 --> 00:43:35,759
um and then is this person always late
744
00:43:35,760 --> 00:43:39,440
are they breaking deadlines and many
745
00:43:43,440 --> 00:43:48,079
and in this situation we are doing that
746
00:43:46,079 --> 00:43:51,039
for another developer that is going to
747
00:43:48,079 --> 00:43:54,079
use our class so here in this situation
748
00:43:51,039 --> 00:43:56,079
we are the ones who are making this
749
00:43:54,079 --> 00:43:58,160
class so we are producing that
750
00:43:56,079 --> 00:44:00,880
smartphone so we are the ones
751
00:43:58,159 --> 00:44:01,679
who should provide that complex logic
752
00:44:01,679 --> 00:44:05,838
functionalities that are complex and
753
00:44:05,838 --> 00:44:09,838
simple and basic interface for anyone
754
00:44:09,838 --> 00:44:14,799
this class here so how do you do that
755
00:44:14,800 --> 00:44:18,480
that contract well the answer is by
756
00:44:18,480 --> 00:44:22,318
abstract classes for those of you who
757
00:44:22,318 --> 00:44:26,400
c sharp or java world there is already
758
00:44:26,400 --> 00:44:29,599
interface that some of you might be
759
00:44:29,599 --> 00:44:33,359
plus plus you can simulate behavior of
760
00:44:33,358 --> 00:44:39,440
by using abstract class so let's create
761
00:44:39,440 --> 00:44:43,440
so here on the top i am going to create
762
00:44:43,440 --> 00:44:51,599
and let's create a class called abstract
763
00:44:51,599 --> 00:44:58,000
like this or you can call it i
764
00:44:54,639 --> 00:45:00,480
employee if you want so this class here
765
00:44:58,000 --> 00:45:03,519
is going to serve as a contract
766
00:45:00,480 --> 00:45:06,159
and this contract will have only one
767
00:45:03,519 --> 00:45:06,719
rule and that rule is that whichever
768
00:45:06,719 --> 00:45:11,039
decides to sign this contract that class
769
00:45:11,039 --> 00:45:14,719
implementation for a method called ask
770
00:45:14,719 --> 00:45:23,358
so a method of return type void
771
00:45:24,719 --> 00:45:29,199
like this and we are also going to make
772
00:45:29,199 --> 00:45:33,439
obligatory which means that we will
773
00:45:33,440 --> 00:45:38,400
signs this contract here to implement
774
00:45:35,920 --> 00:45:41,280
this method here so how can we make this
775
00:45:38,400 --> 00:45:42,480
obligatory the answer is by making this
776
00:45:42,480 --> 00:45:47,199
a pure virtual function so i'm going to
777
00:45:51,199 --> 00:45:58,318
here i'm going to say is equal to zero
778
00:45:55,358 --> 00:45:59,759
and now this class here has become an
779
00:45:59,760 --> 00:46:03,040
and this method here this function is
780
00:46:03,039 --> 00:46:09,599
virtual function or an abstract function
781
00:46:06,559 --> 00:46:10,880
and this means that whoever decides to
782
00:46:10,880 --> 00:46:16,079
called that abstractemployee.com
783
00:46:14,159 --> 00:46:18,639
that signs this contract will have to
784
00:46:16,079 --> 00:46:21,760
provide implementation for this
785
00:46:18,639 --> 00:46:24,078
method here so now the question is how
786
00:46:21,760 --> 00:46:26,640
can my class my employee class
787
00:46:24,079 --> 00:46:28,160
sign this contract here the answer is
788
00:46:28,159 --> 00:46:33,199
you add here column sign and then you
789
00:46:30,880 --> 00:46:33,760
specify the name of your contract which
790
00:46:37,838 --> 00:46:40,960
okay and now my class has successfully
791
00:46:40,960 --> 00:46:44,000
this contract and immediately if you
792
00:46:44,000 --> 00:46:49,920
these two red dots that appeared below
793
00:46:47,679 --> 00:46:51,838
if we hover over these errors because
794
00:46:49,920 --> 00:46:52,800
these are errors you can see that it
795
00:46:52,800 --> 00:46:56,000
pure virtual function abstract employee
796
00:46:59,039 --> 00:47:02,719
which means hey you signed this contract
797
00:47:02,719 --> 00:47:06,159
but you are not obeying the rules of
798
00:47:06,159 --> 00:47:09,679
providing implementation for this
799
00:47:09,679 --> 00:47:14,559
so in order to avoid these errors and
800
00:47:12,719 --> 00:47:16,959
fix these errors that we have now what
801
00:47:14,559 --> 00:47:19,759
we have to do is we have to provide
802
00:47:16,960 --> 00:47:21,519
implementation for this method here so
803
00:47:21,519 --> 00:47:28,000
okay i am going to copy it here and then
804
00:47:28,000 --> 00:47:31,199
and here in these curly brackets we are
805
00:47:31,199 --> 00:47:35,598
implement that method but as you could
806
00:47:33,039 --> 00:47:38,800
see immediately the errors that we had
807
00:47:35,599 --> 00:47:41,200
below have disappeared so here
808
00:47:38,800 --> 00:47:42,000
inside this class i want to provide
809
00:47:42,000 --> 00:47:47,199
for ask for promotion method so once
810
00:47:44,960 --> 00:47:48,720
more we said that that logic is pretty
811
00:47:47,199 --> 00:47:50,558
complex and that we should take in
812
00:47:50,559 --> 00:47:54,160
so things like how long that person has
813
00:47:54,159 --> 00:47:57,598
how much that person is contributing to
814
00:47:56,000 --> 00:47:58,239
the company what kind of relationship
815
00:47:58,239 --> 00:48:02,479
with their colleagues and so on but
816
00:48:00,719 --> 00:48:03,039
considering that we don't have all of
817
00:48:03,039 --> 00:48:06,318
information what i'm going to do is i'm
818
00:48:04,800 --> 00:48:07,200
going to make it a little bit more
819
00:48:07,199 --> 00:48:10,480
so i'm going to say for example any
820
00:48:10,480 --> 00:48:14,559
that is older than 30 years old can get
821
00:48:14,559 --> 00:48:22,480
for example so i'm going to say here if
822
00:48:22,480 --> 00:48:28,880
i will do the following i will say std
823
00:48:25,920 --> 00:48:32,318
see out and then let's write the name of
824
00:48:28,880 --> 00:48:37,119
this employee i'm going to say name
825
00:48:32,318 --> 00:48:37,119
and i'm going to say got promoted
826
00:48:42,400 --> 00:48:47,599
excellent and then in the else case
827
00:48:45,679 --> 00:48:49,838
so in the case that person has less than
828
00:48:47,599 --> 00:48:51,280
30 years i'm going to write something
829
00:48:51,280 --> 00:48:54,800
deny that promotion to the person who is
830
00:48:54,800 --> 00:49:02,000
so i'm going to say name and then
831
00:48:58,000 --> 00:49:04,239
let's write out sorry no promotion for
832
00:49:08,159 --> 00:49:13,920
now let's test this method here
833
00:49:11,358 --> 00:49:14,558
so i'm going to go to my main function
834
00:49:14,559 --> 00:49:18,880
will delete all of this code here
835
00:49:22,559 --> 00:49:26,079
this method here on my employee 1 and
836
00:49:26,079 --> 00:49:29,440
so that i see if any of them can get
837
00:49:36,400 --> 00:49:41,599
employee two dot ask for promotion as
838
00:49:45,280 --> 00:49:48,480
it says salinas sorry no promotion for
839
00:49:51,920 --> 00:49:55,200
and why that happened because let me
840
00:49:55,199 --> 00:50:02,000
because salina is 25 and then john is
841
00:49:58,800 --> 00:50:02,960
35 and this rule here says that whoever
842
00:50:02,960 --> 00:50:07,119
than 30 which is john he gets promoted
843
00:50:07,119 --> 00:50:11,680
if that person is younger than 30 that
844
00:50:11,679 --> 00:50:16,318
promoted okay so with this we have
845
00:50:16,318 --> 00:50:20,480
we have implemented a contract which is
846
00:50:20,480 --> 00:50:25,119
class and that abstract class or that
847
00:50:25,119 --> 00:50:30,880
one rule and that rule is this pure
848
00:50:28,480 --> 00:50:34,079
virtual function here which is called
849
00:50:30,880 --> 00:50:36,800
ask for promotion so that means that
850
00:50:34,079 --> 00:50:38,400
whichever class signs this contract
851
00:50:38,400 --> 00:50:41,920
inherits from this abstract employee
852
00:50:41,920 --> 00:50:45,280
this class here that class will have to
853
00:50:45,280 --> 00:50:49,040
implementation for this method here so
854
00:50:49,039 --> 00:50:53,519
promotion so in this situation our
855
00:50:53,519 --> 00:50:57,039
inheriting from our abstract employees
856
00:50:57,039 --> 00:51:02,159
is signing that contract which means
857
00:50:59,920 --> 00:51:05,200
that that class needs to provide
858
00:51:02,159 --> 00:51:05,838
implementation for the method that is in
859
00:51:05,838 --> 00:51:09,679
contract okay now what this allows us to
860
00:51:09,679 --> 00:51:14,719
when another developer comes to our code
861
00:51:12,159 --> 00:51:16,879
and wants to use our employee class
862
00:51:14,719 --> 00:51:18,318
that developer will see this contract
863
00:51:18,318 --> 00:51:23,759
oh so this employee class has method
864
00:51:21,119 --> 00:51:26,400
which is called ask for promotion
865
00:51:23,760 --> 00:51:29,200
so that means that if i use employee
866
00:51:26,400 --> 00:51:31,039
class i can invoke this method here
867
00:51:31,039 --> 00:51:33,920
complexity of this method i don't need
868
00:51:33,920 --> 00:51:38,400
about how this promotion is given or
869
00:51:38,400 --> 00:51:42,400
because that is the worry of the person
870
00:51:42,400 --> 00:51:46,960
this class here so that is the worry of
871
00:51:45,199 --> 00:51:47,919
the person who is going to sign this
872
00:51:47,920 --> 00:51:51,680
and in this situation we are that person
873
00:51:51,679 --> 00:51:55,440
producing that smartphone we are the
874
00:51:53,280 --> 00:51:58,800
person who is writing this class
875
00:51:55,440 --> 00:52:01,119
and this class is signing the contract
876
00:51:58,800 --> 00:52:02,880
which means that we have to provide
877
00:52:02,880 --> 00:52:09,119
method here as we did in this situation
878
00:52:06,480 --> 00:52:10,318
and we tested it and as you could see i
879
00:52:10,318 --> 00:52:14,000
and john did so that is the idea of
880
00:52:14,000 --> 00:52:17,358
and this ask for promotion method is
881
00:52:17,358 --> 00:52:20,880
that button that we mentioned a button
882
00:52:20,880 --> 00:52:25,599
from the beginning of this chapter the
883
00:52:23,838 --> 00:52:27,519
third principle of object oriented
884
00:52:25,599 --> 00:52:30,400
programming that i want to talk about
885
00:52:27,519 --> 00:52:32,480
is called inheritance the idea of
886
00:52:32,480 --> 00:52:36,159
so there is this base class also known
887
00:52:36,159 --> 00:52:39,440
super class or parent class and then
888
00:52:39,440 --> 00:52:45,920
derived class also known as child class
889
00:52:42,639 --> 00:52:48,078
or subclass now this base class here
890
00:52:45,920 --> 00:52:51,599
this parent class has certain
891
00:52:48,079 --> 00:52:54,559
attributes and behaviors it has members
892
00:52:51,599 --> 00:52:55,519
and then if this class here decides to
893
00:52:55,519 --> 00:52:58,880
from the base class at that moment this
894
00:52:58,880 --> 00:53:04,160
becomes a child class and by that this
895
00:53:04,159 --> 00:53:07,279
all of the members of this base class
896
00:53:07,280 --> 00:53:11,200
which means this class is going to have
897
00:53:11,199 --> 00:53:15,519
attributes and behaviors as its base
898
00:53:15,519 --> 00:53:20,880
and then this derived class can also
899
00:53:18,559 --> 00:53:22,640
have its own members that are specific
900
00:53:22,639 --> 00:53:28,799
which this class here does not have so
901
00:53:26,159 --> 00:53:31,118
in order to explain this let's use an
902
00:53:28,800 --> 00:53:31,760
example that is going to be familiar to
903
00:53:31,760 --> 00:53:35,599
let's let's return to the example of a
904
00:53:35,599 --> 00:53:41,519
in the beginning of this video so a car
905
00:53:39,039 --> 00:53:42,079
as a class is going to have certain
906
00:53:42,079 --> 00:53:48,640
for example a name a model price
907
00:53:45,519 --> 00:53:50,960
a color and so on and then it is going
908
00:53:48,639 --> 00:53:52,639
to have behaviors as well for example it
909
00:53:52,639 --> 00:53:57,838
drive now what kind of derived classes
910
00:53:57,838 --> 00:54:01,759
base class what kind of classes are more
911
00:54:01,760 --> 00:54:04,880
so what kind of car types are more
912
00:54:04,880 --> 00:54:09,440
than just car let's say for example that
913
00:54:09,440 --> 00:54:13,679
one derived class that is called
914
00:54:11,440 --> 00:54:15,200
electric car and then another derived
915
00:54:18,480 --> 00:54:23,039
this electric car is also going to have
916
00:54:21,199 --> 00:54:24,558
all the same properties that a car has
917
00:54:24,559 --> 00:54:28,319
um it is going to have a name a model a
918
00:54:28,318 --> 00:54:32,318
and then it is going to have a method
919
00:54:29,920 --> 00:54:34,400
called drive and then this conventional
920
00:54:32,318 --> 00:54:35,279
car is also going to have all those same
921
00:54:35,280 --> 00:54:39,440
but they are going to be different
922
00:54:37,199 --> 00:54:42,318
between them this car for example
923
00:54:39,440 --> 00:54:42,960
is going to have a method called charge
924
00:54:42,960 --> 00:54:47,039
is electric car and we need to use
925
00:54:45,199 --> 00:54:49,118
electricity to charge it and then it is
926
00:54:47,039 --> 00:54:51,920
going to have an attribute called
927
00:54:49,119 --> 00:54:52,880
for example battery status and then this
928
00:54:52,880 --> 00:54:56,960
gas car conventional car is going to
929
00:54:56,960 --> 00:55:00,639
think which means a method of putting
930
00:55:00,639 --> 00:55:04,960
in that car and then it is going to have
931
00:55:04,960 --> 00:55:08,240
which is named tank status which shows
932
00:55:08,239 --> 00:55:12,159
how much gas do you have left in your
933
00:55:12,159 --> 00:55:15,759
derived classes is going to have their
934
00:55:15,760 --> 00:55:21,119
but they will also inherit all of those
935
00:55:18,880 --> 00:55:23,838
attributes that their base class
936
00:55:21,119 --> 00:55:25,760
has so now let's see how we can
937
00:55:23,838 --> 00:55:26,719
implement that on the example that we
938
00:55:26,719 --> 00:55:32,159
so here i have class called employee and
939
00:55:30,000 --> 00:55:34,159
what i want to do is i want to create a
940
00:55:34,159 --> 00:55:37,838
here so what kind of class can inherit
941
00:55:37,838 --> 00:55:42,719
employee class logically what is more
942
00:55:42,719 --> 00:55:46,719
let's say for example that we want to
943
00:55:46,719 --> 00:55:51,439
developer so let's do that here i'm
944
00:55:51,440 --> 00:55:55,760
and then let's call this class developer
945
00:55:59,679 --> 00:56:02,960
we have created a class called developer
946
00:56:02,960 --> 00:56:06,079
is i want to make this class inherit
947
00:56:06,079 --> 00:56:10,480
this class here so how do you do that
948
00:56:10,480 --> 00:56:15,358
you put column sign here and then you
949
00:56:13,039 --> 00:56:16,558
specify the name of your base class and
950
00:56:20,960 --> 00:56:25,280
a few things happened this class here
951
00:56:25,280 --> 00:56:28,640
a child class which means a subclass or
952
00:56:28,639 --> 00:56:32,558
or derived class and then this class
953
00:56:32,559 --> 00:56:35,920
is now called base class or super class
954
00:56:35,920 --> 00:56:39,599
parent class and also another thing that
955
00:56:39,599 --> 00:56:43,119
is that this developer class here now
956
00:56:43,119 --> 00:56:46,960
all of these properties and then these
957
00:56:46,960 --> 00:56:53,119
as well that our employee class has
958
00:56:50,400 --> 00:56:55,039
so developer has all of the properties
959
00:56:53,119 --> 00:56:56,480
that employee has and then let's create
960
00:56:55,039 --> 00:56:58,159
one more property that is going to be
961
00:56:58,159 --> 00:57:01,838
only so i'm going to make that property
962
00:57:01,838 --> 00:57:05,119
so here i'm not obeying the rules of
963
00:57:03,519 --> 00:57:06,960
encapsulation you can do that for
964
00:57:06,960 --> 00:57:11,119
so let's create a property that only
965
00:57:11,119 --> 00:57:14,798
employee does not that is going to be
966
00:57:14,798 --> 00:57:19,039
favorite programming language developer
967
00:57:17,199 --> 00:57:20,960
has favorite programming language but an
968
00:57:19,039 --> 00:57:22,000
employee does not necessarily have to be
969
00:57:22,000 --> 00:57:26,400
so he does not necessarily need to have
970
00:57:24,079 --> 00:57:27,039
a favorite programming language so i'm
971
00:57:36,480 --> 00:57:42,559
i hope that i didn't make a typo here so
972
00:57:40,000 --> 00:57:43,440
now let's check if everything that i
973
00:57:43,440 --> 00:57:49,920
correct so if this developer indeed has
974
00:57:46,960 --> 00:57:50,798
access to all the properties that our
975
00:57:50,798 --> 00:57:54,000
and then to this one here which is
976
00:57:57,280 --> 00:58:00,640
this code here and then i want to create
977
00:58:00,639 --> 00:58:06,000
object of class developer so i'm going
978
00:58:06,639 --> 00:58:12,960
and let's call that object d like this
979
00:58:10,559 --> 00:58:13,839
okay and the first thing that happens is
980
00:58:17,280 --> 00:58:20,960
it says default constructor of developer
981
00:58:20,960 --> 00:58:24,480
referenced so this error here is
982
00:58:24,480 --> 00:58:28,559
obviously because we don't have a
983
00:58:26,159 --> 00:58:30,399
default constructor and if you remember
984
00:58:28,559 --> 00:58:31,680
when we were talking about constructors
985
00:58:31,679 --> 00:58:34,960
every class when you create it every
986
00:58:34,960 --> 00:58:38,000
automatically generated default
987
00:58:38,000 --> 00:58:41,199
but once you decide to create your own
988
00:58:41,199 --> 00:58:44,639
lose that default constructor so now you
989
00:58:44,639 --> 00:58:49,519
well selena we have not created
990
00:58:47,199 --> 00:58:52,318
constructor for this developer class
991
00:58:49,519 --> 00:58:53,838
and i'm going to say you're right but we
992
00:58:53,838 --> 00:58:59,519
employee class and that employee class
993
00:58:56,960 --> 00:59:01,280
has a constructor that we created so
994
00:58:59,519 --> 00:59:04,318
that means that we need to provide a
995
00:59:01,280 --> 00:59:07,440
constructor for the derived classes of
996
00:59:04,318 --> 00:59:08,239
employee class which is our developer
997
00:59:08,239 --> 00:59:12,239
so now what i'm going to do is i'm going
998
00:59:10,159 --> 00:59:13,118
to create a constructor for developer
999
00:59:13,119 --> 00:59:17,440
so if you remember three rules of
1000
00:59:17,440 --> 00:59:20,639
first rule is that it does not have
1001
00:59:20,639 --> 00:59:23,920
second rule is that it has the same name
1002
00:59:23,920 --> 00:59:27,119
and then third rule is that it needs to
1003
00:59:27,119 --> 00:59:32,240
public so i'm going to call this
1004
00:59:36,318 --> 00:59:39,759
and then i need to put these curly
1005
00:59:39,760 --> 00:59:44,240
so now the job of this constructor here
1006
00:59:44,239 --> 00:59:48,798
developer object and considering that
1007
00:59:46,719 --> 00:59:49,838
this developer object inherited from
1008
00:59:49,838 --> 00:59:55,199
that means that developer has
1009
00:59:52,960 --> 00:59:56,960
these properties so these attributes and
1010
00:59:56,960 --> 01:00:01,440
specific for only developer class so now
1011
01:00:01,440 --> 01:00:06,400
provide this constructor with those
1012
01:00:04,000 --> 01:00:08,639
parameters so i'm going to copy
1013
01:00:06,400 --> 01:00:09,599
these three here because i already have
1014
01:00:09,599 --> 01:00:14,318
and then paste them here and then i will
1015
01:00:11,838 --> 01:00:18,239
add this parameter which is specific for
1016
01:00:14,318 --> 01:00:21,759
developer class so i will say string
1017
01:00:21,838 --> 01:00:25,279
like this so this is favorite
1018
01:00:25,280 --> 01:00:28,640
actually let's call it favorite
1019
01:00:26,559 --> 01:00:30,079
programming language so that we are
1020
01:00:33,760 --> 01:00:38,160
these four parameters to our developer
1021
01:00:38,159 --> 01:00:41,519
what we have to do is we have to
1022
01:00:41,519 --> 01:00:45,199
attribute here and then these three
1023
01:00:43,679 --> 01:00:46,480
attributes here because those are
1024
01:00:46,480 --> 01:00:50,880
developer class has but one thing that i
1025
01:00:49,119 --> 01:00:53,599
want to show you is following
1026
01:00:50,880 --> 01:00:55,760
considering that this developer class is
1027
01:00:53,599 --> 01:00:57,760
inheriting from employee class
1028
01:00:55,760 --> 01:00:59,839
and that employee class has a
1029
01:00:57,760 --> 01:01:00,400
constructor of its own which is this one
1030
01:01:00,400 --> 01:01:03,519
that constructor already knows how to
1031
01:01:03,519 --> 01:01:08,239
name and company at age so that means
1032
01:01:06,318 --> 01:01:10,318
that we don't have to worry about
1033
01:01:08,239 --> 01:01:12,798
these three properties in our developer
1034
01:01:10,318 --> 01:01:15,199
class we should only worry about
1035
01:01:12,798 --> 01:01:17,119
a specific properties of our developer
1036
01:01:15,199 --> 01:01:18,960
class and these three properties that
1037
01:01:17,119 --> 01:01:21,358
actually belong to our employee
1038
01:01:18,960 --> 01:01:22,318
we can pass these three to our employee
1039
01:01:22,318 --> 01:01:29,039
so i'm going to do that like this
1040
01:01:25,679 --> 01:01:31,598
here so after this parentheses
1041
01:01:29,039 --> 01:01:34,000
you put a column sign and then you
1042
01:01:31,599 --> 01:01:38,559
specify the name of that constructor so
1043
01:01:38,559 --> 01:01:43,920
base class and then here i will pass
1044
01:01:41,760 --> 01:01:44,960
these parameters that our base class
1045
01:01:52,318 --> 01:01:57,279
okay so now this constructor the
1046
01:01:55,440 --> 01:01:58,400
constructor of our base class is going
1047
01:01:58,400 --> 01:02:02,639
these three properties and we are left
1048
01:02:00,960 --> 01:02:03,599
with this property here which is
1049
01:02:03,599 --> 01:02:07,680
for our developer class so here i want
1050
01:02:07,679 --> 01:02:12,399
our favorite language property
1051
01:02:11,039 --> 01:02:14,799
so i'm going to say that favorite
1052
01:02:14,798 --> 01:02:21,280
has been passed to this constructor
1053
01:02:21,280 --> 01:02:25,519
down there to my main function where i
1054
01:02:25,519 --> 01:02:30,159
let's try to construct an object of type
1055
01:02:28,400 --> 01:02:31,358
developer let's try to invoke this
1056
01:02:31,358 --> 01:02:34,880
like we did in this line here and then
1057
01:02:34,880 --> 01:02:40,000
employee so i'm going to say that a
1058
01:02:37,280 --> 01:02:43,599
developer is going to be equal to
1059
01:02:40,000 --> 01:02:46,639
developer and then here i want to pass
1060
01:02:43,599 --> 01:02:47,280
values for this constructor here so
1061
01:02:47,280 --> 01:02:50,880
name and company name and age and
1062
01:02:49,119 --> 01:02:53,358
favorite programming language
1063
01:02:50,880 --> 01:02:55,680
so let's call this developer also
1064
01:02:53,358 --> 01:02:58,558
salvina i'm going to delete these two
1065
01:02:55,679 --> 01:02:59,519
because we will not need them anymore
1066
01:02:59,519 --> 01:03:03,679
and then let's say that i work for
1067
01:03:07,199 --> 01:03:10,798
i am 25 years old and my favorite
1068
01:03:15,039 --> 01:03:18,880
so now that i have successfully created
1069
01:03:18,880 --> 01:03:22,798
let's test this so how am i going to
1070
01:03:22,798 --> 01:03:27,199
well let's create a method on our
1071
01:03:25,280 --> 01:03:28,319
developer class that is going to use
1072
01:03:28,318 --> 01:03:32,239
values here so that we can know if these
1073
01:03:32,239 --> 01:03:35,759
successfully so what kind of method can
1074
01:03:35,760 --> 01:03:40,240
implement on developer class for example
1075
01:03:38,480 --> 01:03:43,199
we can implement a method called
1076
01:03:40,239 --> 01:03:45,038
fix bug so i'm going to do that let's
1077
01:03:43,199 --> 01:03:47,358
collapse this constructor and let's say
1078
01:03:50,400 --> 01:03:54,798
and then what i want this method here to
1079
01:03:58,000 --> 01:04:01,440
plus or selena fixed but using c plus
1080
01:04:01,440 --> 01:04:05,599
so here i'm going to do that i'm going
1081
01:04:05,599 --> 01:04:08,880
c out and then in order to access the
1082
01:04:08,880 --> 01:04:12,079
of this employee i'm going to use getter
1083
01:04:12,079 --> 01:04:19,200
get name which we created previously
1084
01:04:15,920 --> 01:04:22,639
and then here i'm going to say fixed
1085
01:04:22,639 --> 01:04:27,838
and then let's use this programming
1086
01:04:29,838 --> 01:04:36,159
like this okay and now if i want to test
1087
01:04:33,920 --> 01:04:38,159
this method here i can invoke it on my
1088
01:04:36,159 --> 01:04:43,199
developer class so i'm going to say d
1089
01:04:38,159 --> 01:04:46,239
dot fix bug and if i run my program
1090
01:04:43,199 --> 01:04:46,879
as you can see this method here works
1091
01:04:46,880 --> 01:04:50,559
the values that we passed in our
1092
01:04:48,719 --> 01:04:52,078
constructor have successfully been set
1093
01:04:52,079 --> 01:04:56,640
and then fixed bug using and then here
1094
01:04:53,920 --> 01:04:59,599
is my favorite programming language
1095
01:04:56,639 --> 01:05:01,598
okay let's close this and now there is
1096
01:04:59,599 --> 01:05:04,000
one more thing that i want to show you
1097
01:05:01,599 --> 01:05:05,280
and that is this part here so i want you
1098
01:05:05,280 --> 01:05:08,640
that this here is the way that we are
1099
01:05:08,639 --> 01:05:12,239
in order to access properties of our
1100
01:05:12,239 --> 01:05:15,598
and then we are accessing directly
1101
01:05:15,599 --> 01:05:21,920
our developer class so can i
1102
01:05:19,280 --> 01:05:24,000
access properties of my employee class
1103
01:05:24,000 --> 01:05:29,358
this property here for example name or
1104
01:05:27,119 --> 01:05:31,440
do i have to access it by using
1105
01:05:29,358 --> 01:05:33,038
getter write me your answers in the
1106
01:05:33,039 --> 01:05:37,200
so i am going to test that now here i'm
1107
01:05:37,199 --> 01:05:44,879
name and we get an error it says
1108
01:05:41,440 --> 01:05:45,599
employee name is inaccessible so we
1109
01:05:45,599 --> 01:05:50,000
access this property here name which is
1110
01:05:50,000 --> 01:05:54,719
our employee class why well if you
1111
01:05:53,199 --> 01:05:57,279
remember when we were talking about
1112
01:05:57,280 --> 01:06:00,559
whatever is private that is going to be
1113
01:06:00,559 --> 01:06:03,839
hidden that is going to be accessible
1114
01:06:03,838 --> 01:06:08,798
that class and then whatever is public
1115
01:06:06,639 --> 01:06:09,920
we will be able to access that outside
1116
01:06:09,920 --> 01:06:16,000
so how can i make this property here
1117
01:06:13,519 --> 01:06:18,159
how can i make this name accessible in
1118
01:06:16,000 --> 01:06:20,798
the derived classes of this class
1119
01:06:18,159 --> 01:06:22,960
here again when we were talking about
1120
01:06:20,798 --> 01:06:24,159
access modifiers we said that there are
1121
01:06:24,159 --> 01:06:29,358
public private and protected and i
1122
01:06:27,039 --> 01:06:30,400
promised you that i am going to explain
1123
01:06:30,400 --> 01:06:36,559
what protected access modifier means so
1124
01:06:34,000 --> 01:06:37,760
in this situation if we make this
1125
01:06:37,760 --> 01:06:41,440
if we make this name property protected
1126
01:06:41,440 --> 01:06:46,720
name available in derived classes of
1127
01:06:50,400 --> 01:06:56,160
like this and then i'm going to take
1128
01:06:53,599 --> 01:06:57,838
this property and i will move it to my
1129
01:06:57,838 --> 01:07:01,599
and then if i return to my developer
1130
01:07:01,599 --> 01:07:04,960
as you can see the error that we
1131
01:07:04,960 --> 01:07:11,838
is now gone so now this name property
1132
01:07:08,318 --> 01:07:12,880
is accessible directly from this derived
1133
01:07:12,880 --> 01:07:17,119
and if i run the program it should work
1134
01:07:17,119 --> 01:07:21,599
and as you can see it indeed works the
1135
01:07:19,358 --> 01:07:25,119
same so selena fixed bug using
1136
01:07:21,599 --> 01:07:25,920
c plus okay now there is one more very
1137
01:07:25,920 --> 01:07:30,240
related to inheritance that i want to
1138
01:07:27,679 --> 01:07:31,679
show you and that is the following
1139
01:07:30,239 --> 01:07:34,959
let's say for example that this
1140
01:07:31,679 --> 01:07:37,598
developer here fixes a lot of bugs
1141
01:07:34,960 --> 01:07:40,639
so if i run my program you can see that
1142
01:07:37,599 --> 01:07:43,760
salvina indeed is fixing a lot of bugs
1143
01:07:40,639 --> 01:07:46,239
so what this developer here decides then
1144
01:07:43,760 --> 01:07:47,680
is he or she in this situation she
1145
01:07:47,679 --> 01:07:52,078
for promotion so i'm going to try to do
1146
01:07:52,079 --> 01:07:58,160
dot but if you look at the list
1147
01:07:55,358 --> 01:07:59,920
that i have here it only has the access
1148
01:07:58,159 --> 01:08:00,558
to favorite programming language and
1149
01:08:00,559 --> 01:08:05,280
fix bug so if you look at this you can
1150
01:08:03,358 --> 01:08:06,558
see that i don't have access to the
1151
01:08:06,559 --> 01:08:09,599
that i have inherited from my employee
1152
01:08:09,599 --> 01:08:14,079
i do have access to them here so if i
1153
01:08:14,079 --> 01:08:17,440
in this method here so if i say here for
1154
01:08:17,439 --> 01:08:22,079
ask for promotion as you can see i do
1155
01:08:22,079 --> 01:08:26,559
but as you could see on my developer
1156
01:08:26,560 --> 01:08:30,719
access those properties so how can i fix
1157
01:08:30,719 --> 01:08:36,079
well the answer is pretty simple this
1158
01:08:33,838 --> 01:08:37,039
inheritance here so this part of the
1159
01:08:37,039 --> 01:08:41,600
this inheritance is private by default
1160
01:08:39,920 --> 01:08:42,000
and in order to fix the problem that we
1161
01:08:42,000 --> 01:08:45,119
we need to make it public so here i'm
1162
01:08:45,119 --> 01:08:51,358
public and the problem should
1163
01:08:48,640 --> 01:08:52,159
disappear so if i return to my developer
1164
01:08:52,158 --> 01:08:55,439
and if i try to ask for promotion as you
1165
01:08:58,479 --> 01:09:01,919
maybe i'll get a promotion this time
1166
01:09:01,920 --> 01:09:07,440
this and unfortunately i am not
1167
01:09:05,359 --> 01:09:08,960
getting a promotion but at least my code
1168
01:09:08,960 --> 01:09:13,920
now we have access to this method here
1169
01:09:13,920 --> 01:09:17,759
employee class so in my base class and
1170
01:09:17,759 --> 01:09:22,719
by making this inheritance public
1171
01:09:22,719 --> 01:09:27,119
inheritance let's create one more class
1172
01:09:25,359 --> 01:09:30,079
that is going to inherit from this
1173
01:09:27,119 --> 01:09:31,039
employee class so let's create one more
1174
01:09:31,039 --> 01:09:37,679
besides this developer so what kind of
1175
01:09:34,880 --> 01:09:38,640
class can inherit from employee what is
1176
01:09:38,640 --> 01:09:43,440
class than employee for example let's
1177
01:09:43,439 --> 01:09:48,079
teacher so here i'm going to go one more
1178
01:09:46,079 --> 01:09:52,000
time through this process of creating
1179
01:09:52,000 --> 01:09:59,679
class and then i will call it teacher
1180
01:09:59,679 --> 01:10:02,880
we have successfully created a class
1181
01:10:02,880 --> 01:10:06,079
and now what i want to do is i want to
1182
01:10:09,198 --> 01:10:12,879
like this okay and then what i need to
1183
01:10:12,880 --> 01:10:17,679
is let's first create some specific
1184
01:10:15,600 --> 01:10:19,840
attributes for my teacher so let's say
1185
01:10:23,760 --> 01:10:27,520
so this here is the subject that my
1186
01:10:27,520 --> 01:10:30,880
and then let's say also that the teacher
1187
01:10:30,880 --> 01:10:34,800
a functionality that is going to be
1188
01:10:34,800 --> 01:10:38,000
and that functionality will be called
1189
01:10:38,000 --> 01:10:41,359
lesson for example so i'm going to say
1190
01:10:49,920 --> 01:10:55,679
and what i want to do here is i want to
1191
01:10:53,760 --> 01:10:57,360
do something like this so i want to say
1192
01:10:55,679 --> 01:10:58,719
for example the name of that teacher and
1193
01:10:58,719 --> 01:11:05,359
he or she is preparing lesson from this
1194
01:11:02,079 --> 01:11:08,640
subject here so let's delete this
1195
01:11:05,359 --> 01:11:12,079
empty space so i'm going to say std
1196
01:11:08,640 --> 01:11:15,440
c out and then let's put the name of my
1197
01:11:18,719 --> 01:11:26,960
preparing and then here i want to put
1198
01:11:22,479 --> 01:11:31,519
the name of my subject like this
1199
01:11:36,880 --> 01:11:42,079
okay now there are a few problems
1200
01:11:40,000 --> 01:11:43,520
that we have with this class here the
1201
01:11:43,520 --> 01:11:47,600
these here are private which means that
1202
01:11:45,840 --> 01:11:49,600
we will not be able to access them
1203
01:11:47,600 --> 01:11:51,120
outside of this class here because
1204
01:11:51,119 --> 01:11:54,800
is private by default so that is going
1205
01:11:54,800 --> 01:11:58,800
and let's fix it so i'm going to say
1206
01:11:58,800 --> 01:12:03,520
like this okay now second thing that is
1207
01:12:01,840 --> 01:12:05,679
going to be a problem for us is that
1208
01:12:03,520 --> 01:12:06,719
this teacher class here does not have a
1209
01:12:06,719 --> 01:12:11,840
so if i try to create an object of type
1210
01:12:09,600 --> 01:12:13,440
teacher i will not be able to do that so
1211
01:12:13,439 --> 01:12:21,519
error now i'm going to say teacher
1212
01:12:17,840 --> 01:12:23,920
like this and let's call it t and well
1213
01:12:21,520 --> 01:12:24,880
if i do this only we should get an error
1214
01:12:24,880 --> 01:12:28,079
default constructor of teacher cannot be
1215
01:12:28,079 --> 01:12:31,760
which means that we need to implement a
1216
01:12:29,840 --> 01:12:34,719
constructor for this class here
1217
01:12:31,760 --> 01:12:36,960
as we did for this class here so as we
1218
01:12:34,719 --> 01:12:37,760
did for our developer class so let's do
1219
01:12:37,760 --> 01:12:40,960
i am going to create a constructor
1220
01:12:44,158 --> 01:12:48,559
like this and then this teacher
1221
01:12:48,560 --> 01:12:53,039
will receive these three properties that
1222
01:12:53,039 --> 01:12:56,640
like this and then it will receive
1223
01:13:04,000 --> 01:13:07,679
this constructor is going to pass these
1224
01:13:07,679 --> 01:13:11,520
to constructor of base class so this one
1225
01:13:14,800 --> 01:13:23,119
let's say here employee and then
1226
01:13:18,800 --> 01:13:27,039
inside i will pass name and company
1227
01:13:23,119 --> 01:13:29,279
and age and then here this teacher class
1228
01:13:27,039 --> 01:13:32,000
will take care of constructing
1229
01:13:29,279 --> 01:13:32,639
this part so i'm going to say that my
1230
01:13:32,640 --> 01:13:37,840
is equal to this subject that we
1231
01:13:37,840 --> 01:13:45,039
okay and now if i return down here
1232
01:13:45,520 --> 01:13:51,760
let's name the teacher jack for example
1233
01:13:48,800 --> 01:13:52,480
and he is going to work in a school
1234
01:13:52,479 --> 01:13:59,599
cool school if such thing exists
1235
01:13:56,479 --> 01:14:00,079
okay and then he's going to be 35 years
1236
01:14:00,079 --> 01:14:06,238
and he is going to be history teacher
1237
01:14:06,238 --> 01:14:09,359
now we have successfully created our
1238
01:14:09,359 --> 01:14:15,279
jack and i am going to collapse this
1239
01:14:12,560 --> 01:14:17,120
and then i want to test this method here
1240
01:14:17,119 --> 01:14:25,119
teacher t dot and then prepare lesson
1241
01:14:21,520 --> 01:14:28,560
okay and if i run this program
1242
01:14:25,119 --> 01:14:31,679
it says that jack is preparing history
1243
01:14:28,560 --> 01:14:33,360
lesson okay and then one more problem
1244
01:14:31,679 --> 01:14:34,239
that we have with this class here is
1245
01:14:34,238 --> 01:14:39,439
teacher t does not have access to
1246
01:14:36,960 --> 01:14:42,800
properties of our employee class so
1247
01:14:39,439 --> 01:14:45,839
if i type t dot as you can see it has
1248
01:14:42,800 --> 01:14:47,920
access only to these two properties so
1249
01:14:45,840 --> 01:14:48,719
in order to fix that problem i am going
1250
01:14:52,000 --> 01:14:57,840
i try to make jack ask for promotion
1251
01:14:55,359 --> 01:14:58,639
let's see if jack gets the promotion
1252
01:14:58,640 --> 01:15:04,960
i didn't get promotion okay it says jack
1253
01:15:01,840 --> 01:15:06,239
got promoted excellent and i didn't and
1254
01:15:06,238 --> 01:15:11,759
35 years old and i am 25 years old
1255
01:15:09,520 --> 01:15:13,840
okay so if you don't know what i'm
1256
01:15:11,760 --> 01:15:16,960
talking about you will have to return
1257
01:15:13,840 --> 01:15:20,719
to the part where we are talking about
1258
01:15:16,960 --> 01:15:23,520
um encapsulation i believe okay
1259
01:15:20,719 --> 01:15:24,079
so that was the story of inheritance we
1260
01:15:24,079 --> 01:15:28,640
two derived classes one is developer and
1261
01:15:28,640 --> 01:15:32,239
and those two classes are inheriting
1262
01:15:32,238 --> 01:15:38,639
employee as you can see here and here
1263
01:15:35,679 --> 01:15:39,359
and we will be using these classes in
1264
01:15:39,359 --> 01:15:42,479
example to explain our next concept
1265
01:15:42,479 --> 01:15:48,000
polymorphism as i said fourth principle
1266
01:15:45,600 --> 01:15:50,800
of object-oriented programming is called
1267
01:15:48,000 --> 01:15:52,000
polymorphism and this is the one that i
1268
01:15:52,000 --> 01:15:55,198
most for some reason i don't know why
1269
01:15:55,198 --> 01:15:59,678
simple and cool as you will see so it is
1270
01:15:59,679 --> 01:16:04,800
only if you understand the things that i
1271
01:16:01,600 --> 01:16:06,400
have explained so far in this course
1272
01:16:04,800 --> 01:16:07,840
so the first thing that i want to
1273
01:16:07,840 --> 01:16:12,880
polymorphism uh the word itself comes
1274
01:16:10,719 --> 01:16:13,920
from greek language and it is a compound
1275
01:16:13,920 --> 01:16:18,880
and morph which means many forms
1276
01:16:17,039 --> 01:16:20,479
and in programming polymorphism
1277
01:16:20,479 --> 01:16:26,479
of an object or a method to have many
1278
01:16:24,319 --> 01:16:28,158
forms now the most common use of
1279
01:16:28,158 --> 01:16:31,599
is when a parent class reference is used
1280
01:16:31,600 --> 01:16:36,640
object of a child class does that sound
1281
01:16:34,800 --> 01:16:37,119
a little bit complex when i say it like
1282
01:16:37,119 --> 01:16:41,279
i believe that it does and i agree but
1283
01:16:39,840 --> 01:16:43,440
stick with me for a couple of minutes
1284
01:16:41,279 --> 01:16:44,319
and let me show you how simple this
1285
01:16:44,319 --> 01:16:49,519
on the example that we have here so in
1286
01:16:46,719 --> 01:16:52,560
order to explain how polymorphism works
1287
01:16:49,520 --> 01:16:55,840
let's return to our employee class so
1288
01:16:52,560 --> 01:16:57,520
this one here i'm going to collapse this
1289
01:16:55,840 --> 01:16:59,679
and then what i want to do in my
1290
01:16:57,520 --> 01:17:00,239
employee class is i want to implement
1291
01:17:00,238 --> 01:17:04,799
method and let's call that method work
1292
01:17:08,800 --> 01:17:12,239
and then here in this method what i want
1293
01:17:12,238 --> 01:17:19,039
is i want to say for example
1294
01:17:19,039 --> 01:17:23,039
employee and then let's say that that
1295
01:17:27,359 --> 01:17:32,158
and then he is checking also task
1296
01:17:36,800 --> 01:17:44,800
task backlog and then he is performing
1297
01:17:44,800 --> 01:17:53,679
and so on okay and let's add end line
1298
01:17:53,679 --> 01:17:58,560
let's return to this main function here
1299
01:17:56,719 --> 01:17:59,198
and what i want to do is i want to
1300
01:17:59,198 --> 01:18:02,960
that new work method that i just
1301
01:18:02,960 --> 01:18:06,560
and i want to invoke that method on this
1302
01:18:06,560 --> 01:18:10,640
teacher class because considering the
1303
01:18:08,640 --> 01:18:12,000
developer and teacher are inheriting
1304
01:18:12,000 --> 01:18:18,560
we should be able to do that so if i say
1305
01:18:21,920 --> 01:18:26,880
let's see what will we get if i run this
1306
01:18:26,880 --> 01:18:30,159
okay it says here selena is checking
1307
01:18:30,158 --> 01:18:35,198
task backlog performing tasks and so on
1308
01:18:32,640 --> 01:18:39,760
and then jack is also checking email
1309
01:18:35,198 --> 01:18:41,919
task backlog and performing those tasks
1310
01:18:39,760 --> 01:18:43,199
but one thing that i don't like is i
1311
01:18:43,198 --> 01:18:46,319
emails and checking task backlog and so
1312
01:18:46,319 --> 01:18:52,079
what i like is i like to write code so
1313
01:18:49,439 --> 01:18:54,319
how about implementing this work method
1314
01:18:54,319 --> 01:18:57,439
so let's return here to my developer
1315
01:18:57,439 --> 01:19:01,039
and what i want to do here is i want to
1316
01:19:04,079 --> 01:19:08,000
instead of checking email and checking
1317
01:19:08,000 --> 01:19:11,279
on what i want to do here is i want to
1318
01:19:11,279 --> 01:19:18,238
that i am writing code so let's say
1319
01:19:18,238 --> 01:19:23,678
writing and then let's put this
1320
01:19:23,679 --> 01:19:31,440
like this and then let's say
1321
01:19:27,198 --> 01:19:32,399
code okay and now i'm going to do the
1322
01:19:32,399 --> 01:19:36,319
teacher class here so let's copy this
1323
01:19:36,319 --> 01:19:42,000
and then paste it in my teacher class
1324
01:19:39,439 --> 01:19:44,000
but instead of writing code teacher is
1325
01:19:42,000 --> 01:19:47,198
going to be doing something else
1326
01:19:44,000 --> 01:19:47,760
so what teacher does well teacher
1327
01:19:47,760 --> 01:19:50,960
so i'm going to say the name of my
1328
01:19:50,960 --> 01:19:58,239
jack in this situation so jack
1329
01:19:58,238 --> 01:20:02,158
and then let's put the name of his
1330
01:20:02,158 --> 01:20:07,679
which is history i believe okay
1331
01:20:05,439 --> 01:20:08,719
now one thing that you must notice is
1332
01:20:08,719 --> 01:20:14,319
work method has the same name here
1333
01:20:11,840 --> 01:20:15,520
and then here as well and it also has
1334
01:20:15,520 --> 01:20:21,520
in this base class so it is called work
1335
01:20:18,639 --> 01:20:22,719
but the implementations of this method
1336
01:20:22,719 --> 01:20:27,039
are different here it says that an
1337
01:20:27,039 --> 01:20:30,158
email and checking task backlog and so
1338
01:20:30,158 --> 01:20:35,119
and then developer has also its own
1339
01:20:32,880 --> 01:20:35,760
implementation which says that developer
1340
01:20:35,760 --> 01:20:40,639
writing code because that is his work
1341
01:20:38,399 --> 01:20:43,920
and then teacher is teaching his
1342
01:20:43,920 --> 01:20:47,440
i have here invoked these two work
1343
01:20:47,439 --> 01:20:51,919
and let's see what will happen if i run
1344
01:20:51,920 --> 01:20:56,239
okay now it is better it says that
1345
01:20:56,238 --> 01:21:02,238
plus code and jack is teaching history
1346
01:21:00,158 --> 01:21:04,079
okay now let's make one small
1347
01:21:02,238 --> 01:21:04,638
improvement to the code that we have
1348
01:21:04,639 --> 01:21:08,639
or actually two improvements and the
1349
01:21:08,639 --> 01:21:13,359
writing with double t okay what is this
1350
01:21:13,359 --> 01:21:17,519
and then the second one is going to be
1351
01:21:15,760 --> 01:21:18,079
the rule that i mentioned in the
1352
01:21:18,079 --> 01:21:21,439
when i said the most common use of
1353
01:21:21,439 --> 01:21:25,919
so let me copy that definition here and
1354
01:21:25,920 --> 01:21:32,158
literally word by word so here it is
1355
01:21:29,279 --> 01:21:33,439
and it says the most common use of
1356
01:21:33,439 --> 01:21:36,799
when a parent class reference is used to
1357
01:21:36,800 --> 01:21:43,119
to a child class object so let's see
1358
01:21:40,238 --> 01:21:43,678
what this means in practice i'm going to
1359
01:21:43,679 --> 01:21:46,880
these two lines because i don't need
1360
01:21:46,880 --> 01:21:50,800
and then what i want to do is i want to
1361
01:21:54,319 --> 01:21:59,279
like this and one quick disclaimer if
1362
01:21:57,198 --> 01:22:01,198
you are not familiar with pointers
1363
01:21:59,279 --> 01:22:03,519
i am going to refer you to my channel
1364
01:22:01,198 --> 01:22:05,759
which is code beauty with where i have
1365
01:22:03,520 --> 01:22:07,679
an entire playlist which is dedicated to
1366
01:22:05,760 --> 01:22:09,039
pointers so make sure to watch that and
1367
01:22:09,039 --> 01:22:15,840
with this example here so again
1368
01:22:12,238 --> 01:22:17,439
i have created an employee pointer
1369
01:22:15,840 --> 01:22:18,960
and what i want to assign to this
1370
01:22:18,960 --> 01:22:22,639
employee object so i want to assign to
1371
01:22:22,639 --> 01:22:27,039
pointer here a reference of this
1372
01:22:27,039 --> 01:22:31,279
teacher class so here i'm going to say
1373
01:22:31,279 --> 01:22:37,439
a reference to this developer here
1374
01:22:34,639 --> 01:22:38,800
and this here is the rule so a pointer
1375
01:22:41,920 --> 01:22:45,359
reference to derive class object which
1376
01:22:45,359 --> 01:22:48,719
this part here so so that is just the
1377
01:22:48,719 --> 01:22:55,279
why well because this developer here
1378
01:22:52,079 --> 01:22:57,679
this developer is deep down inside it is
1379
01:22:55,279 --> 01:22:59,039
an employee because developer is
1380
01:22:59,039 --> 01:23:02,880
employee as we already could see in the
1381
01:23:02,880 --> 01:23:08,400
course so now that i have done this
1382
01:23:05,840 --> 01:23:10,719
let's do the same for our teacher so
1383
01:23:08,399 --> 01:23:12,559
i'm going to copy this line and then
1384
01:23:12,560 --> 01:23:15,760
employee 1 and then this is going to be
1385
01:23:15,760 --> 01:23:18,960
and here i will say that employee 2 will
1386
01:23:18,960 --> 01:23:25,359
the reference of my teacher here
1387
01:23:22,639 --> 01:23:26,560
so now what i want to do and what i want
1388
01:23:26,560 --> 01:23:30,800
is the following wouldn't it be nice if
1389
01:23:29,198 --> 01:23:34,319
i could do something like this
1390
01:23:41,039 --> 01:23:44,079
symbol here is used when you want to
1391
01:23:44,079 --> 01:23:49,439
members using a pointer so instead of
1392
01:23:47,439 --> 01:23:51,359
dot symbol when you use a pointer you
1393
01:23:51,359 --> 01:23:54,719
okay so wouldn't it be nice if this here
1394
01:23:54,719 --> 01:23:58,880
well let's see maybe it does if i run my
1395
01:23:58,880 --> 01:24:02,000
as you can see it again says alina is
1396
01:24:02,000 --> 01:24:06,079
and task backlog and performing tasks
1397
01:24:04,000 --> 01:24:08,000
and so on and jack is doing the same and
1398
01:24:06,079 --> 01:24:10,479
he's a teacher and i'm developer
1399
01:24:08,000 --> 01:24:11,439
so that means that this here does not
1400
01:24:11,439 --> 01:24:16,879
but if you only knew how close we are to
1401
01:24:16,880 --> 01:24:21,760
you are literally missing only one tiny
1402
01:24:19,359 --> 01:24:24,960
little thing that i didn't tell you
1403
01:24:24,960 --> 01:24:29,039
what that thing is well let's return to
1404
01:24:32,158 --> 01:24:36,158
this work method let's make this work
1405
01:24:36,158 --> 01:24:42,799
virtual so i'm going to say virtual
1406
01:24:39,679 --> 01:24:45,679
here so let's run our program now
1407
01:24:42,800 --> 01:24:46,480
and as you can see it says saudina is
1408
01:24:46,479 --> 01:24:49,519
plus code and jack is teaching history
1409
01:24:49,520 --> 01:24:57,600
it works wait i have to explain this
1410
01:24:54,319 --> 01:24:58,079
okay so let's return to our employee
1411
01:24:58,079 --> 01:25:03,519
to explain what just happened well
1412
01:25:01,198 --> 01:25:04,719
look at this function here this virtual
1413
01:25:04,719 --> 01:25:09,520
this function with this virtual keyword
1414
01:25:09,520 --> 01:25:13,199
function and what that means well it
1415
01:25:13,198 --> 01:25:16,238
when a virtual function is invoked it
1416
01:25:16,238 --> 01:25:20,959
hey can you please check if there is
1417
01:25:18,880 --> 01:25:24,319
implementation of this function
1418
01:25:20,960 --> 01:25:27,039
in my derived classes and if yes please
1419
01:25:24,319 --> 01:25:27,599
execute that instead so what ends up
1420
01:25:27,600 --> 01:25:32,079
is that the most derived version of this
1421
01:25:32,079 --> 01:25:37,279
executed and that is this function here
1422
01:25:37,279 --> 01:25:43,119
this function here for teacher
1423
01:25:40,479 --> 01:25:44,000
so does that mean that if we didn't for
1424
01:25:44,000 --> 01:25:48,000
implementation of function called work
1425
01:25:48,000 --> 01:25:52,800
that our developer then would do this
1426
01:25:52,800 --> 01:25:56,639
work function the answer is yes so let
1427
01:25:56,639 --> 01:26:01,279
just to demonstrate how it would behave
1428
01:25:59,039 --> 01:26:02,719
so now i commented implementation of
1429
01:26:02,719 --> 01:26:06,319
developer class so if i run the program
1430
01:26:06,319 --> 01:26:09,599
as you can see it says salina is
1431
01:26:09,600 --> 01:26:13,120
email and task backlog and performing
1432
01:26:13,119 --> 01:26:16,399
and then jack is teaching and this is
1433
01:26:16,399 --> 01:26:19,519
teacher class has its own implementation
1434
01:26:19,520 --> 01:26:23,440
work method work function but developer
1435
01:26:23,439 --> 01:26:27,279
does not so here developer is going to
1436
01:26:27,279 --> 01:26:33,359
parents class method work which is this
1437
01:26:30,639 --> 01:26:35,520
method here so that's why i am checking
1438
01:26:33,359 --> 01:26:36,000
email and task backlog and performing
1439
01:26:36,000 --> 01:26:42,079
tasks instead of writing c plus
1440
01:26:39,279 --> 01:26:43,039
code so let's close this and let's now
1441
01:26:43,039 --> 01:26:49,439
i'm going to uncomment it like this
1442
01:26:46,158 --> 01:26:50,238
and let's run my program once more and
1443
01:26:50,238 --> 01:26:54,238
everything works again and we have this
1444
01:26:54,238 --> 01:26:59,599
so that ladies and gentlemen is
1445
01:27:00,319 --> 01:27:04,559
so let me give you one little tip of
1446
01:27:02,639 --> 01:27:06,159
what we can do with this knowledge now
1447
01:27:06,158 --> 01:27:09,439
and i'm going to return to my main
1448
01:27:09,439 --> 01:27:15,759
here as you can see here we are creating
1449
01:27:12,800 --> 01:27:17,279
a base class pointer to a derived class
1450
01:27:15,760 --> 01:27:18,480
object which is in the situation
1451
01:27:18,479 --> 01:27:22,559
in this situation it is teacher and then
1452
01:27:20,719 --> 01:27:24,880
in some other situation it can be
1453
01:27:22,560 --> 01:27:27,120
some other type of employee so if we
1454
01:27:24,880 --> 01:27:29,520
created more derived classes which we
1455
01:27:27,119 --> 01:27:33,198
could do we could create for example
1456
01:27:29,520 --> 01:27:34,320
derived class called bus driver or pilot
1457
01:27:34,319 --> 01:27:40,399
actor doctor and so on and then
1458
01:27:37,840 --> 01:27:41,520
we could keep objects of those derived
1459
01:27:41,520 --> 01:27:44,880
so we could reference them with this
1460
01:27:44,880 --> 01:27:48,639
pointer because they would derive from
1461
01:27:48,639 --> 01:27:52,480
and then imagine situation where you had
1462
01:27:52,479 --> 01:27:58,319
thousand or even 10 000 employees
1463
01:27:55,760 --> 01:28:00,079
and then you are storing those like this
1464
01:27:58,319 --> 01:28:02,559
like we did with our developer and
1465
01:28:00,079 --> 01:28:03,519
our teacher and then you could literally
1466
01:28:03,520 --> 01:28:07,760
say employee dot work and what would
1467
01:28:07,760 --> 01:28:11,520
all of those different types of
1468
01:28:11,520 --> 01:28:16,159
doing their own job so painters would
1469
01:28:16,158 --> 01:28:20,238
actors would act doctors would talk with
1470
01:28:20,238 --> 01:28:23,678
i don't know so that is the most
1471
01:28:22,238 --> 01:28:27,119
beautiful thing that we have
1472
01:28:23,679 --> 01:28:28,399
achieved with this here so i hope that
1473
01:28:28,399 --> 01:28:32,719
if you did give it a thumbs up and then
1474
01:28:30,639 --> 01:28:33,279
also if you want more videos like this
1475
01:28:33,279 --> 01:28:37,759
you can visit my channel which is code
1476
01:28:35,520 --> 01:28:40,880
beauty and there i have a lot of
1477
01:28:37,760 --> 01:28:42,960
videos related to programming and c plus
1478
01:28:40,880 --> 01:28:44,400
and also leave me a comment i would love
1479
01:28:44,399 --> 01:28:49,039
i am coming from precode camp or
1480
01:28:46,000 --> 01:28:51,439
something like that on one of my videos
1481
01:28:49,039 --> 01:28:53,039
so thank you very much for watching and
1482
01:28:51,439 --> 01:28:54,479
i'm going to see you in some next video
1483
01:28:54,479 --> 01:28:58,638
if you decide to visit code beauty
1484
01:28:56,719 --> 01:28:59,439
youtube channel i will definitely see
1485
01:29:02,479 --> 01:29:06,158
so now i'm going to take a break for a
1486
01:29:04,158 --> 01:29:08,238
couple of hours but for you it is going
1487
01:29:06,158 --> 01:29:10,319
to be just a couple of seconds because
1488
01:29:08,238 --> 01:29:12,399
i'm going to pause the video here
1489
01:29:10,319 --> 01:29:13,920
and for this i want to try a transition
1490
01:29:24,000 --> 01:29:28,639
so what is polymorphism the word
1491
01:29:26,399 --> 01:29:39,839
polymorphism comes from latin language
1492
01:29:28,639 --> 01:29:39,840
and it means or is it greek
1493
01:29:51,279 --> 01:29:57,279
i'm just going to skip this information
1494
01:29:54,399 --> 01:29:58,238
so if i hover over this as you can see
1495
01:29:58,238 --> 01:30:03,599
error but we have a different error
1496
01:30:00,639 --> 01:30:06,159
which means that we are making progress
1497
01:30:03,600 --> 01:30:08,320
also visit me on my channel which is
1498
01:30:06,158 --> 01:30:10,079
code beauty and subscribe there
1499
01:30:08,319 --> 01:30:12,238
and also leave me a comment i would love
1500
01:30:10,079 --> 01:30:13,760
to read something like hey selena i am
1501
01:30:13,760 --> 01:30:18,960
and your course was great or hey salina
1502
01:30:17,279 --> 01:30:19,759
i am coming from free code camp and your
1503
01:30:19,760 --> 01:30:23,520
excellent or perfect or outstanding or
109613
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.