1
00:00:00,810 --> 00:00:01,080
All right.

2
00:00:01,110 --> 00:00:06,180
Welcome back to another lesson for this object or any programming section of the course.

3
00:00:07,560 --> 00:00:13,380
So we're rolling forward with the kind of same code that we had before with the car class.

4
00:00:13,860 --> 00:00:19,200
Except in this light, some make some small modifications to the code and we're going to go over a couple

5
00:00:19,200 --> 00:00:20,010
of new concepts.

6
00:00:21,040 --> 00:00:26,970
One of those concepts is something that I mentioned in the last lecture, which is for us to use one

7
00:00:26,970 --> 00:00:31,350
type of smart pointer that can do reallocate itself.

8
00:00:31,350 --> 00:00:38,790
So we do not have to do something like this where we are explicitly deleting cars since we've dynamically

9
00:00:38,790 --> 00:00:39,450
allocated it.

10
00:00:41,070 --> 00:00:48,000
So we are going to do that, and then we're also going to go over an interesting concept where we try

11
00:00:48,000 --> 00:00:55,560
to create a new object and say that it's equal to some existing object so we can make a copy.

12
00:00:56,790 --> 00:01:03,330
And we're going to talk about what that does and where it can kind of have some potential issues.

13
00:01:04,380 --> 00:01:07,110
So let's go ahead and get started.

14
00:01:07,380 --> 00:01:12,060
So first off, I and talk about how we can use this thing called a smart coin.

15
00:01:13,470 --> 00:01:18,840
And there's multiple types of smart pointers, but we're going to just use one today called unique pointer

16
00:01:20,160 --> 00:01:26,880
and what we're going to do as kind of make another car object just like this and we'll dynamically allocate

17
00:01:26,880 --> 00:01:27,000
it.

18
00:01:27,330 --> 00:01:31,620
But when we use this smart pointer, it's going to allocate itself.

19
00:01:31,620 --> 00:01:34,170
We won't have to put the delete key word for it.

20
00:01:35,760 --> 00:01:42,210
So let's go ahead and get this started, so we're going to actually have to include something for this.

21
00:01:42,540 --> 00:01:46,230
We need to include a library and this library is called memory.

22
00:01:47,670 --> 00:01:49,140
Seniors, some, including memory here.

23
00:01:49,770 --> 00:01:55,730
So what we're going to do is we do Stevie, Colin, Colin and then we do unique pointer.

24
00:01:56,160 --> 00:02:00,510
And you notice that this comes up right here, so I can just press tab and you notice that what comes

25
00:02:00,510 --> 00:02:08,220
after the key word unique underscore pointer is the type of pointer that is, you know, the type of

26
00:02:08,220 --> 00:02:11,210
pointer, what it's really pointing to, which will be type car, right?

27
00:02:11,230 --> 00:02:13,230
We want to make a car pointer, so I press tab.

28
00:02:13,980 --> 00:02:21,420
It spells out this unique pointer for me and then inside of these angle brackets here, similar to what

29
00:02:21,420 --> 00:02:25,110
we use for vectors, you know how vectors we can say, enter something like that.

30
00:02:25,710 --> 00:02:30,150
And here we can also say card for a vector two.

31
00:02:30,150 --> 00:02:37,260
But we're going to say car and here to say that we would like a pointer for type car.

32
00:02:38,730 --> 00:02:43,680
So then after this, we can put the name that we want.

33
00:02:43,680 --> 00:02:51,780
So I'm going to call this car three and then we're going to put some parentheses and inside the parentheses,

34
00:02:52,200 --> 00:03:01,710
I'm going to say new Capital C car, and I could put some of these in here.

35
00:03:01,710 --> 00:03:08,580
I can put some like parameters, but I'm not going to use this parameterized constructor and actually

36
00:03:08,580 --> 00:03:10,170
going to use the default constructor.

37
00:03:10,530 --> 00:03:14,610
And what I'd like to show you is I've modified the code a little bit in here.

38
00:03:14,610 --> 00:03:20,190
So I have actually added I have something in the bottom there that I'll talk about in a second, but

39
00:03:20,190 --> 00:03:27,900
I have added a default constructor as well, so I'm making it so we don't have to use a parameter as

40
00:03:27,900 --> 00:03:28,740
constructor.

41
00:03:28,750 --> 00:03:34,140
We can use a default constructor as well, where everything is just zeroed out and empty strings for

42
00:03:34,140 --> 00:03:38,190
the brand model and paint color, zero for odometer, zero for fuel.

43
00:03:39,520 --> 00:03:45,190
This is something I talk about in a second that I have added, we'll talk about that when it comes to

44
00:03:45,190 --> 00:03:46,420
making copies of objects.

45
00:03:47,830 --> 00:03:52,180
But, you know, I've just like to say that I've made that, you know, we have this default constructor

46
00:03:52,180 --> 00:03:55,270
here prototype before, but we never really implemented it.

47
00:03:55,280 --> 00:04:01,120
It was just there and it would have just kind of allocated memory, but we have not really initialized

48
00:04:01,120 --> 00:04:02,110
any of these members.

49
00:04:02,350 --> 00:04:07,660
Now we are initializing these members in an implementation of the default constructor.

50
00:04:07,930 --> 00:04:11,020
So this wasn't here before, right?

51
00:04:11,170 --> 00:04:12,520
We didn't have this here before.

52
00:04:12,520 --> 00:04:14,140
We just had a prototype for it.

53
00:04:14,150 --> 00:04:15,550
There was this thing right here.

54
00:04:16,130 --> 00:04:24,310
Now we're making it so we can create default objects and we have default initialization values for all

55
00:04:24,310 --> 00:04:24,720
of these.

56
00:04:24,730 --> 00:04:26,260
And once again, ignore this.

57
00:04:26,260 --> 00:04:28,240
For now, just focus on these other ones.

58
00:04:30,000 --> 00:04:33,080
So this is how we make a unique point.

59
00:04:33,120 --> 00:04:37,990
We're not saying what the paint, the brand, the model and the models are.

60
00:04:38,010 --> 00:04:43,470
We're just saying make me a new car and that new car is going to be dynamically allocated.

61
00:04:44,190 --> 00:04:46,080
It's going to be a pointer car.

62
00:04:46,080 --> 00:04:52,440
Three will be a pointer to a dynamically allocated car object and it is going to hold all the values

63
00:04:52,860 --> 00:04:55,320
that are here in this constructor.

64
00:04:55,410 --> 00:04:59,790
This default constructor, this is the default constructor empty parentheses here.

65
00:05:00,090 --> 00:05:01,950
This parameter is constructor.

66
00:05:04,330 --> 00:05:04,630
All right.

67
00:05:05,440 --> 00:05:10,870
So now we have this pointer, it's going to automatically get the allocated notice.

68
00:05:10,870 --> 00:05:15,700
We have three things now we have car car two and car three.

69
00:05:15,940 --> 00:05:25,120
So let's go ahead and run this code here and see what happens as far as what's called for the destructor.

70
00:05:28,290 --> 00:05:32,850
All right, so you notice that we actually get the destructor called three times.

71
00:05:33,180 --> 00:05:39,930
So you notice before I just had this daily car, so this object right here, when we comments it out,

72
00:05:40,920 --> 00:05:43,050
you'll notice that one of these goes away.

73
00:05:44,320 --> 00:05:50,140
Now there's only two, so this one needs to delete, right car needs to be explicitly deleted because

74
00:05:50,140 --> 00:05:51,190
it's just a normal coin.

75
00:05:51,280 --> 00:05:53,020
It's not a smart pointer yet.

76
00:05:53,030 --> 00:05:55,780
This one here is a smart pointer.

77
00:05:56,200 --> 00:05:58,510
And more specifically called a unique pointer.

78
00:05:59,170 --> 00:06:05,290
And once it goes out of scope, it gets rid of itself just like the stack allocated one here card two.

79
00:06:05,650 --> 00:06:11,230
So once Card three, even those dynamically allocated once it goes out of scope, since it's a smart

80
00:06:11,230 --> 00:06:18,100
pointer, it'll automatically delicate itself and then you can just use it similar to everything else.

81
00:06:18,100 --> 00:06:21,130
Like you can say, Card three get odometer instead.

82
00:06:21,730 --> 00:06:25,960
Like, you could just put these right here and I run this, and it's going to give me some different

83
00:06:25,960 --> 00:06:31,450
results, obviously, because, you know, these are just the zero and this is an empty string right

84
00:06:31,450 --> 00:06:32,080
for paint.

85
00:06:32,260 --> 00:06:37,060
Paint was just a totally painkiller with an empty string.

86
00:06:37,060 --> 00:06:40,930
So that's why you see this empty string is use zero for the odometer, right?

87
00:06:40,930 --> 00:06:44,200
Because that's what it got initialized to with the default constructor.

88
00:06:44,200 --> 00:06:45,850
We haven't set anything for it.

89
00:06:46,090 --> 00:06:52,090
I just made this new car without any parameters because I didn't use the parameter constructor.

90
00:06:52,090 --> 00:06:54,580
I use the default constructor, which is right here.

91
00:06:55,540 --> 00:06:59,650
And so that's why we see a zero printing out here and an empty string printing out here.

92
00:07:01,050 --> 00:07:05,760
So I just want to show you that you can use the arrow the same way it works the same way, it's just

93
00:07:05,760 --> 00:07:12,240
a smart pointer, so it doesn't need any explicit cleanup by using the delete key word.

94
00:07:14,580 --> 00:07:22,690
OK, so now we're going to talk about something else, so I'm going to go ahead and make a nother object,

95
00:07:22,710 --> 00:07:25,320
and I'm just going to call this car for.

96
00:07:25,650 --> 00:07:31,680
And I want to show you what happens if I say car car four equals car to.

97
00:07:34,250 --> 00:07:41,700
So now what I do is I'm making a new car and I'm sitting in an equal to an existing car.

98
00:07:41,900 --> 00:07:43,280
This already exists.

99
00:07:44,960 --> 00:07:45,470
So.

100
00:07:46,610 --> 00:07:55,890
What I would like to show you is kind of what happens here, so I might go ahead and let's see, we

101
00:07:55,890 --> 00:07:57,210
have to.

102
00:07:58,220 --> 00:08:00,110
Let's go ahead and print out.

103
00:08:02,710 --> 00:08:03,940
Let's see, I say.

104
00:08:08,380 --> 00:08:26,300
Let's just do a s t c out, and I will say car for dot model jet model, OK, so.

105
00:08:28,940 --> 00:08:35,060
Typing is still slow, so we're going to see our car for you while in fact, I'll say.

106
00:08:39,060 --> 00:08:42,270
Team car for a model.

107
00:08:42,570 --> 00:08:46,590
So I'll say that just so we can see it better in the output.

108
00:08:47,700 --> 00:08:54,060
So right here, I'm just saying car, car, car making a new car, right, so this is us declaring a

109
00:08:54,060 --> 00:08:54,700
new car.

110
00:08:54,720 --> 00:08:56,210
It's not dynamically allocated.

111
00:08:56,220 --> 00:08:57,990
It's just normal, normally allocated.

112
00:08:58,350 --> 00:09:01,410
So I'm saying new car car for not not new.

113
00:09:01,410 --> 00:09:02,310
I don't mean new like this.

114
00:09:02,310 --> 00:09:03,660
I'm just making another object.

115
00:09:03,670 --> 00:09:06,060
So car car four equals car two.

116
00:09:06,270 --> 00:09:08,280
And then I say printing car for model.

117
00:09:08,280 --> 00:09:08,930
And let's see what.

118
00:09:09,150 --> 00:09:10,140
Let's see what it prints out.

119
00:09:13,970 --> 00:09:18,320
And you notice there's also another destructor call here automatically since we're just making those

120
00:09:18,320 --> 00:09:20,000
two allocated car.

121
00:09:20,630 --> 00:09:25,190
So it says printing car for model, I input a space there, but it says Fiesta.

122
00:09:25,400 --> 00:09:27,500
So it's the same as Car two, right?

123
00:09:28,280 --> 00:09:30,200
We said car four equals car two.

124
00:09:30,770 --> 00:09:41,420
You know, I can go here and I could say car for, let's just say, give brand something like that.

125
00:09:42,470 --> 00:09:46,850
And you know, this is going to be smashed together, but whatever.

126
00:09:47,360 --> 00:09:50,510
So I'll run this and let's see what it says.

127
00:09:50,520 --> 00:09:54,020
So it says printing car for model and it says Fiesta Ford.

128
00:09:54,860 --> 00:09:55,060
Right.

129
00:09:55,100 --> 00:09:57,440
So it's totally the same object.

130
00:09:57,440 --> 00:10:01,360
Is this if we kept printing like paint and miles, it would be the same miles.

131
00:10:01,370 --> 00:10:08,810
So it's set to the same attributes, the same number of variables as car two.

132
00:10:09,290 --> 00:10:12,380
So essentially, we've made a copy.

133
00:10:12,590 --> 00:10:13,070
Right?

134
00:10:13,840 --> 00:10:14,480
Well.

135
00:10:15,550 --> 00:10:22,120
Kind of kind of, yeah, this is this kind of makes a copy when I say kind of I mean, what it's really

136
00:10:22,120 --> 00:10:27,970
doing is just copying over the members in what we call a shallow copy.

137
00:10:28,990 --> 00:10:34,060
And I'm about to show you an example now of a problem that can occur.

138
00:10:35,460 --> 00:10:41,670
With shallow copies, and then we're going to talk about how to fix it with something called a deep

139
00:10:41,670 --> 00:10:44,040
copy and using a special constructor.

140
00:10:45,470 --> 00:10:52,430
So right now, what's happening is it's just basically copying over all of the members that car who

141
00:10:52,430 --> 00:10:53,870
had all of the data members.

142
00:10:55,990 --> 00:11:03,970
So let's head over to the class here, so what I did was I actually made a new data member here, so

143
00:11:04,360 --> 00:11:11,440
I called this upgrade code, so it's just a vector of integers, but it's actually a pointer to a vector

144
00:11:11,440 --> 00:11:12,220
of integers.

145
00:11:13,180 --> 00:11:16,690
And that vector of integers, the upgrade codes are just like.

146
00:11:17,740 --> 00:11:22,510
Packages that might come, you know, maybe have the sport package and that's like code one and then

147
00:11:22,510 --> 00:11:28,930
you have like the Lake Luxury package in that code two or something like that.

148
00:11:29,470 --> 00:11:33,310
So let's head to a copy.

149
00:11:33,670 --> 00:11:36,170
So what happens in the default constructor with this?

150
00:11:36,260 --> 00:11:40,760
Well, what I say is I say upgrade codes and this is a pointer, right?

151
00:11:40,780 --> 00:11:45,370
So it's a it's a pointer to a vector, and I say upgrade code equals new.

152
00:11:45,370 --> 00:11:49,330
So I'm dynamically allocating a vector for this.

153
00:11:49,330 --> 00:11:54,060
And then what I do since this is a pointer, I use the arrow and I push back one.

154
00:11:54,070 --> 00:12:01,090
So by default, let's say you get the base package or something like that base package upgrade.

155
00:12:01,090 --> 00:12:01,960
Maybe it's not even OK.

156
00:12:01,970 --> 00:12:06,040
I mean, it's like you get like a media upgrade, right?

157
00:12:06,370 --> 00:12:11,440
So default car comes with a media upgrade, which is like code one or something.

158
00:12:13,110 --> 00:12:14,550
So I also have it here.

159
00:12:14,880 --> 00:12:21,930
When you choose your paint and brand and all that with this parameter as constructor, it also pushes

160
00:12:21,930 --> 00:12:29,580
back this like code one, which we'll call the media upgrade or something like that to the car as well.

161
00:12:32,020 --> 00:12:38,890
So I also have this function, no function down here called add upgrade, and it's actually lets you

162
00:12:38,890 --> 00:12:43,330
add a code to your upgrade codes for the car so you can add an upgrade.

163
00:12:43,330 --> 00:12:46,780
Let's say you want to add a sport package or something, and maybe that's code two.

164
00:12:47,500 --> 00:12:49,060
So let's head back to main.

165
00:12:49,540 --> 00:12:53,500
So what I want to do is I have this car for equals car two.

166
00:12:53,860 --> 00:12:54,940
And so I'm going to say, you know what?

167
00:12:54,940 --> 00:12:57,130
I want my car four to have an upgrade, for sure.

168
00:12:57,140 --> 00:13:06,490
So I'm going to say car for Oops, car for Dot and I'm going to say, add upgrade.

169
00:13:06,490 --> 00:13:13,780
And I'm going to say, let's add code two, which is like some type of sports package or a luxury package

170
00:13:13,780 --> 00:13:14,260
or whatever.

171
00:13:15,430 --> 00:13:17,710
So I go ahead and I add this now.

172
00:13:19,610 --> 00:13:26,240
I want to show you in the debugger what is what's going to happen here, so I'm going to put a full

173
00:13:26,240 --> 00:13:31,370
breakpoint right here and when we go ahead and debug and we get we're going to examine this stuff here.

174
00:13:35,230 --> 00:13:37,540
So I'm going to try.

175
00:13:37,580 --> 00:13:42,700
OK, I can't really zoom in much more here and figure out how to do that.

176
00:13:42,700 --> 00:13:47,230
Yeah, after subsequent lectures, figure out how to make this bigger, but hopefully you can see,

177
00:13:47,230 --> 00:13:47,590
OK.

178
00:13:48,580 --> 00:13:52,430
So let's take a look at Car two, because that exists at this point, right?

179
00:13:52,450 --> 00:13:55,610
We have car and car to this line hasn't been processed yet.

180
00:13:55,630 --> 00:13:59,140
We stopped on it, but it hasn't been executed yet, so we haven't created Car three yet.

181
00:14:00,100 --> 00:14:03,820
So let's look at car to what is car to have for its upgrade codes.

182
00:14:04,090 --> 00:14:05,950
Well, it just has one in here, OK?

183
00:14:06,250 --> 00:14:10,870
Because that's what comes with the basic like when you just get a car just comes with one, right?

184
00:14:11,970 --> 00:14:14,340
And that's coming from up here.

185
00:14:15,560 --> 00:14:15,900
Right.

186
00:14:15,920 --> 00:14:20,900
So here here and the default constructor in here in this parameter as constructor.

187
00:14:21,740 --> 00:14:23,030
So just want to remind you that.

188
00:14:23,980 --> 00:14:27,490
So let me go ahead and step over, so I step down to here.

189
00:14:27,520 --> 00:14:28,870
Now we have car three.

190
00:14:29,680 --> 00:14:32,200
And then what I'm going to do is I'm going to step to the next line.

191
00:14:32,200 --> 00:14:34,390
Remember, it's not going to execute this next line yet.

192
00:14:34,660 --> 00:14:37,300
It will execute this line, though it won't make car for.

193
00:14:38,260 --> 00:14:40,090
So I say car for now.

194
00:14:40,090 --> 00:14:42,940
Let's go ahead and take a look at Cartoon Car four again.

195
00:14:42,940 --> 00:14:43,780
So car for.

196
00:14:44,760 --> 00:14:45,760
Upgrade codes.

197
00:14:45,780 --> 00:14:47,760
It just has one as expected, right?

198
00:14:48,270 --> 00:14:51,690
Yes, we made a new car, but we said it was the same as car to.

199
00:14:53,030 --> 00:14:57,140
So Car two also has one, so that looks right, right?

200
00:14:57,920 --> 00:14:58,790
It's the same thing.

201
00:15:00,760 --> 00:15:05,070
It's made a copy of car to car for should be the same thing as car two, so it looks good, right?

202
00:15:05,080 --> 00:15:06,810
They both say one for upgrade kits.

203
00:15:07,570 --> 00:15:12,190
I'm going to go ahead and step to the next line and what is going to do is it's going to execute this

204
00:15:12,190 --> 00:15:16,480
line nine when it's going to add upgrade to to car for.

205
00:15:18,410 --> 00:15:21,290
So let's go ahead and check this out, so I got a car for now.

206
00:15:22,130 --> 00:15:26,420
I look in the upgrade codes and it has one and two it added to.

207
00:15:27,410 --> 00:15:29,250
So that that that makes sense, right?

208
00:15:29,270 --> 00:15:33,020
We wanted to add an upgrade to it now it shows up in the vector.

209
00:15:33,890 --> 00:15:34,880
Sounds good to me.

210
00:15:34,960 --> 00:15:35,780
And what's the problem?

211
00:15:35,960 --> 00:15:40,100
Let's go ahead and take a look at Car two, though it should just have one, right?

212
00:15:40,880 --> 00:15:43,130
Oh, but it doesn't have one car.

213
00:15:43,130 --> 00:15:47,540
Two also has the code to added to its vector now.

214
00:15:48,440 --> 00:15:49,620
And why is that?

215
00:15:49,640 --> 00:15:57,110
Because what it did was it just literally copied over the same member, right?

216
00:15:57,260 --> 00:16:02,330
I said that what it does is it makes a shallow copy which is just copying over all the data members.

217
00:16:02,660 --> 00:16:04,700
Let's take a look at those data members again.

218
00:16:05,630 --> 00:16:06,990
This all seems good, right?

219
00:16:07,010 --> 00:16:10,220
Fuel tank and just copies, etc. That's what we want, right?

220
00:16:10,400 --> 00:16:13,190
But what it does is it copies this over to right.

221
00:16:13,400 --> 00:16:14,290
And what is this?

222
00:16:14,300 --> 00:16:15,350
It's a pointer.

223
00:16:16,170 --> 00:16:23,550
To a victor now here you see the problem, what it did was it copied over the pointers, so Car two

224
00:16:23,550 --> 00:16:29,160
and Car four are both pointing to the same memory address for the vector.

225
00:16:29,910 --> 00:16:35,340
So when I modify one, it modified the modifies the other because it's just the same memory is pointing

226
00:16:35,340 --> 00:16:39,120
to the same vector that was dynamically allocated on the heap.

227
00:16:40,340 --> 00:16:46,430
So whenever I change car for is going to change car two and whenever I change cards to and I don't mean

228
00:16:46,430 --> 00:16:51,770
in general, I mean when I change the vector, if I add stuff to Katya's vector, it's going to add

229
00:16:51,770 --> 00:16:53,570
the same thing to car force vector.

230
00:16:53,750 --> 00:16:54,710
So I add stuff to car.

231
00:16:54,710 --> 00:16:58,540
Ford's vector is going to have the same things the car to vector, and maybe we don't want that.

232
00:16:58,550 --> 00:17:00,260
We just said car for an upgrade.

233
00:17:00,260 --> 00:17:01,640
I don't want to add it to car, too.

234
00:17:02,570 --> 00:17:04,820
So therein lies the issue.

235
00:17:04,850 --> 00:17:10,500
This is a shallow copy, and we're noticing a potential problem that can happen with that.

236
00:17:10,520 --> 00:17:13,250
So how do we fix this?

237
00:17:13,280 --> 00:17:17,300
Well, here comes another concept yet again.

238
00:17:17,300 --> 00:17:24,800
So we're going to make a special type of constructor mint for making copies, and that's called the

239
00:17:24,800 --> 00:17:26,090
copy constructor.

240
00:17:27,050 --> 00:17:29,240
And so let's head back over to our code.

241
00:17:29,540 --> 00:17:31,340
So what we're going to do?

242
00:17:32,380 --> 00:17:39,100
Is we're actually going to put it up here, and I'm going to put it right after this one here, and

243
00:17:39,100 --> 00:17:43,180
I'm going to say car some and make this constructor but what it's going to have.

244
00:17:44,100 --> 00:17:49,220
This is going to say it's going to say a car.

245
00:17:49,620 --> 00:17:55,470
So what we're going to have is going to have another car passed in the parameters here.

246
00:17:57,420 --> 00:18:05,280
And so I'm just going to call this object like that car and object, and it's passed by reference.

247
00:18:06,210 --> 00:18:08,310
So this is how we make the copy constructor.

248
00:18:08,320 --> 00:18:12,870
It looks just like the normal constructor says car and kind of, you know, same as this one.

249
00:18:13,110 --> 00:18:18,420
But in the parameters here, we're doing something special and this is something that is going to be

250
00:18:18,420 --> 00:18:19,230
recognized.

251
00:18:19,770 --> 00:18:21,330
C++ is going to recognize this.

252
00:18:21,330 --> 00:18:23,880
So we're passing a car.

253
00:18:25,040 --> 00:18:27,050
Object by reference.

254
00:18:28,190 --> 00:18:35,210
Into here and what this is going to be is it's going to be the car on the other side of the equals sign.

255
00:18:35,210 --> 00:18:37,520
So in here we say car.

256
00:18:37,550 --> 00:18:39,140
We're making a new car, right?

257
00:18:39,530 --> 00:18:42,620
So whenever we make a new car, we should call a constructor right.

258
00:18:43,900 --> 00:18:45,880
But what constructeur are we going to call?

259
00:18:45,910 --> 00:18:47,750
Well, we're going to call a copy constructor.

260
00:18:47,770 --> 00:18:48,460
Why is that?

261
00:18:48,460 --> 00:18:51,820
Because this is making a new car in the highlighted section here.

262
00:18:51,850 --> 00:18:55,120
Then we have in one equals sign, which is for assignment.

263
00:18:55,480 --> 00:18:57,820
And then we have an existing car, which is car two.

264
00:18:57,850 --> 00:19:05,960
So this highlighted car car two right here is actually going to be represented by this car and obs here.

265
00:19:05,980 --> 00:19:09,940
So this parameter is going to be what's on the right side of the equals sign, which is car.

266
00:19:11,260 --> 00:19:14,740
And our responsibility now is to implement this.

267
00:19:18,010 --> 00:19:22,180
And I want to explain the other constructors here, so I'm going to put it up here and one copy paste

268
00:19:22,180 --> 00:19:22,360
it.

269
00:19:26,810 --> 00:19:33,530
And our responsibility now is to make sure to copy over everything that's in, you know, copy over

270
00:19:33,530 --> 00:19:38,450
all the other members and copy over everything that's in the vector, but don't have it be the same

271
00:19:38,450 --> 00:19:42,620
point or we want a brand new vector for our car.

272
00:19:44,070 --> 00:19:51,750
So what we're going to do is we're going to put all the we're going to like put all the member functions

273
00:19:51,750 --> 00:19:53,700
here, I'm sorry, member variables here.

274
00:19:53,700 --> 00:19:58,500
So I'm going to say fuel and what I'm going to do is just copy everything over like normal.

275
00:19:58,860 --> 00:20:04,770
But when it comes to the vector, I'm going to make a new vector so this car forward will have its own

276
00:20:04,770 --> 00:20:05,240
vector.

277
00:20:05,250 --> 00:20:13,170
It's not going to try and use the same exact memory like as car, which is going to be OBJ right here

278
00:20:13,170 --> 00:20:16,290
is going to be car to pass by reference.

279
00:20:17,460 --> 00:20:22,680
So I will say fuel equals obj

280
00:20:25,380 --> 00:20:26,910
dot to your.

281
00:20:32,620 --> 00:20:39,040
So and then what I'm going to do is I'm going to say all of this for all the other one, I say odometer

282
00:20:39,580 --> 00:20:40,270
equals.

283
00:20:42,150 --> 00:20:43,140
Object shots.

284
00:20:46,410 --> 00:20:52,560
So Australia Day odometer and I'm going to have to do this for each one, I'll say painkiller equals

285
00:20:53,220 --> 00:20:55,500
object dot painkiller.

286
00:20:57,960 --> 00:21:03,600
And then now when I get to upgrade codes will actually let me do brand first, some I say brand equals

287
00:21:03,900 --> 00:21:06,880
object brand.

288
00:21:09,510 --> 00:21:15,210
And then here we have the what is this model?

289
00:21:16,850 --> 00:21:28,100
So model equals object model, and now I come to the upgrade code, so I'm going to say upgrade codes.

290
00:21:28,550 --> 00:21:29,810
But what I'm going to say?

291
00:21:32,210 --> 00:21:33,650
Is upgrade codes.

292
00:21:33,890 --> 00:21:38,780
Equals new and I'm actually just going to copy all this, I want to make a new vector.

293
00:21:41,190 --> 00:21:46,140
Once what's at stick on me, see what's going on.

294
00:21:47,070 --> 00:21:52,320
Caitlin, face that here, and then what we want to do is we want to add all the stuff from objects

295
00:21:52,320 --> 00:21:53,220
factor into here.

296
00:21:53,220 --> 00:21:56,130
So I'm going to say or and I.

297
00:21:57,780 --> 00:22:00,360
Equals zero.

298
00:22:02,060 --> 00:22:09,470
I will say so actually, what I will do is I will say.

299
00:22:14,500 --> 00:22:15,310
No, this will be fine.

300
00:22:15,400 --> 00:22:17,200
I'll say NTI equals zero.

301
00:22:19,200 --> 00:22:23,550
I is less than and I will say object.

302
00:22:24,980 --> 00:22:30,530
So, object, what, uh, upgrade codes.

303
00:22:31,580 --> 00:22:34,370
And this is going to be an arrow because it's a pointer, right?

304
00:22:34,370 --> 00:22:39,150
And we arrows size and then I will say it plus plus.

305
00:22:39,150 --> 00:22:40,160
So we're going to go the first.

306
00:22:40,160 --> 00:22:47,990
Many times we're going to look for as many times as there are objects and as there are codes in this

307
00:22:47,990 --> 00:22:55,310
other object, which will be this remember Car 2's upgrade codes vector

308
00:22:58,190 --> 00:22:59,820
when I need to do something like this.

309
00:22:59,820 --> 00:23:04,970
So I'm actually going to say auto v, I make some kind of vector.

310
00:23:07,610 --> 00:23:12,800
Actually, I just say temp equals, and what I need to do is, do you reference this object?

311
00:23:13,580 --> 00:23:22,250
Dot upgrade codes, because it's a pointer to a vector, and then I'm using the auto keyword to automatically

312
00:23:22,250 --> 00:23:23,450
detect like that.

313
00:23:23,450 --> 00:23:28,250
It's a vector so you can use auto and it will automatically detect the type here.

314
00:23:29,030 --> 00:23:37,850
So right now, it's knowing that this is a vector of integers because I'm d referencing the pointer

315
00:23:38,300 --> 00:23:43,790
object of great curves is a pointer right at pointer to a vector of integers.

316
00:23:44,450 --> 00:23:47,130
So what I'm doing here is I'm d referencing that.

317
00:23:47,150 --> 00:23:53,150
So now we have a normal vector and then what I'm going to do now is I'm actually going to say upgrade

318
00:23:53,150 --> 00:23:55,940
codes, aero push back.

319
00:23:56,450 --> 00:23:59,760
And then in here, I'm going to say temp I.

320
00:24:01,400 --> 00:24:05,900
So what I'm doing is I'm just copying over the all the values that are in the.

321
00:24:07,460 --> 00:24:14,540
This objects of great codes over to the current objects of great codes, when I say current, I mean

322
00:24:14,540 --> 00:24:19,880
like car for right, we say car, which is like, make a new car.

323
00:24:19,910 --> 00:24:21,230
What do we call it car for?

324
00:24:21,260 --> 00:24:24,530
So what it does is it says, OK, I see this, I see this.

325
00:24:24,530 --> 00:24:26,210
I see you want to set it equal to you.

326
00:24:26,900 --> 00:24:31,430
I see what you want to do is you want to want to make a copy of Car two and have that be car for.

327
00:24:31,460 --> 00:24:37,090
So what it does is it goes here and it calls this copy constructor and it says, OK, this is car to

328
00:24:37,100 --> 00:24:38,510
now pass by reference.

329
00:24:39,110 --> 00:24:46,190
And this stuff over here, that is just the data members of the class is the.

330
00:24:48,550 --> 00:24:56,890
The object that is being called upon another way you can refer to this is some using the keyword this

331
00:24:58,480 --> 00:25:03,730
so this dot upgrade code is something like.

332
00:25:08,410 --> 00:25:08,770
Yes.

333
00:25:09,220 --> 00:25:11,200
Can you change into a point, for this instance?

334
00:25:12,290 --> 00:25:12,620
Looks.

335
00:25:14,690 --> 00:25:25,040
This arrow of great codes or this upgrade codes is a is referring to car for in this example.

336
00:25:25,040 --> 00:25:27,910
So it's the object that is being called upon.

337
00:25:27,920 --> 00:25:30,710
So same thing here, like a car.

338
00:25:31,980 --> 00:25:37,260
Arrow jet fuel car is the object that's being called upon.

339
00:25:38,340 --> 00:25:44,870
All right, so that's that that would be referred to as this, but we don't need to put that like we

340
00:25:44,870 --> 00:25:45,740
can just get rid of this.

341
00:25:45,740 --> 00:25:47,770
I mean, this is upgrade codes.

342
00:25:47,780 --> 00:25:53,600
This is the upgrade codes of car for it's the one that is being called upon.

343
00:25:53,840 --> 00:26:00,230
This is the object that's being passed to the copy constructor for us to put copies of into these guys

344
00:26:00,230 --> 00:26:00,890
over here.

345
00:26:01,920 --> 00:26:02,280
I.

346
00:26:03,570 --> 00:26:10,530
All right, it's all we're doing is coughing all the stuff over from cartoons, vector into car force

347
00:26:10,530 --> 00:26:12,870
vector, which is the object, this being vehicle.

348
00:26:14,960 --> 00:26:19,550
OK, so once we do that, it should be all good.

349
00:26:19,670 --> 00:26:26,060
And so let's go ahead and see how this plays out now.

350
00:26:26,930 --> 00:26:31,670
So we have this same thing where we add the upgrade to, but things should change now, so let's go

351
00:26:31,670 --> 00:26:34,010
ahead and put a breakpoint.

352
00:26:37,330 --> 00:26:42,550
Let's go ahead and run it, and let's see what happens, so we will step over here.

353
00:26:43,940 --> 00:26:46,060
Car foreign, you should both have one.

354
00:26:47,830 --> 00:26:54,520
So we have let's check the upgrade code, so we have one in there, so that's totally fine.

355
00:26:55,500 --> 00:26:59,820
And then let's go ahead and check out car for.

356
00:27:00,480 --> 00:27:07,290
So this just has one as well, which is good for now, so let's run this line right?

357
00:27:08,130 --> 00:27:09,540
And then now let's see what happens.

358
00:27:09,570 --> 00:27:15,480
So Car four has two, which is what we want, but let's check car to.

359
00:27:17,200 --> 00:27:23,800
COTU only has one now, so it didn't modify a cartoon because what we did was we made it have its own

360
00:27:23,800 --> 00:27:29,730
vector, so each object is going to have its own vector even if it's making a copy.

361
00:27:29,740 --> 00:27:31,420
So this is a new vector.

362
00:27:31,420 --> 00:27:36,190
So Carrefour had its own new dynamically allocated vector.

363
00:27:36,790 --> 00:27:41,570
And we just copied all the stuff over when we made the copy.

364
00:27:41,590 --> 00:27:49,030
So at this point in time, Car two only had one and then as far as that, Vector had code one only.

365
00:27:49,240 --> 00:27:51,340
So that's why car for only had code one.

366
00:27:51,340 --> 00:27:55,930
And then now we add that of Card Carrefour, which has two.

367
00:27:56,200 --> 00:27:58,180
And it did not modify Car two.

368
00:27:58,180 --> 00:28:00,310
It only modified Carrefour, which is what we want.

369
00:28:02,810 --> 00:28:08,970
OK, so kind of a lot of stuff, but I'm yeah, I'm not going to go over anything else in this lecture.

370
00:28:08,990 --> 00:28:11,020
I just wanted to touch on those few things.

371
00:28:11,030 --> 00:28:16,460
So the unique pointer, remember that this is a smart pointer that will allocate itself.

372
00:28:16,460 --> 00:28:17,660
We don't need to delete it.

373
00:28:19,700 --> 00:28:26,960
And the this right here, if you don't have a copy constructor, it is going to make a shallow copy

374
00:28:26,960 --> 00:28:29,840
where it just copies all the members over straight up.

375
00:28:30,650 --> 00:28:36,110
So it would have copied that pointer over and it would have been pointing to the same piece of memory

376
00:28:36,620 --> 00:28:41,670
cartoon Carrefour would have been pointing to the same piece of memory for the vector, is what I mean.

377
00:28:41,690 --> 00:28:49,790
So the vector here would have been the same piece of memory for car two and car for Intel.

378
00:28:50,600 --> 00:28:54,620
We made this right here, the copy constructor, right?

379
00:28:54,650 --> 00:28:55,820
Here it is in the header.

380
00:28:56,600 --> 00:28:58,150
Made this copy constructor.

381
00:28:59,660 --> 00:29:01,580
Here it is in the implementation file.

382
00:29:01,940 --> 00:29:07,550
The banking sector where we copy everything over, but we make sure to make a new vector for this right?

383
00:29:08,000 --> 00:29:13,430
New dynamically allocated vector and then we go ahead and we copy everything that was in the McDeere

384
00:29:13,430 --> 00:29:13,760
over.

385
00:29:15,630 --> 00:29:20,640
So now when you're here and you're seeing this and you have a copy instructor, you know that it's going

386
00:29:20,640 --> 00:29:22,380
to make something called a deep copy.

387
00:29:22,830 --> 00:29:30,360
So you're the one controlling what happens when you make a copy and have a new object that gets set

388
00:29:30,360 --> 00:29:32,760
to a copy of this existing object?

389
00:29:34,070 --> 00:29:39,190
OK, so we're going to kind of continue on to the next lecture with something about assigning things

390
00:29:39,200 --> 00:29:40,940
that we want instead of making a new copy.

391
00:29:40,970 --> 00:29:49,850
We'll try and see what we need to do if we want to have a shallow or deep copy for setting one car equal

392
00:29:49,850 --> 00:29:50,480
to car to.

393
00:29:50,600 --> 00:29:57,710
So if I made like a car car four by itself, like with a default constructor or parameterized, and

394
00:29:57,710 --> 00:30:03,800
then later on, I want to say Car four equals car two and I wanted to have it be the same as Car two.

395
00:30:04,190 --> 00:30:09,680
We run into the same issue with the shallow anticorpi, and so we're going to have to implement yet

396
00:30:09,680 --> 00:30:15,440
another special function in here, like the copy constructor that will handle that.

397
00:30:17,660 --> 00:30:20,440
OK, so with that, I will see you in the next election.
