All language subtitles for Object-Oriented-Programming-OOP-in-C-Course_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

hi everyone and welcome my name is

and i make programming related videos on

and in this video i want to talk about

object oriented programming this course

will start on a beginner basic level

and then as the course progresses we

advanced concepts i will explain those

advanced concepts and i will show you

all of those concepts in code you can

find the full content of this course

in the description of this video so make

and then if you want to see some

different examples if you want to learn

more c plus and you want to upgrade your

i have a lot more videos like this one

on my channel so i invite you to visit

and i'm going to see you there in some

with this video so the first thing that

oop is programming paradigm which means

it is a set of rules and ideas and

concepts it is basically a standard in

that we use to solve a specific type of

now besides object-oriented paradigm

programming paradigms as well you may

and our computers help us solve those

and if one paradigm is good to solve one

that does not necessarily mean that it

all other types of problems that we have

the idea behind object-oriented paradigm

we want to be able to represent

objects real-life entities together with

attributes and their behaviors we want

those objects to our computer and to

in our program and that is exactly what

is used for so how does this work on a

real life example let's use the example

so a car as an entity in real life has

characteristics and some of those are

is that car a volvo a ferrari or

a ford or some other type of car and

a collar it has price it has max speed

and many more characteristics i'm sure

many more characteristics of a car than

so those would be some of the attributes

a car in real life and then some of the

are for example a car can drive a car

uh stop you can open the door of a car

you can tank your car so you can put

fuel in your car and then you should

show how much fuel that car has left so

behaviors that car has and as i said

object oriented programming the idea of

is to be able to represent that car from

in your program so how do we do that how

a real life entity like a car in my

another entity like a game or a sport or

student or a book or animal you know

so the answer to this question is by

classes now class is very important

class is building block of object

and um a class is basically user

data type and in order to be able to

understand what are user-defined data

you first need to understand what are

pre-defined data types what are built-in

data types like integer and float and

and so on and if you are not familiar

with these i am going to refer you to my

that i made on my channel related to

data types so just search code beauty

data types and you should be able to

and then if you are familiar with these

predefined data types i'm going to tell

is a little bit more complex data type

and in what way well let's say for

example that in your program you want to

username of your user in order to be

you will just create a variable of type

username and you should be able to store

of your user in that variable but

what happens if you want to store the

in your program that user has many more

characteristics than just username user

his name as well and then gender and

height and width and um i'm sorry wait

he has weight and then he also has

some other characteristics and all of

these characteristics are going to be

variables that have its own type and

all of these variables together are

which will be a class that represents

one thing to keep in mind though is that

you will need to store the information

that is important for your specific

program for example if we are talking

then height and weight of that user is

but if you are building a fitness

height and weight of your user is

extremely important so you should store

the information that is important for

specific program so let me show you now

a class in c plus code and the ide that

here is called visual studio and you can

download community version of visual

in case that you want to follow this

tutorial and type at the same time

together with me so let me show you now

in c code the class that i want to

is called employee so let's do that now

employee class in this program here so

thing is you type class keyword

and then you give a name to your class

and then you use these curly brackets

curly brackets you need to put semicolon

because if you don't if you forget this

semicolon you will get a compile time

and if i try to run this program now

that is not going to be possible as you

expected a semi column so you need to

here okay now that we have created

this employee class we see that this

and what we need to put inside this

class here so inside these curly

because that is the body of our class we

members of this class now the members of

its attributes and its behaviors so what

an employee has well an employee

definitely has a name so let's put

that in so i'm going to say std

okay and then employee definitely has a

company that he works for so let's add

company like this okay and i want to

uh string i'm going to include it here

std string so that i don't need to type

it every time that i'm using string

okay and now i can remove this now it

readable okay so so far we have added a

and a company for our employee and let's

add one more thing let's say that every

age so inch age we are going to

we are going to represent age as

an integer number so as you can see at

of each of these attributes you put

as well okay so far our employee has

name company and age and you can add

many more attributes here if you want

his title you can add his email you can

add employment date his birth date

uh and so many more but i'm not going to

situation because i don't want to add a

bunch of code that we will not be using

because i'm going to add that code as we

so this class here does not represent

data this class here represents a

so whenever you want to create an

employee this class here will serve

as a model for that employee so this

hey your employee needs to have a name a

and age so how do you create an instance

of this class how do you create an

let's show that now so how do you create

a variable of type int for example well

and then you give the name to that

number okay and with this we have

successfully created a variable of type

int and it is going to be the same with

employee so you first specify the name

and the name is going to be employee so

okay and then how do i want to name this

and with this we have successfully

of type employee this here is going to

of this class here so let's remove this

because we don't need it anymore

it was just for demonstration purposes

how do you access now these attributes

of employee class well the approach

you type the name of that object

like this and then if i put dot here

we should be able to see all of these

members here but that is not happening

why is that the case well there is one

everything inside class in c plus plus

is private by default which means that

all of these members here are private

and in order to be able to understand

this and fix the problem that we have i

story about access modifiers so in c

plus plus we have three access modifiers

private we have public and then we have

and let's explain now what each of these

private means that whatever is private

that is not going to be accessible

outside of your class that is basically

hidden and that public means that

whatever is public in your class

you will be able to access that outside

of your class as well so anyone else

outside of your class is going to be

public members of your class and then

is somewhere in between private and

certain rules to it so we will be

protected access modifier when we start

later in this course so for now you need

to remember that there are three of them

there is private there is public and

protected so in this particular

inside your class is private by default

so this situation here is the same

okay this is just explicit way to say

hey everything beneath this private

access modifier is going to be

private so this situation is the same

as this situation now let's return our

private access modifier and let's see

how we can fix the problem that we have

and the problem is that we cannot access

these members of our employee class and

let's try to change this private access

access modifier let's try protected for

these members name company and age now

as you can see nothing is happening so i

cannot access these name company names

if i try to type for example name

i get an error which says member

protected is not helping as well

so let's try the third one let's try

okay and now as you can see the error

to do this once more we have all of

members name company name and age we

here they are offered to us so that

by changing this access modifier to

able to see all of these properties here

let's now set the values of these

let's say that the name of our employee

and then let's copy this two more times

like this so the company will be

let's say for example youtube and then

code beauty which is my channel let's

say that that is the company that i work

okay age is an integer variable so we

are going to set it as an integer

so with this here we have created an

of type of type employee that object is

employee 1 and here we have set

values for the properties we have set

company and age of this employee object

but we also said that we can describe

so how can we describe a behavior of an

let's first think of a behavior that an

let's say for example that he can

so he comes to work and he says hello my

so and so i work for this company and i

25 years old so how can we describe

that behavior in this class here well

we can describe that with a class method

it is basically a function so we are

inside this class employee so let's do

i'm going to create a function of return

void and i'm going to call that function

like this okay and then inside this

what i want to do is you can write hello

my name is and then you put this name

this company and i am 20 years old for

but i'm going to make it a little bit

more formal so i'm going to say the

c out and then i'm going to write out

like this okay and then let's put

i'm going to add endline and then i'm

these two informations as well so

paste here company and then age as well

okay now how can i invoke this

introduce yourself function well the

i'm going to say employee and then i put

and as you can see this introduce

is available here so let's invoke that

like this and now we should be able to

as you can see here it says name salina

company youtube code beauty and then age

25 okay great and now if we needed to

introduce our employee five times for

we just copy this function five times

like this and our user is going to

and if we didn't have this class method

we would have to copy this code five

times so each time that we want to

our user we would have to copy this code

instead of doing that we can create a

class method which represents a behavior

and then we can invoke that class method

whenever we need it and if i run

this program this user should introduce

and then oh sorry four and then five

this because i don't need it anymore

because i want to show you something

so let's say that we want to create

another employee in our program so how

that well we would use the same approach

so employee the name of my class and

employee let's call it employee 2 which

the values for these attributes for our

second employee as well i'm going to say

and his name is going to be john for

like this let's copy this two more times

amazon for example john works for amazon

so now if i want to introduce john i'm

employee 2 dot introduce yourself

excellent so if i run this program now

as you can see here is me salina youtube

code beauty and then here is john

now one problem that i already see here

this part of the code and then this part

what would happen if we wanted to create

100 more users we would have to repeat

this for every single user that we

optimal because there is a better way

and in order for you to be able to

understand this i will have to tell you

constructors so now you may wonder

salvino what is a constructor well a

is a special type of method that is

each time that an object of a class

is created so whenever you create an

of a class a constructor is invoked

so does that mean that here and here as

a constructor is invoked the answer to

yes now you may wonder okay we have not

created any constructor you must be

well let me demonstrate you something

and now let's comment this code here as

let's see what will happen if i run my

so we are not assigning any values to

attributes of this employee nor this

if i run my program okay here is what

as you can see this is our first user

and this here is our second user

so here we don't have anything and then

here we here we also don't have anything

and then here is a number which

i am not going to try to read okay but

this here is the work of default

what is default constructor default

to describe a constructor that is

automatically generated by your compiler

in case that you don't create a

your compiler is going to automatically

constructor which is called default

let's see how we can create our own

constructor because those values that

that is something that we cannot work

with i mean we don't want to work with

those values we want to use our own

we want to use this and this and then

this here and for our second user we

these values here so how can you create

there are a few rules when it comes to

there are three rules actually so the

following we already said that a

is just a method but unlike other

a constructor does not have a return

as you can see this method here

introduce yourself this method has a

void which is nothing but we still have

but a constructor will not have this at

so that's going to be the first rule the

second rule is that a constructor has

as the class that it belongs to so the

employee will be called employee so that

the second rule the third rule is that

public now this i'm seeing as a rule at

this level of knowledge because

a constructor does not necessarily need

to be public always there are

certain situations specific situations

when you would want to make your

private but at this level of knowledge i

would advise you to make sure that your

are public because if you remember when

we talked about access modifiers and

when we said what private means

that means that everything that is

inside your class and you most

definitely don't want to make your

constructor hidden so uh third

three rules uh the first one is that

return type the second one is that

constructor has the same name

as the class so let's create constructor

it will be called employee and i am

that constructor in this public area

because if i decided for some reason to

if i decided to make this private let's

let's make this private like this as you

immediately here we get an error so our

employees too this error says

employee constructor is inaccessible

and here as well because we have made

this constructor private and we cannot

access it outside of this class here and

we want our constructor to be public so

i'm going to remove this private access

modifier and as you can see this error

disappeared now so the job of this

constructor here is going to be to

the object of employee that means that

employee we want to pass these three

name company and age we want to pass

here so we are going to receive that in

employee constructor as parameters so

okay now that we have received these

three parameters here what i want to do

with them is i want to assign them

to these three properties here so i'm

name of my employee will be this

company that our employee works in it

will be whatever we passed as this

here okay and now if you look at our

employee here we have an error again

so let's check what their what that

no default constructor exists for class

employee so what this error here means

it means that when we decided to create

constructor at that moment we lost

the default constructor that was

for us so when you decide to create your

you are going to lose that default

constructor and you can fix that by

making your own default constructor or

providing here these three values that

we have specified that our constructor

so let's do that let's provide here name

company name and age so how do we do

let me show you how we can use this

so in this line i'm going to say

that my employee 1 is going to be equal

and i'm going to invoke this constructor

hey i am invoking this constructor here

and here it expects to receive argument

it is these three parameters so name

company and age so here i'm going to

and then company is going to be this

company here so youtube code beauty i'm

and then age is going to be 25

okay so with this we have successfully

object which means that i can delete

and then if we do this same for our

employee tool we will be able to delete

this code here as well so let's do that

employee and then here i want to pass

these three values so name is john

okay and then company is amazon

excellent so i am able to delete

check what is going to happen if i run

my program i'm just going to format

this so if i run my program nothing

i should still be able to introduce this

user and then this user as well so let's

do that let's run our program

okay and as you can see selena youtube

so now we have managed to reduce the

code that we had in our main function

to only two lines per employee so

the first line is to construct that

second line is to introduce that user

and we have managed to do that

with our employee constructor which is

now doing that work of constructing the

object based on based on the parameters

constructor that constructor receives

those parameters here and then

it constructs the object here it

initializes the values of that

object and then in our introduce

invoke that function for an object of an

that function is going to do this code

here so it is going to basically

employee so i hope that this part so far

was understandable for you because

here i'm going to take a quick break and

i'm going to pause the video and then i

might be back in a couple of minutes or

in a couple of hours or even in a couple

of days but that is going to be

just a couple of seconds for you and i

want to make a transition that i never

on my channel i never did this on code

beauty but i'm going to do it here

and that is this transition here

hello and welcome back and let's

object oriented programming but before i

continue i want to make a quick

summary of the things that we have

so first i have explained what is

object-oriented programming and what is

object-oriented paradigm and then i have

explained what our classes and what our

objects and how you can use those

in order to represent real life entities

together with their attributes and their

in your programs describe them to your

and then we also talked about access

private public and protected and then we

have seen what our constructors and how

and use constructors and now the time

four most important principles four

pillars for most important concepts of

and those are encapsulation abstraction

inheritance and polymorphism so the

first one that i want to talk about

the idea of encapsulation is idea of

bundling or tying together data and

data so that they are grouped together

and why do we do this well we do this

with the purpose of preventing

anyone or anything outside of our class

to be able to directly access our data

and to interact with it and to modify it

so i am not saying that we don't want

to access our data at all i'm just

saying that i don't want that to happen

directly i don't want that other class

modify and change and interact with my

because i want to provide my own way for

to happen so how do i do that how do i

a way for other classes to interact with

well i provide very specific public

that that other class can invoke and in

that way interact with my data

so again how do you access encapsulated

properties of a class the answer is

its methods and these methods are very

often implemented as getters and setters

now i'm going to show you in code how

look like and how we can modify this

so that we obey that rule of

so the first thing that i want to do

i want to make these three properties i

private so i want to encapsulate these

hide them within this class so let's do

here i'm going to put private modifier

so this here is private access modifier

and we don't actually have to write this

everything that does not have a modifier

c plus plus is private by default within

but i just find it more readable if i do

here in this private area i am going to

properties okay and then paste them here

like this and now we have hidden these

within this class called employee which

means that now we will not be able

to access these three properties anymore

let me just collapse this and let me

name or company or age on my employee

i should not be able to do that now so

and then i type this dot you can see

that the only thing that we can access

is this introduce your introduce

method which is this one here and that

is the only thing that is public

besides this constructor here so

how can we access these properties then

well as i said we have to expose our own

are going to be public and then by using

other classes and other users will be

interact with this data here with these

so let's create getters and setters for

let's first do it for for our name

so i'm going to do it here uh let's say

and then what this method what this

well that is going to be a string

and then once this function receives

parameter called name what i want to do

with this is i want to assign this value

to this property here so i'm going to

and then i want to create so this here

is a setter and then i want to create a

i am going to say string get name

like this and then here i'm just going

this method here receives one parameter

name and then it sets the value of

our property which is encapsulated which

to that value that we have received in

setter and then this method here get

that method is going to return the value

of our name property that is also

encapsulated and that value is going to

invokes this method and because these

are public anyone outside of our class

set name and get name so these

are examples of setter and getter and

now i'm going to do the same for our

company property and age property and i

will be back to show you that code

in a moment so i have created getters

our name property here so this is sether

our name property and then here is a

setter for our company and then

getter for company property and then

setter and getter methods for age and

now because these methods here are

public everyone outside of our class

should be able to access these methods

whereas these three properties are now

encapsulated which means that they are

within this class and no one else

members of this class here are going to

be able to access them directly

so let's test now these getters and

okay so let's say that employee 1

and let's say set age and i'm going to

say for example that my employee 1 now

gutters i'm going to say for example std

get name okay so our getter should work

like this and then let's say employee

is and i'm going to test if this value

applied successfully so i'm going to say

okay and now if i run this program

we should get the result of this line

here and we should be able to see if our

getters and setters work as they should

okay and now this last line of code here

says salina is 39 years old okay so

i shouldn't have done this for myself i

should have done this for john for

so i'm going to close this program and

these get name and get age and also

get company and then set their methods

whereas the properties that they are

hiding are hidden within the class and

so the only way to access these three

properties is going to be using

these methods now what we can do with

these methods is we can provide

special rules to interact with this data

for example let's say that employee

older than 18 years old so here

when actually here when i'm trying to

set the age of our employee i'm going to

so i'm going to add a validation rule

this age that i have received in my

greater than 18 years old or actually

or equal to than 18 because person that

of age only in the case that this

property here is greater than 18 years

only then i am going to assign that to

property here in the case that our user

anyone else decides to assign value

which is less than 18 so 17 or 15 or 10

or whatever i am not going to apply

those changes so let's test this

age to 39 i'm going to say that salina

15 so let's test those changes

okay and now as you can see it says

why because this set age method here

age is 18 or greater than 18 and only in

those changes are going to be applied

validation rules to your setter methods

and then i'm going to leave to you to

apply certain validation rules

to your set company name and then set

employee name this property here

so you can do that as a homework you can

only letters or you want to allow also

and special characters as the name for

as the name for your company okay so

once more the idea of encapsulation is

so the idea of encapsulation is to make

properties private and then whoever

wants to access these properties

outside of this class will have to go

through the methods that you

expose that do have access to your

properties okay so let's collapse this

getters and setters for these three

the second principle that i want to talk

what is abstraction abstraction means

complex things behind a procedure that

simple so in order to explain this let's

everyday life example let's use the

so one of the main characteristics that

smartphones have these days is that they

pictures so how does that procedure of

well it's pretty simple at least on your

a button and you have taken a picture

and then you make a call or you send a

that is not really that simple because

a button click but for the company that

there is much more complex logic that

in order for you to be able to press a

take a picture so there is some sort of

interface between you and then that

company that produces those smartphones

on your side everything is pretty simple

and easy you just press a button and you

make a call you press a button and you

but then all of that complexity behind

is hidden from you and that complexity

is on the side of the company that

now all of this complexity that is

hidden from you you don't actually need

anything about this in order to be able

to use this site here in order to be

and send messages and take pictures

so what happens if another company comes

and they decide that they want to

what they have to do is they have to

they have to provide you as a user with

nice and beautiful and clean and simple

so that you are able to press a button

and then they have to take care on their

to implement all of that complex

logic which is hidden from you because

if they decide to show you that

ugly side that complex side chances are

that no one is going to want to use

and even if someone decides that they do

they will probably not understand this

complex logic well so that they will

so they will make mistakes and they will

ways so this process of hiding that

as a user is called abstraction and in

this very very complex system is hidden

and this system here is represented as

system by this contract so let's see how

this contract here that makes one side

look simple and then other side

very complex let's implement that on the

code and example that we have here in

so let's take a look at this employee

what kind of functionality complex

implement on this class here let's say

that every employee can ask for

so he goes to his boss and he says hey

and then his boss needs to go through

complex thought process in order to

this person deserves a promotion or not

so his boss needs to consider many

consider for example how long this

person has been working for the company

how much knowledge this person has

and then what kind of relationship this

person has with their colleagues how

much this person is contributing to the

um and then is this person always late

are they breaking deadlines and many

and in this situation we are doing that

for another developer that is going to

use our class so here in this situation

we are the ones who are making this

class so we are producing that

smartphone so we are the ones

who should provide that complex logic

functionalities that are complex and

simple and basic interface for anyone

this class here so how do you do that

that contract well the answer is by

abstract classes for those of you who

c sharp or java world there is already

interface that some of you might be

plus plus you can simulate behavior of

by using abstract class so let's create

so here on the top i am going to create

and let's create a class called abstract

like this or you can call it i

employee if you want so this class here

is going to serve as a contract

and this contract will have only one

rule and that rule is that whichever

decides to sign this contract that class

implementation for a method called ask

so a method of return type void

like this and we are also going to make

obligatory which means that we will

signs this contract here to implement

this method here so how can we make this

obligatory the answer is by making this

a pure virtual function so i'm going to

here i'm going to say is equal to zero

and now this class here has become an

and this method here this function is

virtual function or an abstract function

and this means that whoever decides to

called that abstractemployee.com

that signs this contract will have to

provide implementation for this

method here so now the question is how

can my class my employee class

sign this contract here the answer is

you add here column sign and then you

specify the name of your contract which

okay and now my class has successfully

this contract and immediately if you

these two red dots that appeared below

if we hover over these errors because

these are errors you can see that it

pure virtual function abstract employee

which means hey you signed this contract

but you are not obeying the rules of

providing implementation for this

so in order to avoid these errors and

fix these errors that we have now what

we have to do is we have to provide

implementation for this method here so

okay i am going to copy it here and then

and here in these curly brackets we are

implement that method but as you could

see immediately the errors that we had

below have disappeared so here

inside this class i want to provide

for ask for promotion method so once

more we said that that logic is pretty

complex and that we should take in

so things like how long that person has

how much that person is contributing to

the company what kind of relationship

with their colleagues and so on but

considering that we don't have all of

information what i'm going to do is i'm

going to make it a little bit more

so i'm going to say for example any

that is older than 30 years old can get

for example so i'm going to say here if

i will do the following i will say std

see out and then let's write the name of

this employee i'm going to say name

and i'm going to say got promoted

excellent and then in the else case

so in the case that person has less than

30 years i'm going to write something

deny that promotion to the person who is

so i'm going to say name and then

let's write out sorry no promotion for

now let's test this method here

so i'm going to go to my main function

will delete all of this code here

this method here on my employee 1 and

so that i see if any of them can get

employee two dot ask for promotion as

it says salinas sorry no promotion for

and why that happened because let me

because salina is 25 and then john is

35 and this rule here says that whoever

than 30 which is john he gets promoted

if that person is younger than 30 that

promoted okay so with this we have

we have implemented a contract which is

class and that abstract class or that

one rule and that rule is this pure

virtual function here which is called

ask for promotion so that means that

whichever class signs this contract

inherits from this abstract employee

this class here that class will have to

implementation for this method here so

promotion so in this situation our

inheriting from our abstract employees

is signing that contract which means

that that class needs to provide

implementation for the method that is in

contract okay now what this allows us to

when another developer comes to our code

and wants to use our employee class

that developer will see this contract

oh so this employee class has method

which is called ask for promotion

so that means that if i use employee

class i can invoke this method here

complexity of this method i don't need

about how this promotion is given or

because that is the worry of the person

this class here so that is the worry of

the person who is going to sign this

and in this situation we are that person

producing that smartphone we are the

person who is writing this class

and this class is signing the contract

which means that we have to provide

method here as we did in this situation

and we tested it and as you could see i

and john did so that is the idea of

and this ask for promotion method is

that button that we mentioned a button

from the beginning of this chapter the

third principle of object oriented

programming that i want to talk about

is called inheritance the idea of

so there is this base class also known

super class or parent class and then

derived class also known as child class

or subclass now this base class here

this parent class has certain

attributes and behaviors it has members

and then if this class here decides to

from the base class at that moment this

becomes a child class and by that this

all of the members of this base class

which means this class is going to have

attributes and behaviors as its base

and then this derived class can also

have its own members that are specific

which this class here does not have so

in order to explain this let's use an

example that is going to be familiar to

let's let's return to the example of a

in the beginning of this video so a car

as a class is going to have certain

for example a name a model price

a color and so on and then it is going

to have behaviors as well for example it

drive now what kind of derived classes

base class what kind of classes are more

so what kind of car types are more

than just car let's say for example that

one derived class that is called

electric car and then another derived

this electric car is also going to have

all the same properties that a car has

um it is going to have a name a model a

and then it is going to have a method

called drive and then this conventional

car is also going to have all those same

but they are going to be different

between them this car for example

is going to have a method called charge

is electric car and we need to use

electricity to charge it and then it is

going to have an attribute called

for example battery status and then this

gas car conventional car is going to

think which means a method of putting

in that car and then it is going to have

which is named tank status which shows

how much gas do you have left in your

derived classes is going to have their

but they will also inherit all of those

attributes that their base class

has so now let's see how we can

implement that on the example that we

so here i have class called employee and

what i want to do is i want to create a

here so what kind of class can inherit

employee class logically what is more

let's say for example that we want to

developer so let's do that here i'm

and then let's call this class developer

we have created a class called developer

is i want to make this class inherit

this class here so how do you do that

you put column sign here and then you

specify the name of your base class and

a few things happened this class here

a child class which means a subclass or

or derived class and then this class

is now called base class or super class

parent class and also another thing that

is that this developer class here now

all of these properties and then these

as well that our employee class has

so developer has all of the properties

that employee has and then let's create

one more property that is going to be

only so i'm going to make that property

so here i'm not obeying the rules of

encapsulation you can do that for

so let's create a property that only

employee does not that is going to be

favorite programming language developer

has favorite programming language but an

employee does not necessarily have to be

so he does not necessarily need to have

a favorite programming language so i'm

i hope that i didn't make a typo here so

now let's check if everything that i

correct so if this developer indeed has

access to all the properties that our

and then to this one here which is

this code here and then i want to create

object of class developer so i'm going

and let's call that object d like this

okay and the first thing that happens is

it says default constructor of developer

referenced so this error here is

obviously because we don't have a

default constructor and if you remember

when we were talking about constructors

every class when you create it every

automatically generated default

but once you decide to create your own

lose that default constructor so now you

well selena we have not created

constructor for this developer class

and i'm going to say you're right but we

employee class and that employee class

has a constructor that we created so

that means that we need to provide a

constructor for the derived classes of

employee class which is our developer

so now what i'm going to do is i'm going

to create a constructor for developer

so if you remember three rules of

first rule is that it does not have

second rule is that it has the same name

and then third rule is that it needs to

public so i'm going to call this

and then i need to put these curly

so now the job of this constructor here

developer object and considering that

this developer object inherited from

that means that developer has

these properties so these attributes and

specific for only developer class so now

provide this constructor with those

parameters so i'm going to copy

these three here because i already have

and then paste them here and then i will

add this parameter which is specific for

developer class so i will say string

like this so this is favorite

actually let's call it favorite

programming language so that we are

these four parameters to our developer

what we have to do is we have to

attribute here and then these three

attributes here because those are

developer class has but one thing that i

want to show you is following

considering that this developer class is

inheriting from employee class

and that employee class has a

constructor of its own which is this one

that constructor already knows how to

name and company at age so that means

that we don't have to worry about

these three properties in our developer

class we should only worry about

a specific properties of our developer

class and these three properties that

actually belong to our employee

we can pass these three to our employee

so i'm going to do that like this

here so after this parentheses

you put a column sign and then you

specify the name of that constructor so

base class and then here i will pass

these parameters that our base class

okay so now this constructor the

constructor of our base class is going

these three properties and we are left

with this property here which is

for our developer class so here i want

our favorite language property

so i'm going to say that favorite

has been passed to this constructor

down there to my main function where i

let's try to construct an object of type

developer let's try to invoke this

like we did in this line here and then

employee so i'm going to say that a

developer is going to be equal to

developer and then here i want to pass

values for this constructor here so

name and company name and age and

favorite programming language

so let's call this developer also

salvina i'm going to delete these two

because we will not need them anymore

and then let's say that i work for

i am 25 years old and my favorite

so now that i have successfully created

let's test this so how am i going to

well let's create a method on our

developer class that is going to use

values here so that we can know if these

successfully so what kind of method can

implement on developer class for example

we can implement a method called

fix bug so i'm going to do that let's

collapse this constructor and let's say

and then what i want this method here to

plus or selena fixed but using c plus

so here i'm going to do that i'm going

c out and then in order to access the

of this employee i'm going to use getter

get name which we created previously

and then here i'm going to say fixed

and then let's use this programming

like this okay and now if i want to test

this method here i can invoke it on my

developer class so i'm going to say d

dot fix bug and if i run my program

as you can see this method here works

the values that we passed in our

constructor have successfully been set

and then fixed bug using and then here

is my favorite programming language

okay let's close this and now there is

one more thing that i want to show you

and that is this part here so i want you

that this here is the way that we are

in order to access properties of our

and then we are accessing directly

our developer class so can i

access properties of my employee class

this property here for example name or

do i have to access it by using

getter write me your answers in the

so i am going to test that now here i'm

name and we get an error it says

employee name is inaccessible so we

access this property here name which is

our employee class why well if you

remember when we were talking about

whatever is private that is going to be

hidden that is going to be accessible

that class and then whatever is public

we will be able to access that outside

so how can i make this property here

how can i make this name accessible in

the derived classes of this class

here again when we were talking about

access modifiers we said that there are

public private and protected and i

promised you that i am going to explain

what protected access modifier means so

in this situation if we make this

if we make this name property protected

name available in derived classes of

like this and then i'm going to take

this property and i will move it to my

and then if i return to my developer

as you can see the error that we

is now gone so now this name property

is accessible directly from this derived

and if i run the program it should work

and as you can see it indeed works the

same so selena fixed bug using

c plus okay now there is one more very

related to inheritance that i want to

show you and that is the following

let's say for example that this

developer here fixes a lot of bugs

so if i run my program you can see that

salvina indeed is fixing a lot of bugs

so what this developer here decides then

is he or she in this situation she

for promotion so i'm going to try to do

dot but if you look at the list

that i have here it only has the access

to favorite programming language and

fix bug so if you look at this you can

see that i don't have access to the

that i have inherited from my employee

i do have access to them here so if i

in this method here so if i say here for

ask for promotion as you can see i do

but as you could see on my developer

access those properties so how can i fix

well the answer is pretty simple this

inheritance here so this part of the

this inheritance is private by default

and in order to fix the problem that we

we need to make it public so here i'm

public and the problem should

disappear so if i return to my developer

and if i try to ask for promotion as you

maybe i'll get a promotion this time

this and unfortunately i am not

getting a promotion but at least my code

now we have access to this method here

employee class so in my base class and

by making this inheritance public

inheritance let's create one more class

that is going to inherit from this

employee class so let's create one more

besides this developer so what kind of

class can inherit from employee what is

class than employee for example let's

teacher so here i'm going to go one more

time through this process of creating

class and then i will call it teacher

we have successfully created a class

and now what i want to do is i want to

like this okay and then what i need to

is let's first create some specific

attributes for my teacher so let's say

so this here is the subject that my

and then let's say also that the teacher

a functionality that is going to be

and that functionality will be called

lesson for example so i'm going to say

and what i want to do here is i want to

do something like this so i want to say

for example the name of that teacher and

he or she is preparing lesson from this

subject here so let's delete this

empty space so i'm going to say std

c out and then let's put the name of my

preparing and then here i want to put

the name of my subject like this

okay now there are a few problems

that we have with this class here the

these here are private which means that

we will not be able to access them

outside of this class here because

is private by default so that is going

and let's fix it so i'm going to say

like this okay now second thing that is

going to be a problem for us is that

this teacher class here does not have a

so if i try to create an object of type

teacher i will not be able to do that so

error now i'm going to say teacher

like this and let's call it t and well

if i do this only we should get an error

default constructor of teacher cannot be

which means that we need to implement a

constructor for this class here

as we did for this class here so as we

did for our developer class so let's do

i am going to create a constructor

like this and then this teacher

will receive these three properties that

like this and then it will receive

this constructor is going to pass these

to constructor of base class so this one

let's say here employee and then

inside i will pass name and company

and age and then here this teacher class

will take care of constructing

this part so i'm going to say that my

is equal to this subject that we

okay and now if i return down here

let's name the teacher jack for example

and he is going to work in a school

cool school if such thing exists

okay and then he's going to be 35 years

and he is going to be history teacher

now we have successfully created our

jack and i am going to collapse this

and then i want to test this method here

teacher t dot and then prepare lesson

okay and if i run this program

it says that jack is preparing history

lesson okay and then one more problem

that we have with this class here is

teacher t does not have access to

properties of our employee class so

if i type t dot as you can see it has

access only to these two properties so

in order to fix that problem i am going

i try to make jack ask for promotion

let's see if jack gets the promotion

i didn't get promotion okay it says jack

got promoted excellent and i didn't and

35 years old and i am 25 years old

okay so if you don't know what i'm

talking about you will have to return

to the part where we are talking about

um encapsulation i believe okay

so that was the story of inheritance we

two derived classes one is developer and

and those two classes are inheriting

employee as you can see here and here

and we will be using these classes in

example to explain our next concept

polymorphism as i said fourth principle

of object-oriented programming is called

polymorphism and this is the one that i

most for some reason i don't know why

simple and cool as you will see so it is

only if you understand the things that i

have explained so far in this course

so the first thing that i want to

polymorphism uh the word itself comes

from greek language and it is a compound

and morph which means many forms

and in programming polymorphism

of an object or a method to have many

forms now the most common use of

is when a parent class reference is used

object of a child class does that sound

a little bit complex when i say it like

i believe that it does and i agree but

stick with me for a couple of minutes

and let me show you how simple this

on the example that we have here so in

order to explain how polymorphism works

let's return to our employee class so

this one here i'm going to collapse this

and then what i want to do in my

employee class is i want to implement

method and let's call that method work

and then here in this method what i want

is i want to say for example

employee and then let's say that that

and then he is checking also task

task backlog and then he is performing

and so on okay and let's add end line

let's return to this main function here

and what i want to do is i want to

that new work method that i just

and i want to invoke that method on this

teacher class because considering the

developer and teacher are inheriting

we should be able to do that so if i say

let's see what will we get if i run this

okay it says here selena is checking

task backlog performing tasks and so on

and then jack is also checking email

task backlog and performing those tasks

but one thing that i don't like is i

emails and checking task backlog and so

what i like is i like to write code so

how about implementing this work method

so let's return here to my developer

and what i want to do here is i want to

instead of checking email and checking

on what i want to do here is i want to

that i am writing code so let's say

writing and then let's put this

like this and then let's say

code okay and now i'm going to do the

teacher class here so let's copy this

and then paste it in my teacher class

but instead of writing code teacher is

going to be doing something else

so what teacher does well teacher

so i'm going to say the name of my

jack in this situation so jack

and then let's put the name of his

which is history i believe okay

now one thing that you must notice is

work method has the same name here

and then here as well and it also has

in this base class so it is called work

but the implementations of this method

are different here it says that an

email and checking task backlog and so

and then developer has also its own

implementation which says that developer

writing code because that is his work

and then teacher is teaching his

i have here invoked these two work

and let's see what will happen if i run

okay now it is better it says that

plus code and jack is teaching history

okay now let's make one small

improvement to the code that we have

or actually two improvements and the

writing with double t okay what is this

and then the second one is going to be

the rule that i mentioned in the

when i said the most common use of

so let me copy that definition here and

literally word by word so here it is

and it says the most common use of

when a parent class reference is used to

to a child class object so let's see

what this means in practice i'm going to

these two lines because i don't need

and then what i want to do is i want to

like this and one quick disclaimer if

you are not familiar with pointers

i am going to refer you to my channel

which is code beauty with where i have

an entire playlist which is dedicated to

pointers so make sure to watch that and

with this example here so again

i have created an employee pointer

and what i want to assign to this

employee object so i want to assign to

pointer here a reference of this

teacher class so here i'm going to say

a reference to this developer here

and this here is the rule so a pointer

reference to derive class object which

this part here so so that is just the

why well because this developer here

this developer is deep down inside it is

an employee because developer is

employee as we already could see in the

course so now that i have done this

let's do the same for our teacher so

i'm going to copy this line and then

employee 1 and then this is going to be

and here i will say that employee 2 will

the reference of my teacher here

so now what i want to do and what i want

is the following wouldn't it be nice if

i could do something like this

symbol here is used when you want to

members using a pointer so instead of

dot symbol when you use a pointer you

okay so wouldn't it be nice if this here

well let's see maybe it does if i run my

as you can see it again says alina is

and task backlog and performing tasks

and so on and jack is doing the same and

he's a teacher and i'm developer

so that means that this here does not

but if you only knew how close we are to

you are literally missing only one tiny

little thing that i didn't tell you

what that thing is well let's return to

this work method let's make this work

virtual so i'm going to say virtual

here so let's run our program now

and as you can see it says saudina is

plus code and jack is teaching history

it works wait i have to explain this

okay so let's return to our employee

to explain what just happened well

look at this function here this virtual

this function with this virtual keyword

function and what that means well it

when a virtual function is invoked it

hey can you please check if there is

implementation of this function

in my derived classes and if yes please

execute that instead so what ends up

is that the most derived version of this

executed and that is this function here

this function here for teacher

so does that mean that if we didn't for

implementation of function called work

that our developer then would do this

work function the answer is yes so let

just to demonstrate how it would behave

so now i commented implementation of

developer class so if i run the program

as you can see it says salina is

email and task backlog and performing

and then jack is teaching and this is

teacher class has its own implementation

work method work function but developer

does not so here developer is going to

parents class method work which is this

method here so that's why i am checking

email and task backlog and performing

tasks instead of writing c plus

code so let's close this and let's now

i'm going to uncomment it like this

and let's run my program once more and

everything works again and we have this

so that ladies and gentlemen is

so let me give you one little tip of

what we can do with this knowledge now

and i'm going to return to my main

here as you can see here we are creating

a base class pointer to a derived class

object which is in the situation

in this situation it is teacher and then

in some other situation it can be

some other type of employee so if we

created more derived classes which we

could do we could create for example

derived class called bus driver or pilot

actor doctor and so on and then

we could keep objects of those derived

so we could reference them with this

pointer because they would derive from

and then imagine situation where you had

thousand or even 10 000 employees

and then you are storing those like this

like we did with our developer and

our teacher and then you could literally

say employee dot work and what would

all of those different types of

doing their own job so painters would

actors would act doctors would talk with

i don't know so that is the most

beautiful thing that we have

achieved with this here so i hope that

if you did give it a thumbs up and then

also if you want more videos like this

you can visit my channel which is code

beauty and there i have a lot of

videos related to programming and c plus

and also leave me a comment i would love

i am coming from precode camp or

something like that on one of my videos

so thank you very much for watching and

i'm going to see you in some next video

if you decide to visit code beauty

youtube channel i will definitely see

so now i'm going to take a break for a

couple of hours but for you it is going

to be just a couple of seconds because

i'm going to pause the video here

and for this i want to try a transition

so what is polymorphism the word

polymorphism comes from latin language

and it means or is it greek

i'm just going to skip this information

so if i hover over this as you can see

error but we have a different error

which means that we are making progress

also visit me on my channel which is

code beauty and subscribe there

and also leave me a comment i would love

to read something like hey selena i am

and your course was great or hey salina

i am coming from free code camp and your

excellent or perfect or outstanding or

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.