1
00:00:02,560 --> 00:00:03,400
Hello everyone.

2
00:00:03,400 --> 00:00:07,460
So in the last radio we discussed about copy constructor.

3
00:00:07,540 --> 00:00:12,250
So in this video we will learn about copy assignment operator and destructor.

4
00:00:12,320 --> 00:00:15,190
Okay so let's start with the copy assignment operator

5
00:00:18,200 --> 00:00:19,920
so let us take an example.

6
00:00:20,210 --> 00:00:22,500
Let's create an object first.

7
00:00:22,730 --> 00:00:29,010
So student S1 ages 10 all numbers and we have another object.

8
00:00:29,030 --> 00:00:33,170
We have another student as to ages 20.

9
00:00:33,200 --> 00:00:41,330
And rule number is 200 so in this case and in this case in both the cases betterment tries to construct

10
00:00:41,330 --> 00:00:46,660
it will be called so betterment based constructor is being getting called.

11
00:00:46,660 --> 00:00:50,870
So what is this an add to the effect of this line is very simple.

12
00:00:51,010 --> 00:00:52,920
Ages 10.

13
00:00:53,200 --> 00:00:57,070
Rule number is hundred.

14
00:00:57,180 --> 00:01:00,320
This is S1 and the effect of this line is also very simple.

15
00:01:00,360 --> 00:01:06,850
Age will be 20 and rule number will be 200.

16
00:01:06,860 --> 00:01:09,150
Now let's see what is our aim.

17
00:01:09,230 --> 00:01:15,920
So our aim is very simple what I want to do I want to copy the value of S1 into S2.

18
00:01:15,990 --> 00:01:16,540
Okay.

19
00:01:16,550 --> 00:01:18,840
I want to copy the value of S1 into S2.

20
00:01:18,920 --> 00:01:26,520
So each will become pen and roll number will become hundred so after creating that object I want to

21
00:01:26,520 --> 00:01:28,610
copy the value of S1 into S2.

22
00:01:28,740 --> 00:01:33,210
So the one simple way to do this is with the help of copy constructor

23
00:01:38,270 --> 00:01:41,930
so after creating the object can I call copy constructor.

24
00:01:42,020 --> 00:01:48,080
No see the objects are already being created the objects are already created.

25
00:01:48,080 --> 00:01:56,330
So what I want is after creating the object I am copying the values of one object to enter object so

26
00:01:56,390 --> 00:01:59,930
after creating the object can I call copy constructor.

27
00:01:59,930 --> 00:02:06,140
The answer is no we cannot call copy constructor because we know for one object constructor is called

28
00:02:06,290 --> 00:02:10,250
only once and that too at the time of creation of the object.

29
00:02:10,430 --> 00:02:15,260
And for S2 parametric constructor has already been called.

30
00:02:15,320 --> 00:02:20,900
Okay so what we have to do we have to copy the value explicitly so copy constructor will not work here.

31
00:02:21,050 --> 00:02:26,450
We have to copy the value explicitly so for copying the value exquisitely we have to write two lines

32
00:02:26,810 --> 00:02:32,500
one for copying the H and therefore copying the elements of our copying the age ages a private field.

33
00:02:32,510 --> 00:02:41,440
So what we have to do we will use public function get principal function so as to not set age then but

34
00:02:41,480 --> 00:02:43,750
really bad amateur will be as Ondaatje good age

35
00:02:46,730 --> 00:02:52,220
and since the roll number is public so we can access public we can access public methods outside the

36
00:02:52,220 --> 00:02:57,610
class so as to not remember it is s on daughter number.

37
00:02:57,870 --> 00:03:01,180
Now the point is we do not need to write these two lines.

38
00:03:01,650 --> 00:03:06,720
We do not need to write these two lines explicitly because we have an operator which will do our work

39
00:03:06,780 --> 00:03:13,610
and that operator is called a copy assignment operator net operator is called copy assignment operator

40
00:03:15,010 --> 00:03:23,040
so instead of writing these two lines I will write just one simple line as two equals S1 and that's

41
00:03:23,040 --> 00:03:25,850
all as two equals S1.

42
00:03:25,850 --> 00:03:32,930
So what do the effect of this line this line will say copy all the values of S1 into S2 copy all the

43
00:03:32,930 --> 00:03:40,550
values of s 1 and 2 S2 and this equal operator is called copy assignment operator Okay so what is the

44
00:03:40,550 --> 00:03:44,200
difference between copy constructor and copy assignment operator.

45
00:03:44,390 --> 00:03:52,190
So there are differences very simple copy constructor what it does is it creates it creates the copy

46
00:03:52,280 --> 00:03:59,630
at the time of creation of the object whereas word copy assignment operator does is copy something the

47
00:03:59,630 --> 00:04:04,140
operator will be useful when both the objects are already created in the memory and then we have to

48
00:04:04,140 --> 00:04:05,390
copy the values.

49
00:04:05,580 --> 00:04:11,880
Okay so copy constructor Create the copy at the time of creation of the object and copy assignment operator

50
00:04:12,150 --> 00:04:17,680
is used when both objects are already present in the memory and then you have to copy their values.

51
00:04:17,760 --> 00:04:18,020
Okay.

52
00:04:18,030 --> 00:04:26,070
So basically they do the same work there are the same book but like they work differently.

53
00:04:26,100 --> 00:04:31,280
Okay so copy constructor will be called at the time of creation of the object to create copy or for

54
00:04:31,280 --> 00:04:36,490
their object and copy assignment operator is useful when two objects are already present in the memory.

55
00:04:36,540 --> 00:04:38,430
And then you have to copy the values.

56
00:04:38,430 --> 00:04:43,700
Okay so enough of to read let's seeing or how this will look like.

57
00:04:43,770 --> 00:04:49,700
So first Heller does create two objects so that I can use copy as an interpreter so as to render S1.

58
00:04:49,710 --> 00:04:51,970
Let's say that ages 10 and the rule number is.

59
00:04:51,990 --> 00:05:01,240
Hundred and let us create an other object student as to age is twenty and Roland Burris 200.

60
00:05:01,470 --> 00:05:06,280
So what we have to do is one simple thing as two equals S1.

61
00:05:07,140 --> 00:05:11,780
Okay so copy assignment operator is used here.

62
00:05:12,090 --> 00:05:17,520
So copy assignment operator is also given us by the class as soon as we create the class copy assignment

63
00:05:17,520 --> 00:05:19,760
operator as well to created.

64
00:05:19,970 --> 00:05:25,370
Okay so what if we have a dynamic object then then within this index.

65
00:05:25,400 --> 00:05:33,760
So to the industry is new student is just today and rule number is tendered

66
00:05:36,450 --> 00:05:46,500
so this index will be quite similar status t is S1 copy the value of S1 and do S3 and similarly copy

67
00:05:46,500 --> 00:05:51,610
the value of S3 and do as to Okay so that Sol

68
00:05:56,940 --> 00:06:06,020
so this is how copy construct deadlocks Okay so copy the value of S1 and do as to copy the value of

69
00:06:06,050 --> 00:06:12,710
S1 into history and then copy the value of S3 and do S2 and this operator is called copy assignment

70
00:06:12,710 --> 00:06:19,980
operator so that's all for copy assignment operator we can create our own copy assignment operator also

71
00:06:20,040 --> 00:06:24,540
but we will do it in the next to read use in the videos later on.

72
00:06:24,540 --> 00:06:32,800
Okay so now now let us talk about destructor what is a destructor.

73
00:06:32,800 --> 00:06:37,270
What is the purpose of district band what are the properties of restricted.

74
00:06:37,270 --> 00:06:46,720
So as the name suggest what district devils do will be allocate the memory of the object it the allocates

75
00:06:48,690 --> 00:06:53,460
memory of the object delegates memory of objects.

76
00:06:53,940 --> 00:07:01,120
There are some properties of destructor for example it will have the same name as class name same name

77
00:07:01,450 --> 00:07:05,940
as class name no return type

78
00:07:11,500 --> 00:07:18,260
and it will not take any argument so annoying but I argument nine put barometers

79
00:07:21,450 --> 00:07:26,310
so if you remember these three are also the property of the default constructor.

80
00:07:26,640 --> 00:07:30,430
Default Constructor also put it is all these three properties.

81
00:07:30,570 --> 00:07:36,540
So how can we differentiate between destructor and default constructor.

82
00:07:36,540 --> 00:07:40,400
So what we will do we will use a special symbol called Taylor.

83
00:07:41,130 --> 00:07:41,680
Okay.

84
00:07:41,700 --> 00:07:48,010
So we will use the special symbolic like Delta to thus index the structure for Destructor is very similar.

85
00:07:48,780 --> 00:07:50,240
To the constructor.

86
00:07:50,240 --> 00:07:53,650
Student No input barometer and no return type.

87
00:07:54,350 --> 00:07:56,780
So this is the structure of.

88
00:07:56,960 --> 00:07:59,500
This is the structure ofa destructor.

89
00:08:00,230 --> 00:08:00,530
Okay.

90
00:08:00,530 --> 00:08:07,570
And the structure for default constructed we already know no return type 9 but argument nothing.

91
00:08:07,630 --> 00:08:08,180
Okay.

92
00:08:08,200 --> 00:08:15,490
So this is the structure of the default constructor so Destructor is automatically created as we create

93
00:08:15,490 --> 00:08:16,030
that class.

94
00:08:16,620 --> 00:08:17,100
Okay.

95
00:08:17,160 --> 00:08:22,410
So just like the constructor district it is also called once in the lifetime of an object.

96
00:08:22,410 --> 00:08:27,900
So a vendor destructively be called and destructively be when the scope of the object finish when scope

97
00:08:27,900 --> 00:08:35,550
of object finish then destructively be called and when the constructor is called when we create object

98
00:08:37,190 --> 00:08:42,470
Mr. constructor is called when we create object district it is called when this call of the object finish.

99
00:08:42,970 --> 00:08:48,770
Okay Destructor is also called once in a lifetime and concept is also called once in the lifetime of

100
00:08:48,770 --> 00:08:50,330
an object.

101
00:08:50,420 --> 00:08:56,840
So just like constructor we can create our own destructor but we do not pass any argument again we cannot

102
00:08:56,840 --> 00:08:59,360
pass any argument to the destructor.

103
00:08:59,360 --> 00:09:07,310
So constructors can be many they can be many constructor in a program in a class but destructive will

104
00:09:07,310 --> 00:09:13,000
only be one they will be only one district in a class.

105
00:09:13,150 --> 00:09:13,690
Okay.

106
00:09:13,690 --> 00:09:18,370
What happened is if we will create our own construct that then default constructor goes.

107
00:09:19,180 --> 00:09:21,940
If we will create our own constructor default constructor goes away.

108
00:09:21,940 --> 00:09:29,490
Similarly if we will create our destructor then and build construct it goes away and our destructively

109
00:09:29,530 --> 00:09:32,620
becalmed Okay so let's say in code

110
00:09:45,530 --> 00:09:53,460
special symbol district the name is same as the name of the class and that's all.

111
00:09:53,460 --> 00:09:54,890
Let's bring something here.

112
00:09:55,110 --> 00:09:55,830
So see out

113
00:09:58,670 --> 00:09:59,510
Mr. Picard

114
00:10:05,360 --> 00:10:12,380
Okay so now since we have written our own destructor so our destructively record.

115
00:10:12,470 --> 00:10:12,760
Okay

116
00:10:16,390 --> 00:10:20,600
our will be called inbuilt Destructor is not available to us any mode

117
00:10:31,100 --> 00:10:35,620
our destructor will be called because we created our own destructor.

118
00:10:35,660 --> 00:10:41,210
And then when destructor will not be called Okay so random destructive will be called when this call

119
00:10:41,210 --> 00:10:50,010
of the object finish so we can see here we have three objects two objects has start tick and one objects

120
00:10:50,040 --> 00:10:52,680
and dynamic so add this line.

121
00:10:52,680 --> 00:10:55,270
Line number 15 the scope of the object finish.

122
00:10:55,320 --> 00:10:59,090
So as soon as we reach line number 15 destructor will be called.

123
00:10:59,460 --> 00:11:00,950
Okay destructively be.

124
00:11:01,040 --> 00:11:03,620
As soon as with each line number 15.

125
00:11:03,870 --> 00:11:04,340
Okay.

126
00:11:04,440 --> 00:11:05,750
So what should be our output

127
00:11:11,620 --> 00:11:17,080
so at this line construct the number three call which is a pyramid based constructor again paramedics

128
00:11:17,080 --> 00:11:20,590
constructively called construct that recall again constructed recall.

129
00:11:20,890 --> 00:11:24,790
Now when we reach this line output will be destructor called

130
00:11:27,430 --> 00:11:33,840
again destructor called and again destructor called divide two times because we have three objects.

131
00:11:34,120 --> 00:11:38,620
So three objects so two time destructor will be called for allocating the memory of the object.

132
00:11:38,950 --> 00:11:41,010
Okay so this is our expected output.

133
00:11:41,010 --> 00:11:46,760
And now let us in this file so first we have to read this file because we need changes to this file

134
00:11:46,830 --> 00:11:48,290
and now we will run this file

135
00:11:53,480 --> 00:11:56,990
so construct that record construct let's recall.

136
00:11:56,990 --> 00:12:02,420
So this time constructor takes call for as one concept to take a while for us to construct that recall

137
00:12:02,570 --> 00:12:03,590
for history.

138
00:12:03,620 --> 00:12:09,800
Now Mr. Special Destructor is being getting called only two times Destructor is called only two times

139
00:12:09,830 --> 00:12:11,380
but we have three objects.

140
00:12:11,450 --> 00:12:14,960
So why Destructor is called two times why not two times.

141
00:12:14,960 --> 00:12:26,850
So let us try to understand why two times and why not three.

142
00:12:26,860 --> 00:12:36,750
So what is the effect of the slain agent Ben number is hundred and this is our student s one and let's

143
00:12:36,750 --> 00:12:44,250
say this is 500 what is the effect of this line age will be twenty.

144
00:12:44,250 --> 00:12:53,960
Rule number is two hundred and student two and let's say this is six hundred what is the effect of this

145
00:12:53,960 --> 00:12:54,310
line.

146
00:12:54,320 --> 00:13:01,640
So we are creating the object dynamically so we have a point that is three and then in the heap memory

147
00:13:01,670 --> 00:13:06,440
we have ages today and rule number is 200

148
00:13:09,360 --> 00:13:12,080
okay let's say it's Airbus's seven.

149
00:13:12,660 --> 00:13:17,200
Okay and then we are doing some stuff okay so at this line what will happen.

150
00:13:17,230 --> 00:13:18,550
Constructor should be called.

151
00:13:18,600 --> 00:13:24,290
So what constructively do it will destroy the memory it will they allocate the object.

152
00:13:24,900 --> 00:13:31,860
So only these parts will be deleted on distracting memory will be deleted only starting objects will

153
00:13:31,860 --> 00:13:33,470
be deleted.

154
00:13:33,480 --> 00:13:35,760
Okay so this is not a static object.

155
00:13:35,760 --> 00:13:42,240
So constructor will not be calling for this Saudi district will not be called for this district will

156
00:13:42,240 --> 00:13:49,420
only destroy this object and this object and since this is not object as trees art object destructive

157
00:13:49,430 --> 00:13:51,010
of history will not recall.

158
00:13:51,330 --> 00:14:00,510
Okay so our destructor will destroy only these two objects destructively be coiled for these two and

159
00:14:00,510 --> 00:14:06,090
not for S3 because this is simple appoint a variable it is not an object.

160
00:14:06,180 --> 00:14:12,210
So what we have to do it is our responsibility we have to delete this class we have to delete this object

161
00:14:12,450 --> 00:14:15,530
okay it is our responsibility to delete this object.

162
00:14:15,540 --> 00:14:17,700
Now how can we delete this object.

163
00:14:17,700 --> 00:14:24,460
So in the dynamic memory location we have studied how to delete dynamic memory using a keyword called

164
00:14:24,480 --> 00:14:25,620
delete.

165
00:14:25,620 --> 00:14:32,830
So we will write delete s.t. word delete activity do it will come here and as soon as we will come here

166
00:14:33,670 --> 00:14:39,760
destructor for this object will be called destructor for as tree will be called and this memory will

167
00:14:39,760 --> 00:14:43,180
get deleted this memory will get deleted.

168
00:14:43,180 --> 00:14:49,480
Okay so always remember always remember if there is a dynamic memory allocation then binding memory

169
00:14:49,480 --> 00:14:52,050
allocation has to be deleted by ourself.

170
00:14:52,240 --> 00:14:52,540
Okay.

171
00:14:52,540 --> 00:14:57,520
They are not automatically deleted only static memories static objects they are deleted automatically

172
00:14:57,790 --> 00:15:07,020
for dynamic memory to be allocate we have to use the keyword delete.

173
00:15:07,380 --> 00:15:13,520
Now let us destroy object as tree or delete tree now destructively recorded.

174
00:15:13,620 --> 00:15:13,910
Okay

175
00:15:17,920 --> 00:15:23,530
see District based being getting called three times okay.

176
00:15:23,770 --> 00:15:26,420
So this is all about destructor.

177
00:15:26,680 --> 00:15:27,070
Thank you.
