1
00:00:00,730 --> 00:00:07,270
OK, so now we are going to be transitioning into a new sub topic.

2
00:00:07,990 --> 00:00:13,990
This is kind of related to the copy constructor, but what we're going to be looking at now is something

3
00:00:13,990 --> 00:00:16,330
called overloading operators.

4
00:00:16,960 --> 00:00:19,070
So you can kind of see these down here.

5
00:00:19,100 --> 00:00:21,280
These are operators that you are familiar with.

6
00:00:21,790 --> 00:00:29,860
We are going to be making special functions, member functions of the classes that can change the way

7
00:00:29,860 --> 00:00:31,900
that these work with our objects.

8
00:00:34,150 --> 00:00:36,840
So think about operations on abstract data types.

9
00:00:36,850 --> 00:00:41,680
When you're working with numbers, for example, everything is pretty straightforward, right?

10
00:00:42,460 --> 00:00:46,840
So if you have something like a equals five, then you say a equals seven.

11
00:00:47,110 --> 00:00:50,080
Now, a holds the value seven instead of five, right?

12
00:00:51,670 --> 00:01:00,100
Or maybe you say a equals A-plus five after this, and now you would have 10 stored in the variable

13
00:01:00,100 --> 00:01:00,850
a right.

14
00:01:01,780 --> 00:01:04,870
What if you wanted to do this same thing with objects, though?

15
00:01:06,010 --> 00:01:13,810
So you wanted to be able to add objects together or assign objects to other objects or subtract them

16
00:01:13,810 --> 00:01:14,740
or multiply them?

17
00:01:15,340 --> 00:01:18,280
This is what we're going to be going over in this section.

18
00:01:18,280 --> 00:01:24,880
So to use these operators with abstract data types rather than the standard ones like, you know, numbers,

19
00:01:24,910 --> 00:01:26,800
strings, stuff like that.

20
00:01:27,220 --> 00:01:34,660
We will need to create our own member functions of the class that define how to perform these operations

21
00:01:35,020 --> 00:01:39,250
on the custom objects that we're making, like the car class, for example, which we will be looking

22
00:01:39,250 --> 00:01:40,120
at right after this.

23
00:01:41,920 --> 00:01:44,980
So this is what we're going to discuss in the next few lectures.

24
00:01:44,980 --> 00:01:46,830
We're going to go over.

25
00:01:46,840 --> 00:01:51,310
I'm going to kind of split it up so we can separately look at each one of these operators.

26
00:01:54,260 --> 00:02:01,370
In this lecture, we are going to focus on just one of them, which is the assignment operator.

27
00:02:02,720 --> 00:02:09,680
So the assignment operator is very similar, like as far as overloading, it is very similar to what

28
00:02:09,680 --> 00:02:11,510
we've already done with the copy constructor.

29
00:02:12,320 --> 00:02:18,560
So by default, assigning an object to another object is going to have the same effect as what we noticed

30
00:02:18,770 --> 00:02:20,780
with the default copy constructor.

31
00:02:21,440 --> 00:02:28,070
If we do not make our own overloaded version of the assignment operator, then we will get a shallow

32
00:02:28,160 --> 00:02:29,500
version of the object.

33
00:02:29,510 --> 00:02:35,270
So think back to the copy constructor and how we talked about that shallow copy that wasn't really good

34
00:02:35,930 --> 00:02:39,260
when we were using dynamic memory, right?

35
00:02:39,260 --> 00:02:44,450
So it just made a copy of the pointer rather than allocating new memory for that vector.

36
00:02:44,450 --> 00:02:49,250
So if you don't remember, you could jump back and look at it, but we're about to look at it again,

37
00:02:49,790 --> 00:02:51,710
so it'll kind of refresh your memory.

38
00:02:52,580 --> 00:02:56,780
So we're going to run into the same issue when we're assigning an object to another object.

39
00:02:57,260 --> 00:03:04,310
If they have a member variable as like a data member variable of the class, that is a pointer.

40
00:03:05,570 --> 00:03:14,150
Then when those variables kind of get copied over to deal with this equality, they're going to actually

41
00:03:14,150 --> 00:03:16,010
use the same pointer for two different object.

42
00:03:16,010 --> 00:03:19,170
You'll have the same pointer, so those variables will point to the same memory.

43
00:03:19,940 --> 00:03:22,610
So we have to come up with a custom way of assignment.

44
00:03:22,820 --> 00:03:25,970
So let's go ahead and hand over to the editor to discuss the syntax for this.

45
00:03:29,250 --> 00:03:31,020
So I'm going into sea line here.

46
00:03:31,740 --> 00:03:34,110
Hopefully, it's big enough for you to see everything OK.

47
00:03:34,740 --> 00:03:40,620
So here you notice that I'm in the header file card, not so familiar project we're looking at, you

48
00:03:40,620 --> 00:03:44,430
know, not real project, but the code we've looked at before for the car class, right?

49
00:03:45,540 --> 00:03:47,700
So here is our copy constructor that we made.

50
00:03:49,090 --> 00:03:57,970
And we're going to be adding our overloaded assignment operator into this header file and the CBP file.

51
00:03:58,630 --> 00:04:01,900
But first, let's go ahead and jump over to me and take a look at something.

52
00:04:03,070 --> 00:04:03,570
So what?

53
00:04:03,760 --> 00:04:08,020
I have a comment, an outline right here, and what I'm going to do is show you kind of just like I

54
00:04:08,020 --> 00:04:13,780
did with the copy constructor, what happens when we just use the default assignment operator for objects?

55
00:04:14,770 --> 00:04:16,300
So I'm going to uncomment this.

56
00:04:17,020 --> 00:04:18,550
And so you see what it's doing here.

57
00:04:19,180 --> 00:04:23,010
As you know, this was the line that we actually use for the copy constructor, right?

58
00:04:23,030 --> 00:04:27,810
And this is using the copy constructor, this highlighted line right here directly after it.

59
00:04:27,820 --> 00:04:32,770
What I say is I say, OK, well, actually, I want Carrefour to be the same as car to.

60
00:04:34,870 --> 00:04:40,420
And then you notice that I change car for a change, I add something to change something to the object.

61
00:04:40,420 --> 00:04:41,710
I add an upgrade, right?

62
00:04:41,950 --> 00:04:43,570
So think back to the add upgrade.

63
00:04:43,570 --> 00:04:44,510
Let's take a look at that.

64
00:04:44,530 --> 00:04:45,070
What is that?

65
00:04:45,070 --> 00:04:45,760
Add upgrade?

66
00:04:45,790 --> 00:04:51,770
Well, remember that it's a pointer to upgrade codes, right?

67
00:04:51,790 --> 00:04:53,050
So this is a vector.

68
00:04:53,980 --> 00:04:56,350
So we have this upgrade codes.

69
00:04:56,350 --> 00:05:02,560
And when we add upgrade with this function right here, let's go into the CBP file and take a look at

70
00:05:02,560 --> 00:05:02,740
that.

71
00:05:02,750 --> 00:05:03,760
So add upgrades.

72
00:05:03,760 --> 00:05:08,350
What it does is it just pushes back a code to the upgrade codes.

73
00:05:10,380 --> 00:05:13,360
You notice like, let's take a look at this copy constructor here.

74
00:05:14,820 --> 00:05:16,680
So this is dynamically allocated.

75
00:05:17,070 --> 00:05:23,010
We have this upgrade codes here, so in the copy constructor, we're doing a new vector for upgrade

76
00:05:23,010 --> 00:05:31,620
codes and in the normal constructor, both right here and right here, we're also making dynamically

77
00:05:31,620 --> 00:05:33,360
allocated memory for this vector.

78
00:05:34,620 --> 00:05:37,410
So this is going to result in the same kind of issue.

79
00:05:37,680 --> 00:05:39,510
It's going this upgrade codes.

80
00:05:40,180 --> 00:05:41,100
So let's go to here.

81
00:05:41,310 --> 00:05:47,640
This upgrade codes data member if we assign it to a.

82
00:05:48,690 --> 00:05:58,860
Another variable here like Car four is equal to car to this upgrade codes, vector is going to be the

83
00:05:58,860 --> 00:06:02,070
same region of memory for both of these objects.

84
00:06:02,580 --> 00:06:04,890
It's just going to copy over the pointer address.

85
00:06:04,890 --> 00:06:11,610
So this will have the car for will have the same vector codes as Car two and we don't want that.

86
00:06:11,610 --> 00:06:16,560
We want both to have their own vector codes, so we're going to need to handle that.

87
00:06:16,770 --> 00:06:19,170
But I'll go ahead and show you what happens with that.

88
00:06:19,170 --> 00:06:22,080
So I'm going to leave this breakpoint for the debugging here.

89
00:06:22,950 --> 00:06:25,110
I mean, zoom in more where we can see it's very well.

90
00:06:25,110 --> 00:06:26,910
The other one was zoomed in, but not this one.

91
00:06:28,020 --> 00:06:32,980
So I'm going to go ahead and run the debugger now, and I'm going to show you kind of what happens here.

92
00:06:33,000 --> 00:06:40,320
So we'll step to this line and step to this line, then step over and we'll see what the ad upgrade

93
00:06:40,920 --> 00:06:43,620
to did to car for and car to.

94
00:06:45,200 --> 00:06:46,790
So let's go ahead and debug this.

95
00:06:50,210 --> 00:06:53,780
So let me bring this up here, so we stopped at this line right here.

96
00:06:54,170 --> 00:06:56,420
I'm going to go ahead and step over.

97
00:06:56,690 --> 00:07:00,890
So Carrefour has been created and it's is set to part two.

98
00:07:02,270 --> 00:07:04,850
This is kind of, you know, so I have car two here.

99
00:07:05,940 --> 00:07:09,840
I could say, let me change this just to Manhattan.

100
00:07:10,020 --> 00:07:11,640
You know, it seemed kind of redundant.

101
00:07:12,330 --> 00:07:14,490
So I'm actually going to say.

102
00:07:16,290 --> 00:07:17,100
Let's see.

103
00:07:18,060 --> 00:07:19,620
Maybe I could just say car.

104
00:07:22,050 --> 00:07:26,190
Like the reference it or something like that, so we can say car four equals car and then we change

105
00:07:26,190 --> 00:07:27,720
it to car two, that makes a little more sense.

106
00:07:27,720 --> 00:07:28,740
So let's go ahead and do that.

107
00:07:30,150 --> 00:07:31,200
So I debug it.

108
00:07:31,380 --> 00:07:34,020
So let's go ahead and step two here.

109
00:07:34,350 --> 00:07:37,920
So now we have a car for and it should be the same as car.

110
00:07:39,570 --> 00:07:42,420
So we got this blue Subaru Outback.

111
00:07:43,650 --> 00:07:44,540
Let's check this out.

112
00:07:44,550 --> 00:07:51,840
This is a blue Subaru Outback with 40000 miles as well, so it's the same same thing copied over upgrade

113
00:07:51,840 --> 00:07:52,890
codes this one.

114
00:07:53,790 --> 00:07:56,250
Let's check this upgrade codes is one.

115
00:07:56,910 --> 00:08:01,080
So Car four is pretty much the same thing as car.

116
00:08:02,700 --> 00:08:06,030
So then let's go here and I'm going to step down.

117
00:08:06,030 --> 00:08:08,520
And now we've gone over this line.

118
00:08:08,530 --> 00:08:11,040
So now Car four should be equal to car two.

119
00:08:11,970 --> 00:08:14,110
So let's go ahead and check that out with his car, too.

120
00:08:14,130 --> 00:08:18,200
Well, it's a white Ford Fiesta with 10000 miles now.

121
00:08:18,300 --> 00:08:23,310
Car four is a white Ford Fiesta with 10000 miles, so it looks like it's copied everything over his

122
00:08:23,310 --> 00:08:24,710
assigned car to the car.

123
00:08:24,720 --> 00:08:25,110
Four.

124
00:08:25,530 --> 00:08:27,180
Let's take a look at the upgrade codes.

125
00:08:27,990 --> 00:08:28,590
One.

126
00:08:28,590 --> 00:08:31,350
For an upgrade code OK, that's good in car two.

127
00:08:31,350 --> 00:08:32,470
That should be one, right?

128
00:08:32,490 --> 00:08:34,050
So this is one for this as well.

129
00:08:35,190 --> 00:08:36,810
Everything looks fine, right?

130
00:08:37,230 --> 00:08:42,750
We've copied car two over the car for, well, let's see what happens after I step past this ad upgrade

131
00:08:42,750 --> 00:08:46,230
and we add the upgrade of two to car for.

132
00:08:47,390 --> 00:08:49,160
So I'm going to go ahead and step over.

133
00:08:50,610 --> 00:08:51,060
So.

134
00:08:52,270 --> 00:08:54,460
Let's check out car for first.

135
00:08:54,910 --> 00:08:56,010
So I go to upgrade codes.

136
00:08:56,440 --> 00:08:57,850
Well, that's what we expect.

137
00:08:58,210 --> 00:09:01,300
Added to car for an upgrade code of two.

138
00:09:02,050 --> 00:09:05,470
Everything else looks the same as it was before, so everything seems fine.

139
00:09:06,010 --> 00:09:08,250
Car two shouldn't have been changed, right?

140
00:09:08,440 --> 00:09:11,910
Because we just wanted to add a upgrade to car four.

141
00:09:12,730 --> 00:09:14,680
Let's go ahead and see what car to says.

142
00:09:14,950 --> 00:09:21,580
So if I go over to car to, oh well, look, it actually added upgrade code two to car two.

143
00:09:22,090 --> 00:09:23,060
And that's a problem, right?

144
00:09:23,080 --> 00:09:24,520
And why is that happening?

145
00:09:24,550 --> 00:09:31,510
Well, it's just like I said, it's because Car Foreign Car two are actually sharing the same upgrade

146
00:09:31,510 --> 00:09:33,580
codes because it's the same region of memory.

147
00:09:33,580 --> 00:09:38,530
All it did was copy of the copy, the pointer address over, which is pointing to the same chunk of

148
00:09:38,530 --> 00:09:40,180
memory that stores the vector.

149
00:09:40,180 --> 00:09:43,660
So they're using the same vector, and that is definitely a problem.

150
00:09:43,660 --> 00:09:51,460
So we're going to have to change that by overloading this assignment operator, which means making something

151
00:09:51,460 --> 00:09:55,210
like our copy constructor, it needs to be a special function.

152
00:09:55,780 --> 00:10:02,170
So C++ knows that when it sees this and it has two objects, it's going to use whatever code is in that

153
00:10:02,170 --> 00:10:02,950
function that we make.

154
00:10:04,010 --> 00:10:05,960
So let's stop this debugging process.

155
00:10:07,170 --> 00:10:09,900
And I'm going to go back over to Corridor H.

156
00:10:10,290 --> 00:10:16,320
What I'm going to do is I'm going to add something here that will be our prototype of the overloaded

157
00:10:16,320 --> 00:10:16,890
operator.

158
00:10:17,790 --> 00:10:24,240
So what this is going to do is it's going to return the type.

159
00:10:25,180 --> 00:10:26,590
Of the class.

160
00:10:26,860 --> 00:10:35,740
So it's going to return car object, but specifically what it's going to return is the calling is basically

161
00:10:35,740 --> 00:10:38,830
going to return the object that is being called upon.

162
00:10:39,100 --> 00:10:41,320
So I'll explain that more later.

163
00:10:41,320 --> 00:10:51,250
But if that's that's what it does is it needs to return to the car object that is being set that's been

164
00:10:51,700 --> 00:10:53,320
having something set equal to.

165
00:10:53,650 --> 00:11:02,170
So what I mean by that is like if we go over here, so it's basically trying to return, return this.

166
00:11:02,470 --> 00:11:06,730
So we're copying everything from here into here.

167
00:11:08,210 --> 00:11:16,460
And what we need is to return this object here, and that may seem confusing, but I'll show you how

168
00:11:16,460 --> 00:11:17,030
that's going to work.

169
00:11:17,030 --> 00:11:23,090
We're actually going to use a special keyword called this and that will represent our object that's

170
00:11:23,090 --> 00:11:24,930
on the left side of the assignment.

171
00:11:24,950 --> 00:11:25,430
Operator.

172
00:11:25,440 --> 00:11:26,210
This one right here.

173
00:11:28,190 --> 00:11:35,270
So let's go ahead and do that and what we're going to do since we're returning that actual object,

174
00:11:35,270 --> 00:11:37,820
we need to return it by reference.

175
00:11:38,000 --> 00:11:44,690
So I'm going to put an ampersand and then what I do is I put the word operator because we are wanting

176
00:11:44,690 --> 00:11:46,720
to overload an operator, right?

177
00:11:46,730 --> 00:11:52,250
You'll notice that this is going to be common amongst all overloading of the operators.

178
00:11:54,520 --> 00:11:59,860
So I say we're going to overload an operator and we're going to return so this far right here just to

179
00:11:59,860 --> 00:12:04,330
recap this says return a car object by reference.

180
00:12:05,080 --> 00:12:07,600
This right here says we're going to overload an operator.

181
00:12:07,900 --> 00:12:10,930
Which one are we going to overload this assignment operator?

182
00:12:12,010 --> 00:12:17,020
Then I put the parentheses for the parameters, and it's going to be just like this right here.

183
00:12:17,410 --> 00:12:22,360
I'm going to put the other object passed by reference.

184
00:12:25,350 --> 00:12:28,590
So what is this object in here?

185
00:12:28,830 --> 00:12:30,120
It's this one.

186
00:12:31,120 --> 00:12:32,620
On this side right here.

187
00:12:33,820 --> 00:12:40,000
Car two is being passed into the parentheses as an argument here.

188
00:12:40,330 --> 00:12:44,840
And what we're going to do is very similar, pretty much the exact same thing as the copy constructor.

189
00:12:44,860 --> 00:12:50,200
We're just going to take everything from this object and we're going to have all of our data members

190
00:12:50,200 --> 00:12:54,450
like down here be set equal to.

191
00:12:54,460 --> 00:12:56,740
And when I say set equal to, I really just mean a sign.

192
00:12:56,740 --> 00:12:59,830
We're going to sign everything from this object over to this object.

193
00:12:59,830 --> 00:13:07,780
So each one of these needs to be, I guess you could say, copied over or overwritten over onto.

194
00:13:08,730 --> 00:13:15,120
The object on the left side, which is this car for, so hopefully that makes sense, that will make

195
00:13:15,120 --> 00:13:17,100
more sense when we're getting into the implementation.

196
00:13:18,390 --> 00:13:20,530
So this is the prototype, this is all good.

197
00:13:20,550 --> 00:13:24,600
I can just click this little light bulb and I'm going to say generate definition for this function.

198
00:13:26,560 --> 00:13:31,810
Cool, and I'm going to delete that, and what I'm going to do is literally just copy over this code

199
00:13:31,810 --> 00:13:34,900
from the copy constructor because it's going to essentially be the same thing.

200
00:13:35,410 --> 00:13:40,360
I'm going to have to modify a few things, but not much so my copy of this code.

201
00:13:43,330 --> 00:13:44,650
I'm going to paste it down here.

202
00:13:45,490 --> 00:13:47,950
Let's go ahead and zoom in a bit too, so we can see better.

203
00:13:49,900 --> 00:13:51,160
So we have this thing.

204
00:13:51,160 --> 00:13:56,680
Orbs right here is the name of the variable in the parameter, so I'm using that object to it.

205
00:13:56,680 --> 00:13:58,840
It was the same as the copy constructor up here.

206
00:13:59,020 --> 00:14:03,850
So that was kind of nice to name it the same thing, so it would be easy to copy over.

207
00:14:05,110 --> 00:14:11,890
So the only thing we really need to change is we don't want to make a new vector.

208
00:14:12,250 --> 00:14:20,170
All we're doing is changing the existing upgrade codes and all of the other variables to whatever is

209
00:14:20,170 --> 00:14:22,090
stored inside this object.

210
00:14:24,170 --> 00:14:26,660
So let's talk about this thing.

211
00:14:27,800 --> 00:14:35,150
So first, what I'm going to do is instead of, I'm going to get rid of this, actually, so we're not

212
00:14:35,150 --> 00:14:36,290
going to make a new one.

213
00:14:38,980 --> 00:14:42,400
What I'm going to do instead is I'm going to clear it out.

214
00:14:42,490 --> 00:14:54,280
So when I do upgrade codes like this, I'm not doing object upgrade codes, object upgrade codes, you

215
00:14:54,280 --> 00:14:57,250
know, that's that has to do with this object right here.

216
00:14:58,450 --> 00:14:59,890
Just upgrade codes.

217
00:15:00,100 --> 00:15:04,720
Is this object right here, the car for?

218
00:15:05,230 --> 00:15:07,880
So just want to really make that distinction very clear.

219
00:15:07,900 --> 00:15:10,180
So let's go here.

220
00:15:10,450 --> 00:15:14,530
So this this car object right here is.

221
00:15:15,600 --> 00:15:26,730
Cartoon all this stuff right here like upgrade codes, model brand, paint color, those are all parts

222
00:15:26,730 --> 00:15:28,680
of another object, right?

223
00:15:29,490 --> 00:15:31,110
And what object are they part of?

224
00:15:31,140 --> 00:15:35,790
They are part of this object, the car for us.

225
00:15:36,090 --> 00:15:39,420
We refer to it as the calling object like right here.

226
00:15:39,420 --> 00:15:46,220
When you say car Ford Dot ad upgrade, we're calling an ad upgrade on car four.

227
00:15:46,230 --> 00:15:51,900
And when we say we're calling something on car floor, we can refer to car for as the object that's

228
00:15:51,900 --> 00:15:53,160
being called upon.

229
00:15:53,910 --> 00:15:59,040
And that object is represented by these data members in this class.

230
00:15:59,050 --> 00:16:07,860
So when this function gets called, the thing that is on the left side of the equals sign right here

231
00:16:07,980 --> 00:16:12,390
is going to be represented by all of this stuff on the left side.

232
00:16:12,630 --> 00:16:17,400
So this this this, this, this this all on the left side of the equal sign over here.

233
00:16:18,120 --> 00:16:24,120
The thing in the parameters is the one on the right side equals sign, and it's the same for like all

234
00:16:24,120 --> 00:16:24,670
other functions.

235
00:16:24,670 --> 00:16:26,160
So we have an upgrade here.

236
00:16:26,580 --> 00:16:34,520
If I wanted to refer in this function to Carrefour, which is calling the ad upgrade function right

237
00:16:34,680 --> 00:16:45,000
and of is being called upon car for right car for could be represented by a special word called this.

238
00:16:46,090 --> 00:16:53,770
This refers to that object that's being called upon and upgraders being called on car for.

239
00:16:54,220 --> 00:17:00,130
And so when I go to the code right here and add upgrade gets executed, right?

240
00:17:00,130 --> 00:17:06,270
Because when this gets cold in Maine, it's going to jump here and it's going to run this function.

241
00:17:06,280 --> 00:17:17,940
The code in here, if I type the word this is referring to, it's referring to the car for whatever

242
00:17:18,070 --> 00:17:19,810
is on the left of the dot right.

243
00:17:19,850 --> 00:17:22,690
This is the car for it's something dot and upgrade.

244
00:17:22,690 --> 00:17:25,700
The thing before the dot is the object that's being referred to.

245
00:17:25,960 --> 00:17:33,450
And we can specifically refer to that object as this because it's like this is the thing that's being,

246
00:17:34,210 --> 00:17:36,240
you know, upgrade upgrades being called on.

247
00:17:36,250 --> 00:17:38,260
It's like this invisible object, right?

248
00:17:38,260 --> 00:17:39,010
We can't see it.

249
00:17:39,220 --> 00:17:41,860
This object we can see, it's obvious, right?

250
00:17:42,220 --> 00:17:45,790
This one's being passed in the parameters like, Oh, look, it's a car object.

251
00:17:45,790 --> 00:17:48,340
We can call a dot view on it and odometer.

252
00:17:49,060 --> 00:17:55,210
But if we don't have any dot fuel, if we just say fuel an odometer, there's like an invisible object

253
00:17:55,210 --> 00:18:00,880
right here before right here we have an object that's visible object dot.

254
00:18:01,210 --> 00:18:02,310
What about over here?

255
00:18:02,320 --> 00:18:05,350
There's no something dot odometer.

256
00:18:05,350 --> 00:18:07,000
There's no something dot fuel.

257
00:18:07,690 --> 00:18:14,710
Well, we could actually use the word this, and it would be the same thing can say this dot fuel.

258
00:18:16,300 --> 00:18:18,430
And I can say this odometer,

259
00:18:22,090 --> 00:18:28,120
and this might be a problem because it needs to be a pointer, actually, so this needs to have an arrow

260
00:18:28,120 --> 00:18:32,530
because of the this keyword so we can change it to an arrow.

261
00:18:32,980 --> 00:18:37,390
So rather than the DOT, it'll have an arrow because this is referred to as a pointer.

262
00:18:38,560 --> 00:18:42,390
So that's just the syntactical thing, but you can think of it as the same thing.

263
00:18:42,400 --> 00:18:45,580
There's object fuel, which is car to.

264
00:18:47,020 --> 00:18:48,370
Fuel fuel of car two.

265
00:18:49,300 --> 00:18:55,840
And then there's this air of fuel, which is car for in this example, right?

266
00:18:57,160 --> 00:18:59,140
So I just want to make that very clear.

267
00:18:59,140 --> 00:19:04,210
That's kind of the most confusing part is you're you wondering like, OK, well, what is this stuff

268
00:19:04,210 --> 00:19:04,960
over here like?

269
00:19:04,960 --> 00:19:07,060
These are just the variables I don't understand.

270
00:19:07,060 --> 00:19:09,880
How are you setting one object equal to another object?

271
00:19:09,910 --> 00:19:15,400
Well, this is the object, the word this, but we don't have to put it.

272
00:19:15,640 --> 00:19:20,620
It's kind of unnecessary because C++ knows that when you just say fuel, you're talking about this arrow

273
00:19:20,620 --> 00:19:20,980
fuel.

274
00:19:22,050 --> 00:19:23,550
Or this era odometer?

275
00:19:24,000 --> 00:19:24,360
All right.

276
00:19:25,140 --> 00:19:28,320
So let's get back to what we have to do for the rest of the code here.

277
00:19:28,350 --> 00:19:30,800
So one thing we have to change is we're not going to make a new vector.

278
00:19:30,810 --> 00:19:33,410
What we're going to do is clear the existing vector.

279
00:19:33,420 --> 00:19:37,510
So if anything is inside of upgrade codes, we want to clear it now.

280
00:19:38,640 --> 00:19:44,190
So we are going to say upgrade clothes, upgrade codes, dot clear.

281
00:19:45,570 --> 00:19:46,620
And this could actually.

282
00:19:46,740 --> 00:19:48,240
I think this needs to be an arrow.

283
00:19:49,890 --> 00:19:52,020
So we'll see upgrade codes aero clear.

284
00:19:54,990 --> 00:19:59,250
And then what we want to do is have this be.

285
00:20:01,340 --> 00:20:06,230
This is going to do the same thing, so it's just going to clear is going to remove everything in the

286
00:20:06,230 --> 00:20:08,830
victor, so clear it all out.

287
00:20:08,840 --> 00:20:16,370
So it's back to just being empty and then we can use the same loop to add all the stuff in OBJ, which

288
00:20:16,370 --> 00:20:21,800
for our example, is car to write everything and obviously will get copied over.

289
00:20:22,250 --> 00:20:27,800
And it actually it's going to like get added into the upgrade codes of great codes as empty after this

290
00:20:27,800 --> 00:20:28,130
line.

291
00:20:28,130 --> 00:20:33,590
And then all the stuff from object is moved over into upgrade codes or at least copied over is what's

292
00:20:33,590 --> 00:20:34,130
really happening.

293
00:20:34,790 --> 00:20:41,330
So I said that we had to return the object being called upon, so we're going to have to return this.

294
00:20:42,170 --> 00:20:45,400
So I'm going to say, return this, but I can't just return this.

295
00:20:45,410 --> 00:20:52,400
You see it underlined and read it says, Oh, you're returning car, you're trying to return car pointer

296
00:20:52,790 --> 00:20:54,200
because this is a pointer, right?

297
00:20:54,210 --> 00:20:57,920
That's why we had to save this aero field because this is an employer.

298
00:20:59,090 --> 00:21:03,590
So I'm going to have to reference this to return an actual car object, right?

299
00:21:04,850 --> 00:21:08,570
I said, this operator is going to return a car object by reference.

300
00:21:09,500 --> 00:21:12,950
So what I need to do now is put a star to reference this.

301
00:21:12,950 --> 00:21:17,330
And what it's really doing is it's going to return in this example car for.

302
00:21:19,790 --> 00:21:24,810
So go ahead and put a semicolon here, and this is all we really need, right?

303
00:21:24,830 --> 00:21:29,390
The only things that we changed from the copy constructor was that we cleared out the vector instead

304
00:21:29,390 --> 00:21:30,470
of making a new one.

305
00:21:31,820 --> 00:21:36,350
Right, so this having a sector made a new vector because we were trying to make a new object that was

306
00:21:36,350 --> 00:21:39,500
a copy of the other object, we're not trying to make a new object now.

307
00:21:39,500 --> 00:21:47,300
We're trying to change an object, we're trying to change car for which is this to be exactly whatever

308
00:21:48,410 --> 00:21:51,110
car to is, which is object in here.

309
00:21:53,030 --> 00:21:58,250
So kind of a lot confusing syntax, but you just you remember when Lincoln want to return a car by reference

310
00:21:58,250 --> 00:22:04,190
and that car is going to be key where this and you're going to have to reference it.

311
00:22:04,190 --> 00:22:05,480
So it's not a pointer anymore.

312
00:22:07,500 --> 00:22:09,690
And that we're just clearing out the venue this time.

313
00:22:10,290 --> 00:22:12,750
So let's go back over here and see how it's different.

314
00:22:13,860 --> 00:22:18,480
So we've added that in, I'm going to go ahead and debug this again, and let's see if we still have

315
00:22:18,480 --> 00:22:23,400
the problem with the ad upgrade being added to Car two as well.

316
00:22:25,240 --> 00:22:26,260
So I'm going to debug.

317
00:22:28,630 --> 00:22:33,670
So let's go ahead and go through here and then step over this line.

318
00:22:33,970 --> 00:22:36,390
Let's just confirm one car to his car.

319
00:22:36,400 --> 00:22:37,550
Two has upgrade code.

320
00:22:37,570 --> 00:22:42,760
Only one car floor has upgrade code only one.

321
00:22:43,240 --> 00:22:45,580
They are both white Ford Fiestas.

322
00:22:46,570 --> 00:22:48,370
Let's go ahead and step to the next line.

323
00:22:48,370 --> 00:22:49,870
So this line will execute here.

324
00:22:50,710 --> 00:22:51,700
So I step over.

325
00:22:52,210 --> 00:22:55,530
Let's check car for car for.

326
00:22:55,570 --> 00:22:56,950
Got to added to it.

327
00:22:57,940 --> 00:23:00,130
So let's go check car two to see if it fixed it.

328
00:23:00,880 --> 00:23:03,760
Notice it's not adding any more to car to.

329
00:23:03,970 --> 00:23:09,790
This is because we are actually referring to a different region of memory.

330
00:23:11,920 --> 00:23:16,540
So it's actually not trying to make, you know, use just copy over the pointer.

331
00:23:16,540 --> 00:23:18,190
It's actually changing stuff.

332
00:23:18,400 --> 00:23:22,210
So it just goes into the memory of the other car.

333
00:23:22,690 --> 00:23:25,390
So it's pretty cool.

334
00:23:26,560 --> 00:23:31,870
Kind of a little bit confusing the syntax for these things, but it's really just going to get easier

335
00:23:32,110 --> 00:23:33,490
over time as you use it.

336
00:23:33,490 --> 00:23:38,380
More often, the things that I talked to you about where the most important really, I think the concept

337
00:23:38,380 --> 00:23:47,290
of understanding that this stuff on the left side is really the same as this arrow fuel and this arrow

338
00:23:47,290 --> 00:23:49,590
odometer, you know, we can change it all to this.

339
00:23:51,220 --> 00:23:53,290
I'll just put this before everything.

340
00:23:57,910 --> 00:24:02,920
Or I can even leave some off like legality of some off here, I'll see this arrow on some and some I

341
00:24:02,920 --> 00:24:13,900
want to do this and I'll go ahead and stop this and I'll go ahead and show you that, you know, I'll

342
00:24:13,900 --> 00:24:18,550
make sure I save everything which it saves automatically when I'm debugging anyways.

343
00:24:18,550 --> 00:24:21,520
But we'll go ahead and debug again, and I'll show you that it doesn't matter.

344
00:24:21,520 --> 00:24:22,900
It'll be the same exact thing.

345
00:24:24,280 --> 00:24:26,470
So let's go ahead and step over.

346
00:24:28,120 --> 00:24:29,520
Car four equals car two.

347
00:24:29,800 --> 00:24:34,300
It still copies everything over, so upgrade code is one and still a white Ford Fiesta.

348
00:24:34,570 --> 00:24:36,490
All that stuff copying over.

349
00:24:36,790 --> 00:24:39,040
So look at this white Ford Fiesta.

350
00:24:39,550 --> 00:24:41,380
Let's take a look at our code again.

351
00:24:41,740 --> 00:24:46,300
So we had the this arrow on fuel odometer, painkiller and brand.

352
00:24:46,310 --> 00:24:48,700
So you already know that it's the same thing.

353
00:24:48,700 --> 00:24:54,040
I mean, it worked because paint, color and brand are using this arrow now.

354
00:24:55,230 --> 00:24:55,560
Right.

355
00:24:58,380 --> 00:24:59,760
Model is not.

356
00:25:00,710 --> 00:25:04,670
But you notice that they all couple copy over so brand is Ford.

357
00:25:05,600 --> 00:25:09,290
For both Katya and Carr for.

358
00:25:10,300 --> 00:25:15,380
And model does not use this, and it got copied over fine, too.

359
00:25:15,400 --> 00:25:17,860
It's fixed on this in this vest on this too.

360
00:25:19,440 --> 00:25:25,590
So this arrow is the same thing, is not included in all, but this kind of helps you understand which

361
00:25:25,590 --> 00:25:30,390
object is on which side and what you're really doing, and it helps you understand how this kind of

362
00:25:30,390 --> 00:25:40,770
works like it returns the object, the car for object so that it's able to copy over this stuff.

363
00:25:40,800 --> 00:25:47,640
So this gets passed to it, it gets copied over and then it needs to return it so it can actually have

364
00:25:47,640 --> 00:25:51,510
this new copying over car for with all the stuff coming over from car to.

365
00:25:53,630 --> 00:25:56,990
Of course, so that is all I'm going to go over in this lecture.

366
00:25:57,020 --> 00:26:04,310
Hopefully it was enough for you to understand the concept of just this operator with the assignment

367
00:26:04,310 --> 00:26:10,070
overloaded assignment operator in the next few lectures, we're going to be continuing on kind of probably

368
00:26:10,070 --> 00:26:13,010
dedicating one lecture for each operator.

369
00:26:13,010 --> 00:26:18,590
Sometimes maybe I'll include two if they're very closely related because it will be easy to transfer

370
00:26:18,590 --> 00:26:18,920
over.

371
00:26:19,940 --> 00:26:20,250
All right.

372
00:26:20,270 --> 00:26:21,950
So with that, I will see you in the next lecture.
