Hello everyone.
So in this video we will discuss some more concepts of object oriented programming.
So in the last video we discussed about excess modifiers.
We know how public and private affected the accessibility of the properties of the class.
So we created this class in the last video.
It has two properties rule number and age.
Rule number is public.
And ages private.
So let's add one more function to this class and let's make that function public.
So I have a public function the name of the function is let's say display so what this function will
do is it will print the age and it will number of the student.
So a return type will be void C out age space and and but
so this function displays the H.
And rule number of the student return type will be void.
Okay now let's discuss how to call this function.
So we will call this function using the dot operator just as we use dot operator to access the properties
we can use dot operator to access or to call the function.
So for property what we used to do is object name dot property name for calling a function object name
dot function name.
Okay.
So S1 so S1 dot uh what is the name of the function.
Display s under display.
Similarly we can call as to dot display and let's call display function for S6 as well.
Starred as six dot display okay.
And let's write short also for a six so a six arrow display and that's all okay.
So this is the V to call a function.
Now our function is not thinking any argument if our function takes our argument we can positing fantasies
since our function doesn't take any argument our identities are empty.
Okay so now what I want to do is uh I want to use this each property outside the class.
This is properties private.
So my question is how to access a private property outside the class.
So the answer is using a function.
So let's make one more public function uh written by will be in danger and the name of the function
is.
Get each.
So this is a public function but this function will do is it will done each age is a private property.
So what is happening now here is see this get page function is a public function.
So since this is a public function we can call it from the other file that is from the student does
not CPB.
So we can call this function and this cottage function will return us each whereas age is a private.
Okay.
So with the help of this public function we can get the value of private function.
Now let's see called.
So this is our file.
Uh what I want to do is uh see out S1 age
s dart we will call the function get age get data is a public function.
So no compulsion that I will be there.
And similarly let's uh do it for this to as well so see out
as to age
as to dart get garbage
OK let's add an on line here OK.
Everything seems fine.
No
so one more thing uh we can only access the age.
We cannot set the age.
So if we will run this program we will get compilation error saying that this age property is private.
So you cannot access them.
So let's make uh let's come on get out age and age.
Okay.
So now what program will run fine.
Okay so let's run our program
Okay so the submitter is found out display what is the arrows.
Glass rent has no member named display.
Oh uh.
We also have to run this file because we did genius to this file.
So this file should also be run now.
Uh see out age and lumber.
Oh it is that is
CO2 is not declared in this code.
So the error is uh this see out the functionality of the C out is defined in your stream file so I have
to include I Ustream here.
Mm hmm.
Again it is.
Okay so this line is defined in namespace.
So namespace the.
Now this is fine.
Uh let's send this file.
So ages private.
Okay.
So we also have to come into it out here and also we have to come into get out
and is this seems fine.
Okay so let's print let's get dates for Essex also.
So see old Essex
six not get each
thing everything is fine now.
Let's start now.
But now let's run this and now we will run this six year dynamic.
So for dynamic we have to use data fence celebrated and now let's send this file
seems like everything is running fine
okay.
So let us analyse the output.
So first of all uh the call the display function.
So display function will print that each and the usual number.
So this is the age which is a random value because we have not set it and this is under because we certainly
number two we entered.
Okay.
So after S1 the print is too.
So if it is true we didn't set any values about the values that got the beach and then we are getting
that H for this on that each for this one is this it is a random value because it is a private field
and we have not make of public function to set it.
Similarly as two ages also a garbage value because we haven't said it.
And then S1 daughter number which we said to be hundred and after that exit door.
We said that all number for a six and a six star display.
So for a six the ages that number Lou and that number is tardy because we said it then again we six.
Then again uh display function.
So both are the same.
And then we got the H for Essex so each for Essex is at random value.
Okay so the output is expected output is what we expected.
So instead of like using the lead fence operator and the DART Operator we can directly write Essex had
a wage
Yeah.
It's working fine.
Okay.
So so now I can read the property of a private field.
So if I can read it then they should also be happy to read it.
So for reading the press or for writing the uh private property what we can do is we can create a function
void stage so this stage function will take age as input and it will set age to a and that's all.
Okay so let's come to this class.
So this class is like we will get confused using this class.
So I created a new class student new DA CPB.
So now we will do the work here.
So for doing something first of all we have to create object.
So let's create two objects.
One is starting and that one is dynamic.
So Student 1.
Now let's create a dynamic object.
STUDENT As to new student
so for this student to work we also have to include the file here so hashing include
student art CPB.
Okay so now let's set the age and then we will display so as are not stage oh now let's say a stage
and let's say the value is 20.
Similarly for as to as to Edwidge so the S2 0 stage and let's say 30.
Now let's call as our display
and as to add or display.
Okay so now let us run our program
so as not set age there is something wrong with it glass front has no members her age or I again did
the same mistake.
I have to run this file because I did 10 years to this file so this green line means that these men
alignment of lines I already compiled and this yellow lines means the area to be compiled so called
Lux's so called logs also help us so now a venue will render program this whole code will become green.
See now everything is compiled.
Now it will work.
It will not draw any other
so ages 20 and the roll number is garbage value.
A just 30.
Rule number is the garbage value
so in this way we can like we can get the property and we can also set the property.
Okay.
A private property.
So one question that will be coming to her mind is what is the use of making each a private field.
If we can't read and write it then basically what did we achieved by making it private.
What is the reason for making something private.
So look carefully we cannot change the age radically because it's a private field but we have to call
a function get age or set age to access the private property age.
Okay so now let's take an example to illustrate to the benefit of private let's say I want to put a
constraint on age that age cannot be negative.
If age would have been public then anyone can set it to be minus one.
How as one dot age equals minus one.
And it would have been Vogue if the age was public but now the age is private.
So I can put a constraint here before starting the age.
What I will do I will check if the input is less than zero then simply done do not do anything.
Simply written we are not setting age okay so we can add constraint like this.
So this is the use of private property.
What we will do we will make a public function to access the private property.
So now here we can add constraint.
For example like uh if you want to add more constraint like there should be a password you have to give
password as well for setting age basically an authorized person can notarized person can only set age.
So let's say the password is 1 2 3.
So if password is articles 2 1 2 3 if password is not mandatory then do not do anything simpler then.
Okay.
So now we got the property to add the constraint we get the power to add the constraint.
So I can add multiple constraints here.
So whenever you are working in a real world project.
If a property is important that is a third person should not be allowed to use the property.
So we should make those properties private.
Okay so this is a good function.
This is a third function get function.
We can read and with the help of said function we can write.
So we generally call it get a function and set the function getter and setter with the help of get the
function.
We will get the data with the help of the function we can said that you do okay.
Thank you.