Hello everyone.
So in this video we will create our own copy constructor.
So before writing our own copy constructor let us analyze again how in built copy constructor works.
Okay.
So let us comment out discord
okay.
So let us make a copy first.
So we have a student as to and as to his copy of S1 so at this line copy constructor will be called.
Okay so a copy constructor called
Okay so now let us change the name of S2.
So let us change it since you wrote the index to X
okay.
So the named properties are private property.
So to exist name let us make name is public.
Okay.
We can also use good enter the function.
But for simplicity I made the name variable a public field.
Okay.
So after changing the name for as to student S2.
Now let us print s Fernandez too.
So as S1 not display function and similarly S2 dart display.
Okay so no one here does in this file.
Okay so let's send this file
so not display Okay so completion edit
see both attorneys to both the names are X BCE which is wrong.
Okay so the student one name also word changed.
If I changed the name of the student to so let us try to analyze what is happening here.
Ideally the name for a student I think should be ABC but it is getting changed to X obesity.
Okay so let's try to understand why why it is happening
so let's say this name contains that the seven ended and we have a b c d and then so that beat us off
is seven.
Okay now when we call paramedics constructor what will happen.
So age will become 20 and since we have made deep copy so the name will contain and address the 800.
So this is our S1 and 800 is the address of a b c d.
And then an so we.
Can you stay the course in deep copy we make a new area.
And then this name will contain their address.
800 so this is the content here.
Okay.
We copied this content now what will happen at this line.
Copy constructively recalled so I'd copy constructor will do.
It will create a new object with the name as to so each will get copied so each will become 20 and the
name will also get copied the name will contain 800.
So what is happening here is the name of S1 is pointing towards this memory location and the name of
S2 is also pointing towards this remote location.
So basically what is happening here is copy constructor makes shallow copy.
Ok this fellow copy is being made shallow copy is created by inbuilt copy constructor
so always remember our inbuilt court because our inbuilt copy constructor always makes shallow copy.
Okay so at this land line number be what we are trying to do we are changing the zero to next to x.
So go to 800 number location and then change the zero to next to x.
So then we are printing and as for not displaying astronaut display.
So I started our display will print this one so x BCG and similarly astronaut display will go to address
800 and and we will print.
So that's why X BSD okay.
So that's why SBC didn't explicitly I'd bring 34 S1 and S2 okay because I went and made copy.
That's a big read shallow copy.
So whenever we have dynamic Eddie as our data member as our property so we should always create our
own copy goes to third to a wide shallow copy.
So when we will create our own copy constructor we will do beep copy.
Okay so it is important here it is.
It is very important here for us to create our own copy construct because we do not want shallow copy.
We want deep copy and inbuilt copy precludes shallow copy.
That's why we have to create our own copy construct bit here.
So it is mandatory here.
It is not a choice.
It is amended reader's compulsion.
Okay.
We have to.
We have to create copy construct but we have to create our own copy construct that here.
Okay.
So creating the copy I stepped over to the structure so the structure is first of all.
Same name as the last name same name as the last name.
What it will take it will take us to an end as input.
So student s and then what it will do it will copy the age Endy name.
So this A2 age is as dotage and this ETL name is as not name.
Okay so this is shallow copy this is shallow copy we are only copying the address.
So this is how inbuilt copy the script that is written.
Okay so this is in made copy constructor This is the code for and built copy constructor but since we
have to create a deep copy since we have to create the copy what we will do we will make a new area
and then we will copy the content so this Edwin name is New get now what would it be the land string
lint off s dart name.
And we have to loop plus one by plus one because we have to copy Mello also and then we will use string
copy function state copy function we will give this ATL name and start name.
Okay so in this way we will create our own DB copy.
So this is the code for deep copy
deep copy.
Okay.
Now what will happen.
So this is our name from the mean.
This convinced that this seven hundred and I will lose a b c d and then
now we have our S1 so S1 contains h to be 20 and named 2B 800 y 800 because we created our new Eddie
and then we copied the content and the address of this new ad is 800.
Okay.
So the name of S1 points towards this primary location.
Now let us talk about the copy constructor so we'll copy constructor we are creating a new object with
the name S2.
So first of all we will copy that each.
So each will become 20 and then what we are trying to do here is we are creating a new edit.
So when you add it will get created the size will be 5 and then what we are trying to do we are copying
the content.
So a b c d and then let's say the address is 900.
So this time the name of S2 is containing the address nine hundred.
So if I will change the name of S2 If I will make it s x then there will be no effect on the name of
S1 so when eventually write as front our display then ABC the printed there then we will write astronaut
display.
Then explicitly got printed.
Okay so this is deep copy and how we will call our constructor student as to in Port argument S1 so
this has to be going this and this S1 will be as a barometer.
Okay.
This S1 will be as a barometer.
Okay so according to me everything seems fine but everything is not fine.
Okay.
Why does not fine.
There is a very big mistake that we are doing here is let's try to understand what is the mistake that
we are that I am talking about.
So what happened is when you light student S2 S1 so this will go in this function of the copy constructor
and S1 will come here.
So a copy will be created.
So over so we can write.
We can write this line is student s is main dot S1 okay S1 from the mean is being incompetent to student
s and in the previous we do as we have learned whenever we write student s three close S1.
So although this is copy assignment operator.
But we know copy constructor will be called here.
We have already discussed about it in the previous videos.
So copy constructor will be called here.
Okay so at this line copy constructor is being getting called Okay by a copy constructor is being recalled
because we are passing values by value.
Okay.
This is passed by values and although this is not correct this is not correct.
Why.
This is not correct.
Why this is not correct because this is not valid because as soon as we create our own copy constructor
and will copy construct is not available to us anymore.
Just like as soon as we write our own default constructor that inbuilt default constructor goes away.
Likewise as soon as we create our own copy constructor the inbuilt copy constructor is not available
to us anymore.
So what really happened at this line we are calling copy constructor so in bulk does not available so
our copy constructively regardless.
So this will call our own copy constructor okay and then we will come to this argument and as we come
to this argument copy construct every get called to copy the values from S1 to s..
Okay so in this way infinite loop will be created in finite loop will be created.
Why.
Because first of all copy constructively but call here to copy the values from S1 to s and then then
we will reach this argument then a game copy constructor will be called and our copy can certainly be
called because we have write our own copy constructors when will construct but is not available to us
anymore so why.
That's why infinite loop is here so why infinite loop because we are talking about creating copy constructor
and in the argument of that copy constructor we need a copy constructor okay I'm repeating myself.
So we're creating a copy constructor and to create copy constructor we need to copy constructor in the
argument to copy the values okay.
So what we have to do is we have to avoid calling this copy constructor and how can we avoid calling
this copy constructor by using passed by reference.
Okay.
So if you look here there is no need to pass by value.
Okay.
Vertical copy gastric but simply can pass biodefense because we are only accessing its properties.
We are not doing anything extra.
We are only accessing the properties of the student is so wide to call copy constructor.
Okay so what will happen.
We can also optimize it how we can meet.
We can make it a constant reference.
We have studied about this.
Like if someone do this as soon as daughter age is minus one.
So I want to avoid.
I want to avoid this line.
I want to avoid this a legal changes so to avoid this all legal changes begin but we can write constraint
here.
Okay.
So what is happening here is
the meaning of this line is very simple student constant reference variable S1 is that S1 so this is
is the mean.
This one from mean.
Okay so this s is an offense variable the westward and constant.
It is an offense variable to us and constraint so this time copy constructively logbook code.
Okay so our inbuilt copy construct but also take argument in this session then we'll copy constructor
also takes arguments in this section.
But the problem with built copy constructor is then built a copy constructively make shallow copy but
we want deep copy since we want the copy we have to write our own copy constructor okay.
Okay sir.
Okay.
So I hope you understood this.
Thank you.