1
00:00:03,600 --> 00:00:04,290
Hello everyone.

2
00:00:04,320 --> 00:00:07,540
So in this video we will create our own copy constructor.

3
00:00:07,650 --> 00:00:13,280
So before writing our own copy constructor let us analyze again how in built copy constructor works.

4
00:00:13,350 --> 00:00:13,810
Okay.

5
00:00:17,850 --> 00:00:19,650
So let us comment out discord

6
00:00:26,200 --> 00:00:26,450
okay.

7
00:00:26,470 --> 00:00:28,310
So let us make a copy first.

8
00:00:29,450 --> 00:00:36,750
So we have a student as to and as to his copy of S1 so at this line copy constructor will be called.

9
00:00:36,770 --> 00:00:39,170
Okay so a copy constructor called

10
00:00:45,640 --> 00:00:51,450
Okay so now let us change the name of S2.

11
00:00:51,550 --> 00:00:55,630
So let us change it since you wrote the index to X

12
00:00:58,770 --> 00:00:59,020
okay.

13
00:00:59,040 --> 00:01:01,670
So the named properties are private property.

14
00:01:01,710 --> 00:01:05,090
So to exist name let us make name is public.

15
00:01:05,910 --> 00:01:06,390
Okay.

16
00:01:12,130 --> 00:01:14,290
We can also use good enter the function.

17
00:01:14,290 --> 00:01:18,280
But for simplicity I made the name variable a public field.

18
00:01:18,990 --> 00:01:19,480
Okay.

19
00:01:19,630 --> 00:01:22,880
So after changing the name for as to student S2.

20
00:01:23,020 --> 00:01:24,920
Now let us print s Fernandez too.

21
00:01:25,240 --> 00:01:31,640
So as S1 not display function and similarly S2 dart display.

22
00:01:35,830 --> 00:01:37,950
Okay so no one here does in this file.

23
00:01:40,200 --> 00:01:41,490
Okay so let's send this file

24
00:01:45,820 --> 00:01:50,360
so not display Okay so completion edit

25
00:01:55,510 --> 00:02:02,300
see both attorneys to both the names are X BCE which is wrong.

26
00:02:02,320 --> 00:02:04,950
Okay so the student one name also word changed.

27
00:02:04,990 --> 00:02:09,440
If I changed the name of the student to so let us try to analyze what is happening here.

28
00:02:09,520 --> 00:02:15,810
Ideally the name for a student I think should be ABC but it is getting changed to X obesity.

29
00:02:15,930 --> 00:02:19,300
Okay so let's try to understand why why it is happening

30
00:02:27,330 --> 00:02:37,250
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

31
00:02:37,310 --> 00:02:39,350
is seven.

32
00:02:39,720 --> 00:02:44,330
Okay now when we call paramedics constructor what will happen.

33
00:02:44,330 --> 00:02:50,970
So age will become 20 and since we have made deep copy so the name will contain and address the 800.

34
00:02:51,290 --> 00:02:56,290
So this is our S1 and 800 is the address of a b c d.

35
00:02:57,080 --> 00:02:59,790
And then an so we.

36
00:02:59,800 --> 00:03:02,270
Can you stay the course in deep copy we make a new area.

37
00:03:02,380 --> 00:03:05,280
And then this name will contain their address.

38
00:03:05,380 --> 00:03:07,510
800 so this is the content here.

39
00:03:07,540 --> 00:03:07,800
Okay.

40
00:03:07,800 --> 00:03:12,980
We copied this content now what will happen at this line.

41
00:03:13,150 --> 00:03:16,720
Copy constructively recalled so I'd copy constructor will do.

42
00:03:16,810 --> 00:03:23,620
It will create a new object with the name as to so each will get copied so each will become 20 and the

43
00:03:23,620 --> 00:03:26,910
name will also get copied the name will contain 800.

44
00:03:27,220 --> 00:03:32,560
So what is happening here is the name of S1 is pointing towards this memory location and the name of

45
00:03:32,560 --> 00:03:35,620
S2 is also pointing towards this remote location.

46
00:03:35,620 --> 00:03:40,350
So basically what is happening here is copy constructor makes shallow copy.

47
00:03:40,400 --> 00:03:46,270
Ok this fellow copy is being made shallow copy is created by inbuilt copy constructor

48
00:03:50,450 --> 00:03:57,120
so always remember our inbuilt court because our inbuilt copy constructor always makes shallow copy.

49
00:03:57,160 --> 00:04:02,980
Okay so at this land line number be what we are trying to do we are changing the zero to next to x.

50
00:04:03,020 --> 00:04:08,770
So go to 800 number location and then change the zero to next to x.

51
00:04:08,900 --> 00:04:12,470
So then we are printing and as for not displaying astronaut display.

52
00:04:12,470 --> 00:04:19,070
So I started our display will print this one so x BCG and similarly astronaut display will go to address

53
00:04:19,110 --> 00:04:21,210
800 and and we will print.

54
00:04:21,290 --> 00:04:23,740
So that's why X BSD okay.

55
00:04:23,780 --> 00:04:29,760
So that's why SBC didn't explicitly I'd bring 34 S1 and S2 okay because I went and made copy.

56
00:04:29,770 --> 00:04:31,850
That's a big read shallow copy.

57
00:04:31,850 --> 00:04:37,610
So whenever we have dynamic Eddie as our data member as our property so we should always create our

58
00:04:37,610 --> 00:04:40,060
own copy goes to third to a wide shallow copy.

59
00:04:40,100 --> 00:04:43,400
So when we will create our own copy constructor we will do beep copy.

60
00:04:43,520 --> 00:04:46,300
Okay so it is important here it is.

61
00:04:47,150 --> 00:04:52,700
It is very important here for us to create our own copy construct because we do not want shallow copy.

62
00:04:52,700 --> 00:04:56,710
We want deep copy and inbuilt copy precludes shallow copy.

63
00:04:56,720 --> 00:04:59,490
That's why we have to create our own copy construct bit here.

64
00:04:59,510 --> 00:05:00,640
So it is mandatory here.

65
00:05:00,650 --> 00:05:01,920
It is not a choice.

66
00:05:01,970 --> 00:05:04,050
It is amended reader's compulsion.

67
00:05:04,100 --> 00:05:04,570
Okay.

68
00:05:04,700 --> 00:05:05,250
We have to.

69
00:05:05,390 --> 00:05:09,290
We have to create copy construct but we have to create our own copy construct that here.

70
00:05:09,290 --> 00:05:10,150
Okay.

71
00:05:10,550 --> 00:05:15,820
So creating the copy I stepped over to the structure so the structure is first of all.

72
00:05:15,830 --> 00:05:19,720
Same name as the last name same name as the last name.

73
00:05:19,800 --> 00:05:23,540
What it will take it will take us to an end as input.

74
00:05:23,580 --> 00:05:29,840
So student s and then what it will do it will copy the age Endy name.

75
00:05:30,900 --> 00:05:43,500
So this A2 age is as dotage and this ETL name is as not name.

76
00:05:43,520 --> 00:05:50,110
Okay so this is shallow copy this is shallow copy we are only copying the address.

77
00:05:50,190 --> 00:05:52,800
So this is how inbuilt copy the script that is written.

78
00:05:52,800 --> 00:05:58,290
Okay so this is in made copy constructor This is the code for and built copy constructor but since we

79
00:05:58,290 --> 00:06:06,500
have to create a deep copy since we have to create the copy what we will do we will make a new area

80
00:06:06,890 --> 00:06:17,590
and then we will copy the content so this Edwin name is New get now what would it be the land string

81
00:06:17,590 --> 00:06:21,070
lint off s dart name.

82
00:06:21,390 --> 00:06:26,880
And we have to loop plus one by plus one because we have to copy Mello also and then we will use string

83
00:06:26,880 --> 00:06:35,540
copy function state copy function we will give this ATL name and start name.

84
00:06:35,610 --> 00:06:38,900
Okay so in this way we will create our own DB copy.

85
00:06:38,910 --> 00:06:40,800
So this is the code for deep copy

86
00:06:44,940 --> 00:06:45,990
deep copy.

87
00:06:45,990 --> 00:06:46,270
Okay.

88
00:06:46,290 --> 00:06:47,100
Now what will happen.

89
00:06:53,650 --> 00:06:56,650
So this is our name from the mean.

90
00:06:56,650 --> 00:07:00,820
This convinced that this seven hundred and I will lose a b c d and then

91
00:07:05,070 --> 00:07:16,940
now we have our S1 so S1 contains h to be 20 and named 2B 800 y 800 because we created our new Eddie

92
00:07:17,300 --> 00:07:21,750
and then we copied the content and the address of this new ad is 800.

93
00:07:21,830 --> 00:07:22,420
Okay.

94
00:07:22,580 --> 00:07:25,780
So the name of S1 points towards this primary location.

95
00:07:25,790 --> 00:07:31,130
Now let us talk about the copy constructor so we'll copy constructor we are creating a new object with

96
00:07:31,130 --> 00:07:32,630
the name S2.

97
00:07:32,840 --> 00:07:35,890
So first of all we will copy that each.

98
00:07:35,900 --> 00:07:41,480
So each will become 20 and then what we are trying to do here is we are creating a new edit.

99
00:07:41,480 --> 00:07:47,690
So when you add it will get created the size will be 5 and then what we are trying to do we are copying

100
00:07:47,690 --> 00:07:48,280
the content.

101
00:07:48,290 --> 00:07:53,100
So a b c d and then let's say the address is 900.

102
00:07:53,210 --> 00:07:57,190
So this time the name of S2 is containing the address nine hundred.

103
00:07:57,480 --> 00:08:05,150
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

104
00:08:05,240 --> 00:08:10,770
S1 so when eventually write as front our display then ABC the printed there then we will write astronaut

105
00:08:10,870 --> 00:08:11,300
display.

106
00:08:11,330 --> 00:08:13,040
Then explicitly got printed.

107
00:08:13,040 --> 00:08:23,530
Okay so this is deep copy and how we will call our constructor student as to in Port argument S1 so

108
00:08:23,530 --> 00:08:29,000
this has to be going this and this S1 will be as a barometer.

109
00:08:29,020 --> 00:08:29,290
Okay.

110
00:08:29,320 --> 00:08:31,240
This S1 will be as a barometer.

111
00:08:32,080 --> 00:08:36,170
Okay so according to me everything seems fine but everything is not fine.

112
00:08:36,580 --> 00:08:37,140
Okay.

113
00:08:37,210 --> 00:08:38,170
Why does not fine.

114
00:08:38,170 --> 00:08:43,210
There is a very big mistake that we are doing here is let's try to understand what is the mistake that

115
00:08:43,630 --> 00:08:46,590
we are that I am talking about.

116
00:08:46,660 --> 00:08:56,870
So what happened is when you light student S2 S1 so this will go in this function of the copy constructor

117
00:08:57,200 --> 00:09:00,020
and S1 will come here.

118
00:09:00,020 --> 00:09:01,890
So a copy will be created.

119
00:09:01,970 --> 00:09:04,340
So over so we can write.

120
00:09:04,340 --> 00:09:16,660
We can write this line is student s is main dot S1 okay S1 from the mean is being incompetent to student

121
00:09:16,660 --> 00:09:24,040
s and in the previous we do as we have learned whenever we write student s three close S1.

122
00:09:24,040 --> 00:09:26,220
So although this is copy assignment operator.

123
00:09:26,260 --> 00:09:29,810
But we know copy constructor will be called here.

124
00:09:30,010 --> 00:09:32,580
We have already discussed about it in the previous videos.

125
00:09:32,620 --> 00:09:34,900
So copy constructor will be called here.

126
00:09:34,900 --> 00:09:41,830
Okay so at this line copy constructor is being getting called Okay by a copy constructor is being recalled

127
00:09:41,830 --> 00:09:44,680
because we are passing values by value.

128
00:09:44,710 --> 00:09:45,210
Okay.

129
00:09:45,280 --> 00:09:49,720
This is passed by values and although this is not correct this is not correct.

130
00:09:49,720 --> 00:09:49,960
Why.

131
00:09:49,990 --> 00:09:51,220
This is not correct.

132
00:09:51,400 --> 00:09:58,360
Why this is not correct because this is not valid because as soon as we create our own copy constructor

133
00:09:58,480 --> 00:10:01,420
and will copy construct is not available to us anymore.

134
00:10:01,540 --> 00:10:07,120
Just like as soon as we write our own default constructor that inbuilt default constructor goes away.

135
00:10:07,120 --> 00:10:12,700
Likewise as soon as we create our own copy constructor the inbuilt copy constructor is not available

136
00:10:12,700 --> 00:10:13,820
to us anymore.

137
00:10:13,990 --> 00:10:19,390
So what really happened at this line we are calling copy constructor so in bulk does not available so

138
00:10:19,390 --> 00:10:21,070
our copy constructively regardless.

139
00:10:21,070 --> 00:10:28,780
So this will call our own copy constructor okay and then we will come to this argument and as we come

140
00:10:28,780 --> 00:10:35,320
to this argument copy construct every get called to copy the values from S1 to s..

141
00:10:35,400 --> 00:10:40,360
Okay so in this way infinite loop will be created in finite loop will be created.

142
00:10:40,360 --> 00:10:40,840
Why.

143
00:10:40,870 --> 00:10:48,220
Because first of all copy constructively but call here to copy the values from S1 to s and then then

144
00:10:48,220 --> 00:10:53,110
we will reach this argument then a game copy constructor will be called and our copy can certainly be

145
00:10:53,110 --> 00:10:57,250
called because we have write our own copy constructors when will construct but is not available to us

146
00:10:57,250 --> 00:10:58,860
anymore so why.

147
00:10:58,890 --> 00:11:05,950
That's why infinite loop is here so why infinite loop because we are talking about creating copy constructor

148
00:11:06,310 --> 00:11:13,220
and in the argument of that copy constructor we need a copy constructor okay I'm repeating myself.

149
00:11:13,220 --> 00:11:19,310
So we're creating a copy constructor and to create copy constructor we need to copy constructor in the

150
00:11:19,310 --> 00:11:22,820
argument to copy the values okay.

151
00:11:22,980 --> 00:11:28,470
So what we have to do is we have to avoid calling this copy constructor and how can we avoid calling

152
00:11:28,470 --> 00:11:31,500
this copy constructor by using passed by reference.

153
00:11:31,500 --> 00:11:32,060
Okay.

154
00:11:32,280 --> 00:11:36,720
So if you look here there is no need to pass by value.

155
00:11:36,720 --> 00:11:37,050
Okay.

156
00:11:37,100 --> 00:11:43,200
Vertical copy gastric but simply can pass biodefense because we are only accessing its properties.

157
00:11:43,290 --> 00:11:44,730
We are not doing anything extra.

158
00:11:44,760 --> 00:11:50,450
We are only accessing the properties of the student is so wide to call copy constructor.

159
00:11:50,520 --> 00:11:52,790
Okay so what will happen.

160
00:11:52,800 --> 00:11:56,400
We can also optimize it how we can meet.

161
00:11:56,490 --> 00:11:58,400
We can make it a constant reference.

162
00:11:58,410 --> 00:12:00,120
We have studied about this.

163
00:12:00,120 --> 00:12:05,040
Like if someone do this as soon as daughter age is minus one.

164
00:12:05,040 --> 00:12:06,520
So I want to avoid.

165
00:12:06,750 --> 00:12:08,690
I want to avoid this line.

166
00:12:08,790 --> 00:12:15,110
I want to avoid this a legal changes so to avoid this all legal changes begin but we can write constraint

167
00:12:15,160 --> 00:12:15,960
here.

168
00:12:16,080 --> 00:12:16,350
Okay.

169
00:12:16,350 --> 00:12:17,580
So what is happening here is

170
00:12:20,800 --> 00:12:33,550
the meaning of this line is very simple student constant reference variable S1 is that S1 so this is

171
00:12:33,560 --> 00:12:34,490
is the mean.

172
00:12:34,660 --> 00:12:35,820
This one from mean.

173
00:12:35,830 --> 00:12:40,860
Okay so this s is an offense variable the westward and constant.

174
00:12:41,170 --> 00:12:46,660
It is an offense variable to us and constraint so this time copy constructively logbook code.

175
00:12:46,930 --> 00:12:53,050
Okay so our inbuilt copy construct but also take argument in this session then we'll copy constructor

176
00:12:53,170 --> 00:12:55,100
also takes arguments in this section.

177
00:12:55,210 --> 00:13:00,700
But the problem with built copy constructor is then built a copy constructively make shallow copy but

178
00:13:00,820 --> 00:13:08,240
we want deep copy since we want the copy we have to write our own copy constructor okay.

179
00:13:43,720 --> 00:13:44,590
Okay sir.

180
00:13:46,410 --> 00:13:46,810
Okay.

181
00:13:46,810 --> 00:13:49,240
So I hope you understood this.

182
00:13:49,240 --> 00:13:49,660
Thank you.
