Hello everyone.
So in this session we will learn a few more important concepts of object oriented programming.
So before starting today's glass let us try something so when we write so when we write in day 5 what
will happen we will get a memory block A and it contains the value five.
Similarly if I want to create a budget constraint where does this index end questioned BTC.
So at this vein what will happen.
I will get a memory block B and it will contain five.
Okay so we can write constants before integer or after integer.
Okay so let's have a choice now.
Can I break this line into two parts.
I can make this statement and do two lines.
So the first line is in a and the second is a S5.
So I had this line.
I will get a and it contains a garbage value and at this line I'm overriding the garbage value with
the value five.
Similarly Can I break this line into two parts and do two statements.
So the first treatment will be I'm creating I integer constant so integer constraint B and it's second
line.
I will copy the value.
So at this point what will happen.
I will get time to Block B and it will contain I'm gonna visually enter this line.
What we are trying to do here is we are trying to overwrite this garbage value with the value 5.
So this line will throw us at it via error because we can see here is integer constraint we cannot change
the value of B once it is assigned.
So at this line B contains the garbage value B contains garbage value and at this land we are trying
to override the garbage value with the value 5.
So we will get an error at this line.
So before getting error at this line we will get error at this line.
So why an error.
This line compiler will throw error at this line because what we are trying to do here is we are creating
an integer constant.
We are creating a digital content and adding digit constant contains a garbage value.
Okay.
So don't be a constraint on Basic garbage value basically that we lose use less what is okay since it
contains the garbage value.
So it is useless for us compiler will not allow us to do this.
So what we have to do we have to initialize the constraint we have to finish that's the constraint variables
at the time of its creation.
Okay so this line is correct.
We have to initialize the constraint variable at the time of its creation.
Now let us take one more example.
So and day is 5 so I really get my memory block I and it contains the value 5.
Now I want to create that offense variable so of variable G is a.
So the name of this block is also G.
Okay so both I enjoy points to this memory location.
Now can I break this line into two parts into two statements.
So the first statement is create a reference variable and the second statement points to J will point
do I.
Okay so again what will happen.
We will get better.
This line so just like the constant variables it is compulsory to give the value of a reference variable
at the time of its creation.
Okay so at this line what will happen it will contain it will point to a garbage memory location okay
so compiler will not allow us to do this.
Okay so for constraint and for reference variable for constraint and for reference variable we have
to give their value at the time of their creation at the time of the memory allocation okay.
Now let's see in code
so I have a class here.
Student So this student contains two public properties each and William.
But now my aim is very simple.
What I want is what we want is once the roll number is set.
It cannot be changed similarly.
So what we have to do here if we write C++ then it will throw at it.
It will throw Error because B's in beta constant B's integer constant.
Similarly my aim is once the value of the lumber is set but the value of raw lumber is set.
I cannot change it.
So what we have to do we have to make it IPG constant so we can write construct before integer or after
ingredient.
So here I am writing question before integer okay.
Now let us create objects so student S1 not a desk tried to change the values of a general number.
Okay so as one dot age lets certain and S1 DOT rule number let's say hundred.
Okay so let us win this fight.
What will happen at this line there will be an edit and because we are trying to change the rules.
But okay so let us first read the file and see that it
so would you indicate then that it why probably we didn't run this file.
Yes.
Okay now we will read this file again well guess what we get an error at this line.
Now let us try to understand why we get an added
so at this line.
What will happen.
Default constructed will be called default in the constructor.
Okay.
So default and built constructor will get called and what it will do age.
Rule number both will be set to garbage value.
So a general number both contingent garbage value.
So this is to rent then that's when they both contain garbage values.
Okay.
Add this line.
What I am trying to do I am making.
I am overwriting the garbage value of the age to pen and add this line.
I am overwriting.
I am writing this garbage value and I am trying to make a hundred.
But you cannot do this.
Why.
Because rule number is a constraint.
If you see the glass if you show the glass we have make it in PJM constant since all lumber is in digit
constant.
Once it creates a value we cannot tolerate it.
So here we cannot tolerate this garbage value.
But if we will see we get an error at line number six why we go down a red line number six because what
we are trying to do here is we are creating we are creating a rule number we are creating and digital
constraint which will contain the garbage value.
So compiler will not allow us to do this.
So basically what compiler will say if these integer constant and this integer constant contains some
garbage value then basically it's useless it's useless to make integer constraint.
Okay so that's why we will get it at line number 6.
We will also get in that red line number eight.
But before line number it we will get that red line number six.
Okay so it is amended to do initialize the rule number at the time of its creation at the time of memory
allocation.
Okay so the one way to correct that one for the one way to correct that record is what we will do we
have to initialize.
So let's make a hundred here now.
There is a problem.
So what is the problem.
Suppose I have tends to rinse.
Then what will happen.
All that tends to advance will have the roll number is handed and they cannot change it.
So obviously this is a wrong approach.
This is a wrong method.
Okay so what we should do.
So where to initialize so that each object can have its different number of each object can have different
number.
So the answer is constructor y constructor because constructor is called for each object and construct
will be called on live once.
Okay so let us create a constructor
so we will construct.
We will create a better matrix constructor it will take.
Rule number as input and then we will set rule number do.
Okay now let us run this file see we are again getting an error so let us try to understand why we get
in it at
so when I write
it a and in the next line.
If I write a equals five.
So at this line what will happen.
So before a I am not writing and Deja before I am not writing Deja.
So what does this mean.
This means that is already created and the memory is already kicked into the memory which contains the
garbage value and we are trying to override this garbage value.
Okay now we see this line rhodium but it goes out we are not writing in Egypt before William but so
that means that William has already been created in the memory and it contains the guide of base value.
And here we are trying to put odd R in doodle lumber and lumber contains garbage value since all lumber
is St. PJ constant we can not tolerate garbage failure without Okay so that's why we are getting added
at this lane.
Okay so what happened is as soon as we entered the constructor as soon as we each line number seven
rule number is already created in the memory and it contains garbage value.
So what is the solution.
The solution is we have to initialize the rule number at the time of memory allocation.
We cannot do it inside the constructor because as soon as we reach inside the constructor memories are
allocated and garbage based Ray Lewis present at the memory location.
Okay so what we can use here is there is something called initialization list
initialization list can help us here.
Okay so let's 1 again see.
What is the problem.
So the problem is very simple.
As soon as we reach line number seven as soon as we reach inside the constructor the roll number this
roll number property this roll number that a member has already been doing to the memory and it contains
a garbage value and add line number seven and line number seven.
We are trying to override this garbage value with ADD okay and we cannot do this.
Why.
Because a number is in beta constraint.
We cannot change the values.
We cannot change the value through a number.
Okay.
So the solution is initialization list so what initialization will do.
It will initialize the constraint properties at the time of memory allocation.
Now let's see this index
so first of all this is wrong.
Let us come into this code and this index for initialization list syntax for using initialization list
is very simple.
Put a column and then name of the name of the bat and we did the argument which is odd.
Okay.
And that's all.
Okay.
So let us win this fight.
See now this time there is no edit.
Okay so what is happening here is
so I told you when we will reach line number seven rule number property is already being created in
the memory.
Now see line number six.
So what will happen I like.
So what will happen at line number six.
We can assume that rule number is given memory at line number six.
Rule number is given memory and add the line number six.
We are writing this statement.
So this rule number will contain R okay.
So this way we can initialize our private properties week initialize have a constant private properties.
So at this line we can say what is happening constant in integer rule number
equals out.
So this is the meaning of this line.
Okay.
Memories being allocated to General Lambert and the OT is initialized and this is and rule number and
rule number of private properties initialized with that.
Okay so this is the use of initialization list.
Okay so what we can do here is it is not necessary to take an integer as input.
We can also set the value as we can also write it like hundred but also.
This is of no use.
This is useless by useless because every object will have the roll number hundred okay every object
will have rule number hundred.
So obviously this is useless now what if I take two parameters hasn't put a general number so in total
number and the second values and each.
Okay so rule number are and then comma each is a okay so.
So we should notice here is initialization list can also be used for normal variables.
Normal integer variables.
Okay.
It can also be used for normal properties so it is not necessary.
It is not compulsory that initialization list can only be used for constant data members.
Okay initialization list can be used for normal data members.
Okay.
Now what will happen if I change the argument name to each so we have to write aged
okay.
So this is correct.
There is no need to write this way because it is very clear.
It is very clear here that this age means this at age and this age is the argument age so there is no
confusion here.
Everything is very clear so there is no need to write this here.
Okay so there is no confusion since there is no confusion.
We do not need to write this okay so one more case where the initialization list can be used is suppose
we have a reference variable let us have a reference variable.
Okay so offense variable X so this X is e.g. defense easy reference variable
Okay so initialization list can also be used for reference variables to what we want is our aim is very
simple
so we've warned
this.
So this is our home okay.
But it cannot write like this way because this H if you see here this age can be argument page.
So there is a confusion here.
Since there is confusion we have to write something like this this arrow each so at this line.
There was confusion that age can be the argument age.
So to avoid the confusion we will write this.
Okay.
So writing this is mandatory here.
And I told you so I told you before.
Just like the constraint variables reference variables have to be initialized at the time of its creation.
Okay so it is mandatory that we have to use initialization list for reference variables so X and the
value will be this arrow each
okay.
Now let us in this file
now obviously as we write our own parameters constructor default constructor is not available to us
anymore.
So we have to use parameters constructor.
So let's say that rule number is a hundred and let's say the easiest 10.
Okay.
So obviously this line will not work
because when somebody is a constant property.
Okay now let us on this file and they should be noted okay.
So our code is running.
There is no added.
So what we learned in this session there's is something called initialization list.
And what is the purpose of initialization list.
The purpose is very easy.
It initialize
constraint properties constraint data members constraint properties and the defense's data member and
reference properties at the time
and their time of memory allocation
okay.
So I hope you have understood this video.
Okay.
Thank you.