Hello everyone in this session we will learn more about constructors in the last session.
We learned what our constructors and how they are called.
When we create an object.
So we also learn that as soon as we create a class our default constructor is created and we can use
the default constructor for initializing the object we can create our own constructor also.
And as soon as we create our own constructor and build constructor is not visible to us anymore.
Okay.
So what happened is as soon as we create our class as soon as we create our class we get a few functions
for free we will get a few functions for free so no one is default constructor.
The second function that we will get for fee is called copy constructor.
The third one is copy assignment operator
and the fourth one is called our district that
okay.
So as soon as we create a class we will get a few functions for free.
We have already started about a default constructor and what it does in this video.
Our main focus will be on copy constructor and in the next videos we will learn about these two copy
assignment operator and district.
Okay so let's start to this topic which is a copy constructor.
So what is a copy constructor.
Let us first understand with the help of an example let us create two objects a student s one and let's
say that all somebody stone ages 10 and somebody is hundred.
So what will happen.
Batman tries to construct a little bit called okay.
Now let us create that object.
Student as to in this case default constructor will be called Okay now I let him Our aim is very simple.
I want to copy the value of S1 and to S2.
Okay.
So our aim is very simple.
Copy the value of S went into S2.
So what I want is is to age should be 10 and as to the number should be a hundred.
Okay so how can we do this.
So there is very simple we we will do like this is to dot set age and we will give input as as one dart
get age
and similarly S2 doctoral but is S1 daughter number.
See if we will write as to what age is S1 Dark Age this line is wrong way because age is a private field
age is a private field so if we want to do something if we want to do something with age we have to
use public functions set age and get age.
Okay rule number is public.
So it will work fine okay.
So if I want to copy the value of S1 into S2 I have to write these two lines I have to write these two
lines explicitly okay.
So we do not need to do this work explicitly.
We have a constructor for doing this work and that constructor is called a copy constructor okay.
So what copy constructor does is it creates the copy it creates an object which is copy of other object
which is copy of either object.
So this is the work of a copy concept.
So let's let's take an example
let's create an object first student S1 ages 10 the numbers ended.
Now I want to make a copy of student S1 so the syntax is very simple.
Student S2 and we will give S1 s better.
Okay so what will happen the properties of the values of S1 will get copied into S2.
So in this case better made price to constructively regard which we have created in the last radios
and in this case copy constructor will be called Okay which will copy the value off as 1 and 2 S2.
So how would this line will be interpreted.
This line will internally look like this as one dot sorry is to not have a function student and input
will be as one parameter will be S1.
Okay so S1 is of type student s and is a barometer of type student.
Now we have not created any constructor which takes one argument of type student as input Okay we have
not made any constructor which takes one argument of type student but we are able to use it.
How because as soon as we create a class copy construct base also created.
Okay so what will the effect of these two lines.
So the effect of this line is very simple.
Age will be 10 and rule number will be entered.
This is S1 and the effect of this line ages then rule number is hundred and this is Student 2 Okay now
one thing here is since copy construct basic concept.
It will be called only at the time of the creation of the object.
Okay now let's see in called
let's make an object string.
That's 1 8 just in really embodies and read.
So copy barometer based constructor will be guarded
okay.
We have created this Panama imitates constructor and let's spend something so we'll see out S1
and let's call display function.
So S1 dart display okay.
So now make an object this object will be the copy of s an object and let's print this object onto the
student as to
as to not display
so in this lane copy construct a little bit cold.
And this court begins to produce in bold constructed.
OK so this better imitates construct that is user defined Vermeer discussed it it is not so this bad
imitates concept but is not given by class.
We created this constructor Okay so user defined
Okay so now let us turn this file
so the value of this is getting printed paramedics construct based called construct with regard the
value of age for student s on the stand and for us to do is also ten hundred and hundred the value from
Lumberton also same.
So one thing here is so one thing to note here is there is no constructor called a message for object
as true because for us to copy constructor is called and we have not made our own copy construct but
we are using the intended copy sector and in that inbuilt copy construct that there is no see out message.
So that's why it see our message will appear for object as to so in this way we can use copy constructor
to create copy of the object.
Okay.
So in this way we can use copy constructor to create copies of the object.
Now here we have created objects statically.
Now let's see what lesson it takes for creating objects creating copy of objects dynamically.
So let us first create a object.
So student history is new student and let's call betterment based constructor ages 20 and almost 200
okay.
Now let's bring those three
as three add or display
okay.
Now let us make copies of s.t. so student as for S3 now is it right.
Is this index right.
No it is wrong.
Why.
Because Estes a pointer and I want values.
So we have to use be defense operator.
Now student s what is the copy of S3.
Okay now we can create dynamic copy also how so a student is five is no student and start history.
Okay.
We have dos the defense operated and what if we want to create the copy of static object so the index
will be student at 6 is no student and start to go object S1 Okay so this is how the copy constructor
works.
So a student S4 is the copy of S3 S5 is the copy of S3.
Okay so we are copying the values of S3 into S5.
Similarly the values of S1 is being copied in two S6.
So this is all about copy constructor
okay.
So now we know what is a default constructor and what is a copy constructor.
Next we do we will learn about copy assignment operator okay.
Thank you.