Hello everyone.
Welcome to this new session and this session we will learn a few more important concepts of Forbes.
If you'll notice in the last session we never used a student name anywhere student name is an important
property that should be included.
So we should have to rename property but we do not include it anywhere.
Why we not included.
Because there is a reason behind it.
So now let's see how we will work on name.
Okay.
So let us create a class a student so let's create a class student.
Now this time I will have two private properties a general number so each will be integer
age will be integer and is a private property.
Now the name will be a string.
So for storing this string we will use a connectivity took our name and letter to the sizes entered.
Okay.
So we should not do this.
Why.
Because now we have started dynamic allocation.
So now we know how to create data dynamically.
So we should not create that is strategically so creating that dynamically index is very simple CAST
OUR NAME IS NEW.
Hundred.
Okay.
So the problem here is there is a problem here and the problem here is that we do not know the size
of the student name.
If it is more than handed then we will be bribed.
If it is less than a hundred then a lot of memory will be wasted.
Okay.
So according to the requirement we will decide the size of the student name.
So for now let's make it a character pointer.
Okay.
So let's make it a character pointer only.
And according to the requirement we will allocate sites.
So now let's create a constructor so the constructor will take two inputs each and lumber.
We are constructing a betterment based constructor.
So int h.
And secondly B name name.
Okay.
So this arrow h we have to use this here because we have to differentiate between the barometer and
the proper barometer and the property agent.
So this shadow reaches age and similarly this add on name is name.
Okay so what constructor is ready now let us create our display function so that we can print so display
so display function.
Let us first bring the value of Nim.
First we had printing the name of the student.
And then we will print the age of the student Okay so now let us create objects
so a student S1.
So is this treatment right or wrong.
So this is wrong.
Why.
Because as soon as we clear the better based constructor then build constructor in both default constructor
is not visible to us anymore.
So this line what it will do it we tried to call inbuilt default constructor and that constructor is
not visible so this line will throw at it.
Okay.
So what we have to do.
Okay.
So first let us create a name a String name.
So can name let's say the name is let's the name is ABC and now let us create a student.
So student is when we have to call parameters constructor let's say that age is 20 and the name is name.
Okay.
So now let us call display function.
So S1 dart display
Okay now that does create.
Okay so now let us change the value of names so let us make the name ABC.
So now the name is ABC.
Now we will create the object.
So student let's name it as two.
So that ages party and the name is name.
Okay.
So now let us call display function
okay.
So if you will run this file what will be our output.
So let's see
so for student S1 name is ABC and ages 20 for students to name is ABC and the ages 30.
Okay.
So this was our expected output.
So there's is nothing new here.
Now what if after printing as to what if after printing as true I am again printing the value of S1.
So what will happen find out dart display function.
Output should be ABC and 20.
Now let's see what do we output.
See there is something wrong.
The output should be the name should be ABC.
But the output is coming out to be ABC okay.
So why is it so.
Let us try to understand what is going good let us analyse what we have done.
Okay.
So what is the effect of this line.
What it will do.
It will create 80 ABC days and then and let's say that those of the 0 10 Nexus 7 ended okay.
So at this line will happen as friend art student name is 20 and it's already ages 20 and name will
be 700.
We all know how good it bad is our past in fact how it is passed we will pass the test of those Littleton
six so seven and I will go from here and that this line what will happen.
So age is 20 and name is 700.
Okay so this is the scenario ahead.
Now what will S1 contain.
So for the S1 age will become 20 and the name which is a character pointer it stores 700 okay too.
This is the scenario.
S1 so basically what is happening here is so the name is pointing towards this name.
Okay so the name of S1 the character pointer is pointing towards this name.
Okay now add this line as our display.
What it will do.
It will print the name and age.
So for name it what it will do.
It will go to the 700.
So we will reach at best seven hundred and then we will print the entire contents of the array which
is a b c d.
So here it is our output.
So output is a b c v and they just run T.
Okay so now what we did here is we changed the target index and now it becomes E.
So ABC e it is now ABC.
Okay.
And here how we will call.
We will call as to dart student so that ages party and addresses 700 similarly agent names will become
20 and set 20.
Sorry it will -- had 700.
And finally S2 will look something like this.
Ages 30 and S2 name also contains the address 700.
Okay.
It also contains that best 700 and this is our S2 so as to is also pointing towards this area only then
we will print the value of S2.
So for printing the name we will go to our number 700 and print its content so the content is ABC and
they just 30.
Now again we are printing S1 s under display so for us on our display we will do each S1 for printing
the name.
What we will do.
We will go to the seven hundred and print the content now editor seven indeed we have ABC.
So ABC will get printed and they each will be 20.
So that's how ABC is coming out to be here.
Which is wrong.
Ideally it should be ABC.
OK so what is happening here is both objects are pointing towards the same area.
If we do change in one object it will reflect in the other object also.
Okay so what we should do we should copy the end that every other then copying the ad does only.
Okay here we are only copying that address seven that is being getting copied.
Ideally what we should do is we should copy that Eddie.
Okay so this copy this copy is called shallow copy.
So what is the meaning of shallow copy shallow copy means copy only the address.
So currently we are doing shallow copy.
Okay.
So whenever we pass Eddie.
So rather than copying the entirety we copy the address of the Zulu index of dairy.
So if we copy the address if we copy the address of Eddie then it is called shallow copy okay.
So now let's see how to fix this problem
see at this line this arrow age is age.
Okay.
So this line is absolutely correct.
Wyatt is correct because age is then digital variable.
So the whole values being getting copied.
But in this case we are only copying the address.
Okay so at a somewhere else and we have only copied the address of the z index of that diary.
So this is called shallow copy.
This is
shallow copy.
I believe we should avoid shallow copy shallow copy means copy only the address of the error a copy
only the address of the area.
So I believe what we should do we should create a new Eddy and then copy the entirety.
Okay.
We should create a new area and then copy the entire content of dairy.
So that is called deep copy so shallow copies
copy only address
often zero index of the area and deep copy is
copy DNA battery not only their address we should copy the entire area.
Now let's see how we really do this
so let us first of all create a new area so that we can copy the entirety.
So we're creating the new areas in Texas like similar to this new cat.
What it will take it will take the size of the eddy size of the this name.
So for calculating the land we have and will function as to get alien.
We will pass it name.
It will give me the name it will give me the size for example the name is ABC.
So listing land as Tierra Leon will give me for ABC SGI really intrigued me food.
So I have to do plus one had Y plus one because we have to also copy the null corrector so for copying
the null corrector I am doing plus one here
okay.
So now we have created a new Eddy.
Now after creating the new Eddy we just have to copy the content.
So for copying the content we have inbuilt function string copy.
So first we have to give their destination and then we have to give the source so the sources name copy
so.
Copy this name into this name.
Copy the content.
Okay.
So content will get copied.
Null will also get copied.
Okay.
Along with content we will also copy the null and void this plus one here because we need one space.
We need one block for copying that also.
Okay.
Now what will happen in this scenario.
So the name dedicated in the mean that was containing there the 700 and the content was initially ABC
the annual then we will call the function we will call constructor.
So for calling the constructors and text will be like as one dot student.
First we will pass the H and then we will pass the name.
So name will be 700.
So the barometer H will contain 20.
And the barometer name will contain 700.
Okay.
So at this line what we are doing we are creating a new 80.
So this name this arrow name what it will do it will create a new Eddy.
And what will the size of the size of that is five Y five because ABC DST ideally will give me four
and null.
So this is four plus one.
So we will get a new every letter that does have those wrote indexes 800 and then we what we are doing
we are copying the content.
So e b c b and.
Okay.
Now this time this arrow name and this arrow name contains 800.
OK.
This item name contains 800.
So in the next step when we change the value of the name ABC when the value of name became ABC.
And then if we again print the value of S1 so that's when name contains at does 800.
So it will print from here.
It will print from here.
So what.
So I would probably be right.
Our output will come out to be right.
ABC.
The only.
OK.
So this is called deep copy.
So what is the procedure for deep copy deep copy space.
Make a new Adi which is same as the size of the this name.
And then copy the content.
Okay.
Make a new Eddie make a new Eddie.
And then copy the content.
So in this way we will solve our problem of shallow copy.
Now let's see.
Okay so let us compile this file
okay.
So this item name is string lint so if we're using the function string England we have to include.
I had a file called C string
Okay so we can see here is now the output is ABC the only.
Okay so this is called shallow copying deep copy.
So I hope you guys understand this.
Okay.
Thank you.