Hello everyone.
So in the last radio we discussed about copy constructor.
So in this video we will learn about copy assignment operator and destructor.
Okay so let's start with the copy assignment operator
so let us take an example.
Let's create an object first.
So student S1 ages 10 all numbers and we have another object.
We have another student as to ages 20.
And rule number is 200 so in this case and in this case in both the cases betterment tries to construct
it will be called so betterment based constructor is being getting called.
So what is this an add to the effect of this line is very simple.
Ages 10.
Rule number is hundred.
This is S1 and the effect of this line is also very simple.
Age will be 20 and rule number will be 200.
Now let's see what is our aim.
So our aim is very simple what I want to do I want to copy the value of S1 into S2.
Okay.
I want to copy the value of S1 into S2.
So each will become pen and roll number will become hundred so after creating that object I want to
copy the value of S1 into S2.
So the one simple way to do this is with the help of copy constructor
so after creating the object can I call copy constructor.
No see the objects are already being created the objects are already created.
So what I want is after creating the object I am copying the values of one object to enter object so
after creating the object can I call copy constructor.
The answer is no we cannot call copy constructor because we know for one object constructor is called
only once and that too at the time of creation of the object.
And for S2 parametric constructor has already been called.
Okay so what we have to do we have to copy the value explicitly so copy constructor will not work here.
We have to copy the value explicitly so for copying the value exquisitely we have to write two lines
one for copying the H and therefore copying the elements of our copying the age ages a private field.
So what we have to do we will use public function get principal function so as to not set age then but
really bad amateur will be as Ondaatje good age
and since the roll number is public so we can access public we can access public methods outside the
class so as to not remember it is s on daughter number.
Now the point is we do not need to write these two lines.
We do not need to write these two lines explicitly because we have an operator which will do our work
and that operator is called a copy assignment operator net operator is called copy assignment operator
so instead of writing these two lines I will write just one simple line as two equals S1 and that's
all as two equals S1.
So what do the effect of this line this line will say copy all the values of S1 into S2 copy all the
values of s 1 and 2 S2 and this equal operator is called copy assignment operator Okay so what is the
difference between copy constructor and copy assignment operator.
So there are differences very simple copy constructor what it does is it creates it creates the copy
at the time of creation of the object whereas word copy assignment operator does is copy something the
operator will be useful when both the objects are already created in the memory and then we have to
copy the values.
Okay so copy constructor Create the copy at the time of creation of the object and copy assignment operator
is used when both objects are already present in the memory and then you have to copy their values.
Okay.
So basically they do the same work there are the same book but like they work differently.
Okay so copy constructor will be called at the time of creation of the object to create copy or for
their object and copy assignment operator is useful when two objects are already present in the memory.
And then you have to copy the values.
Okay so enough of to read let's seeing or how this will look like.
So first Heller does create two objects so that I can use copy as an interpreter so as to render S1.
Let's say that ages 10 and the rule number is.
Hundred and let us create an other object student as to age is twenty and Roland Burris 200.
So what we have to do is one simple thing as two equals S1.
Okay so copy assignment operator is used here.
So copy assignment operator is also given us by the class as soon as we create the class copy assignment
operator as well to created.
Okay so what if we have a dynamic object then then within this index.
So to the industry is new student is just today and rule number is tendered
so this index will be quite similar status t is S1 copy the value of S1 and do S3 and similarly copy
the value of S3 and do as to Okay so that Sol
so this is how copy construct deadlocks Okay so copy the value of S1 and do as to copy the value of
S1 into history and then copy the value of S3 and do S2 and this operator is called copy assignment
operator so that's all for copy assignment operator we can create our own copy assignment operator also
but we will do it in the next to read use in the videos later on.
Okay so now now let us talk about destructor what is a destructor.
What is the purpose of district band what are the properties of restricted.
So as the name suggest what district devils do will be allocate the memory of the object it the allocates
memory of the object delegates memory of objects.
There are some properties of destructor for example it will have the same name as class name same name
as class name no return type
and it will not take any argument so annoying but I argument nine put barometers
so if you remember these three are also the property of the default constructor.
Default Constructor also put it is all these three properties.
So how can we differentiate between destructor and default constructor.
So what we will do we will use a special symbol called Taylor.
Okay.
So we will use the special symbolic like Delta to thus index the structure for Destructor is very similar.
To the constructor.
Student No input barometer and no return type.
So this is the structure of.
This is the structure ofa destructor.
Okay.
And the structure for default constructed we already know no return type 9 but argument nothing.
Okay.
So this is the structure of the default constructor so Destructor is automatically created as we create
that class.
Okay.
So just like the constructor district it is also called once in the lifetime of an object.
So a vendor destructively be called and destructively be when the scope of the object finish when scope
of object finish then destructively be called and when the constructor is called when we create object
Mr. constructor is called when we create object district it is called when this call of the object finish.
Okay Destructor is also called once in a lifetime and concept is also called once in the lifetime of
an object.
So just like constructor we can create our own destructor but we do not pass any argument again we cannot
pass any argument to the destructor.
So constructors can be many they can be many constructor in a program in a class but destructive will
only be one they will be only one district in a class.
Okay.
What happened is if we will create our own construct that then default constructor goes.
If we will create our own constructor default constructor goes away.
Similarly if we will create our destructor then and build construct it goes away and our destructively
becalmed Okay so let's say in code
special symbol district the name is same as the name of the class and that's all.
Let's bring something here.
So see out
Mr. Picard
Okay so now since we have written our own destructor so our destructively record.
Okay
our will be called inbuilt Destructor is not available to us any mode
our destructor will be called because we created our own destructor.
And then when destructor will not be called Okay so random destructive will be called when this call
of the object finish so we can see here we have three objects two objects has start tick and one objects
and dynamic so add this line.
Line number 15 the scope of the object finish.
So as soon as we reach line number 15 destructor will be called.
Okay destructively be.
As soon as with each line number 15.
Okay.
So what should be our output
so at this line construct the number three call which is a pyramid based constructor again paramedics
constructively called construct that recall again constructed recall.
Now when we reach this line output will be destructor called
again destructor called and again destructor called divide two times because we have three objects.
So three objects so two time destructor will be called for allocating the memory of the object.
Okay so this is our expected output.
And now let us in this file so first we have to read this file because we need changes to this file
and now we will run this file
so construct that record construct let's recall.
So this time constructor takes call for as one concept to take a while for us to construct that recall
for history.
Now Mr. Special Destructor is being getting called only two times Destructor is called only two times
but we have three objects.
So why Destructor is called two times why not two times.
So let us try to understand why two times and why not three.
So what is the effect of the slain agent Ben number is hundred and this is our student s one and let's
say this is 500 what is the effect of this line age will be twenty.
Rule number is two hundred and student two and let's say this is six hundred what is the effect of this
line.
So we are creating the object dynamically so we have a point that is three and then in the heap memory
we have ages today and rule number is 200
okay let's say it's Airbus's seven.
Okay and then we are doing some stuff okay so at this line what will happen.
Constructor should be called.
So what constructively do it will destroy the memory it will they allocate the object.
So only these parts will be deleted on distracting memory will be deleted only starting objects will
be deleted.
Okay so this is not a static object.
So constructor will not be calling for this Saudi district will not be called for this district will
only destroy this object and this object and since this is not object as trees art object destructive
of history will not recall.
Okay so our destructor will destroy only these two objects destructively be coiled for these two and
not for S3 because this is simple appoint a variable it is not an object.
So what we have to do it is our responsibility we have to delete this class we have to delete this object
okay it is our responsibility to delete this object.
Now how can we delete this object.
So in the dynamic memory location we have studied how to delete dynamic memory using a keyword called
delete.
So we will write delete s.t. word delete activity do it will come here and as soon as we will come here
destructor for this object will be called destructor for as tree will be called and this memory will
get deleted this memory will get deleted.
Okay so always remember always remember if there is a dynamic memory allocation then binding memory
allocation has to be deleted by ourself.
Okay.
They are not automatically deleted only static memories static objects they are deleted automatically
for dynamic memory to be allocate we have to use the keyword delete.
Now let us destroy object as tree or delete tree now destructively recorded.
Okay
see District based being getting called three times okay.
So this is all about destructor.
Thank you.