Hello everyone.
So in today's session we will continue our discussion on constructor and we will also learn one more
thing which is called this keyword.
Okay.
So let's start out with the decision so let's add one more constructive one more pyramid based construct
but not this time.
This bedroom a dice construct Beverly take two barometers each.
Andrew Lambert OK a general number so property age is barometer age and property.
Rule number is the barometer number.
Okay.
Let's add one brain statement also.
So see out construct three called
okay.
So what will happen now.
If I will create an object with two barometers.
So student six two parameters 10 and under the value of a general number and what will happen.
So this will be interpreted as as as six dot student
Bennett ended.
So which constructively.
Which construct grade will be calling.
So construct better material be called because this is matching constructor Okay so construct them but
table book called.
So if I write a six star display now our age and got an order a general number they do not contain garbage
values.
Age is simply 10.
And rule number is a hundred.
Okay.
This time did not contain any garbage values.
Now let's see
Su let's create a student object.
STUDENT 6.
And in order to call constructor three we have to parse two arguments.
Age and rule number and let's call the display function to a six dart display.
Okay so we have to run this file first.
Make positive changes to this file and now we will run this file
so construct that record.
Okay then.
And under this time neither age nor the rule number contain garbage value then is the age under this
deal or no
okay.
So tell me what will happen if I like if I come on down the default constructor so let's come and out.
The default constructor now what do you think what will happen.
Okay.
So as soon as we create any constructor whether this default or betterment praised then the inbuilt
constructor is not available to us anymore okay.
And will domestic is not available to us anymore.
So at this point of time I have only two constructors and both the constructor that parameter based
constructors.
Okay so student S1 this line will throw us at it.
STUDENT As to this plan we also throw us it assemblies to industry this time will also through a set
away because and then really what happens is how.
This is how this line is interpreted it will call as One dot student
so it is trying to call a constructor with no arguments and in this file and in surrender typically
file I do not have any constructor with zero barometers with zero arguments so I will get an error saying
like matching constructor not found something like this matching construct the north found or better
can be like something like this.
We cannot initialize S1 cannot initialize S1 Why.
Because the concept is not there.
Okay.
Construct is not there.
So as soon as we create our own goes to bed.
Default Constructor vanishes.
Default Constructor goes away.
Okay now let's see that it
okay.
So why this is running.
Because after doing changes to this file after commenting commenting it out I didn't run this file.
So first we will run this file okay and now run this file so I've got an idea and you can see that it
so no matching function for call student student.
Okay so this is data that we have discussed it cannot find.
It is not able to find the function okay.
Now let's experiment with one more thing.
So what will happen if I like change the parameter name to property name.
So let's make it all No and let's also make it rule number.
So the barometer name is the same as the property roll number.
Okay.
Barometer rule number is same as the property number.
They both have the same name.
So what will happen at this line.
What do think what will happen.
Okay so to understand what will happen we have to go back to the class of scope of variable.
So if you remember scope of a variable let's take an example to understand.
I have this small code and based in if a is there then 5 goes inside the loop create another variable
with the same name.
Let's initialize it with 1 and now let us try to print the value of it.
So according to me output will be when the output of this line will be one by one.
Because at this line would come by level to come by level prior to find the closest a compiler will
try to find the closest a which one is the closest this one is the closest.
Add this line.
I have 2 is I have 2 is this one and this one.
Two ways are visible at this line but come by level try to find the closest one.
Since this is the closest one output will be 1 Okay.
Now see here.
Rule number at this line I have two rule members I have two numbers to rule numbers are visible at this
line first one is perimeter rule number and the second one is property rule number.
So what do we think.
Virtual no compiler consider I think the barometer one way because this is close.
This is close the property rule number is like defined above.
Okay.
Property really embraced defined above.
So this rule number is the closest.
So at this land what will happen for the video flow.
Rule number two.
Rule number.
So basically this line means nothing.
Net result zero net result is zero.
This line is like absolutely doing nothing.
Okay.
So now what I want is already
our aim is very simple.
We want this window number to be barometer rule number and this rule number to be property roll number.
So this is our simple him.
Now how we can achieve it okay.
How can we achieve it.
So there are two methods to achieve this so let us discuss both the methods.
So the first method is make rule number out again.
So it was I'd make it out again.
Make it out again.
So if the names are different if the name of the property and the barometers name are different then
there is no confusion at all.
But now let's come to the second case I want this to be rule number only.
That is I want property name is same as barometers name.
Now how we can achieve our aim so we can achieve our aim with the help of this keyword with the help
of this keyword.
Now what is this keyword.
So this holds the address of the current object holds the address of the current object.
Okay so I think pointer holds memory or this is we have discussed pointers and we know pointers holds
memory addresses.
This is also holding the address.
What does it mean.
It means this is a pointer variable.
This keyword is a pointer variable.
Again this giver is a pointer variable.
Every function of the class.
This is a function.
This is a function constructor is also function but it is a special function.
This is also a function.
Okay so every function of the class every function of class have access to this keyword every function.
Have this keyword often on their own of their own.
Okay.
Now let's see uh how can we achieve our aim with the help of this keyboard.
So what this uh what this keyboard used to do is call this one what I will do I will create a student
s with one parameter let certain so this constructor will be called.
Now what happens is I will get h I will get rule number
and name is s and let's say that I dust off this memory block is 700 so this function the student the
student function have discovered.
So the student will contain a student.
This will contain 7 ended okay.
It will contain 700.
Okay now let's deep dive.
How this can be used.
So see when this function is getting called when this concept plays being called what it is doing for
the value of a into each but whose H whose objects h.
Okay.
Whose object's age.
There are multiple objects we have we have collected many objects but whose s1 s2 s3.
Whose whose H I am referring to.
Okay so what happened is whenever you call whenever you call a function of the class you pass the address
of the object to the function let me let let us understand with the help of an example.
Okay so whenever I create an object let's say I created object s with two barometers.
Then it ended.
So what.
This function will do this function will call this construct to construct the number t matching constructor.
Okay.
And let's say this is H and this one is rule number and that and this is 700.
So this student function have discovered now this keyword will contain 700 via 700.
Because then this is when this call is when this call is there it passes 700 it passes the object it
passes the address of the object to 700 is being passed.
So that's way seven hundred will get stored in this point.
No.
When I do so at this line of what I'm doing.
Age equals a put the value of a in each but whose age.
How do I know whose age this age this is.
With the help of this I can data friends that I am talking about S1 I'm talking about as.
So this each so age equal say implicitly this implicitly it is interpreted as this arrow age is a.
Okay.
So this contains seven hundred so seven hundred arrow age is a what is the value of a ten.
Similarly this adorable number is containing hundred okay.
This is same as star.
This dark age is.
Okay so this is how it works.
So whenever I object calls a function it will pass.
It will give them function its address and that address will be stored in that this keyword update function.
Okay so this is how it works.
Okay so now let us know let us see in code.
Let us try to print the value of this.
What this contains.
Okay so which class supply should I.
Okay so let's first comment out everything.
So to avoid this or to avoid confusion
so I am commenting out everything to avoid confusion.
Now let us create some objects.
Okay.
I am creating object as one.
Now let's say uh it contains two barometers age and the rule number.
Now let us branded the best of S1 so c out
C older.
This of S1 how we can get this off.
S1 using the address operator.
So where does off S1 Okay so it will call this one this constructor.
Now this constructor has access to this keyword.
So the address of s so that s of S1 will be stored in this keyboard.
This is a pointer a pointer variable.
So this and and line.
Okay let's send this file.
Now let us also read this file
C and it does of S1 is this one and this also contains the same address okay.
So this will contain this is containing that dose of S1.
Similarly if we will create more objects I am creating one more object.
Let's call it S2 and led to the values R and D and 200
let us spend the Nestle festival
now that doesn't decide
so at this office when this what I've seen and does a festival and that dress off as to is in this this
point are valuable.
OK.
So I hope you understood the point.
Whenever I object calls a function it will pass its address and here in this function that address will
be stored in the variable.
This in the keyword this Okay so age you could say this implicitly is interpreted as this ad age.
But using this is optional using this is optional.
Okay.
Similarly using this is optional for here.
Here also here also using this option you can use this or if you don't want to use then don't use it.
It is optional here but in this case see in this case where the parameter name is same as the property
name head it is mandatory for us to use this operator to use this keyword sorry.
So here it is mandatory
here.
It is mandatory to use this.
Why does mandatory.
Because we have to differentiate between the barometer role member and the property remember but
okay so how we will call this function we will create an object student test with one parameter let's
say 10 so all number is containing 10.
This function will be called and let's say it does is 200.
So I have a memory block which s age and rule number and IDs addresses 200.
So 200 is being passed these two hundred will be stored in this keyword of this function.
So this contains 200.
So what I'm doing here is this at oral Lambert
is better midazolam but so better midazolam but is Ben and this at room.
This this this is containing so I want 200 so 200 adderall but is Ben okay.
So this is how it works.
I hope you haven't destroyed everything.
Okay.
So what we learned in today's session.
So we learned verdict I suppose we learned how they are created automatically how they are called automatically
with every class we get a default constructor for free.
How can we create our own constructor with different parameters.
And at last we see what is this keyword and how to use it.
Okay.
Okay so one more thing that I want to cover is suppose I want to add a constraint.
I want to add a constraint what constraint.
I want every object I I want property age should not contain garbage value property age should not contain
any garbage value.
So how can I achieve this goal.
How can we add this constraint on the object.
So what I will do I will write a bottom or trace constructor just like this one so I will write a better
modest constructor so student.
This better modest concept will take ages and put and this will do.
Age equals a.
So as soon as I created this bad image tries to construct that as soon as I created this paramedic's
constructor default constructor is no longer available.
Default Constructor goes away it is not there.
Okay.
So whenever a person wants to create the object of student class what it will do first of all it will
try to do student s but this will throw at it why I did because there is no matching function no matching
constructor will be dead.
Okay.
There is no matching constructor.
And similarly if he tried to pass two by two arguments let's say 10 and under.
This would also throw us at a because there are no matching constructor.
So ultimately if he wants to create object of student class he has to give one value he has to give
one integer value as parameter and this integer value will call this paramedic's constructor.
And this the age will be set to die days will be said 210.
Okay so in this way my property age cannot contain a garbage value.
Okay so for creating the object of student class the user have to give one digit argument.
Okay so this is how we can add constraints.
Okay.
How we are able to add constraints because as soon as we create any constructor any whether this barometer
used or default or any other type of question that this default constructor is no longer available since
this default constructor is no longer available.
This statement is not valid anymore.
Okay so I hope everything is fine.
So thank you.