1
00:00:01,060 --> 00:00:04,930
OK, so welcome back to another lesson for object oriented programming.

2
00:00:06,040 --> 00:00:15,010
So in this lesson, we are going to talk about a few things you won't be as difficult as a lecture as

3
00:00:15,010 --> 00:00:15,690
previous ones.

4
00:00:15,700 --> 00:00:19,750
I'm not going to stuff as much information into it as previous lectures.

5
00:00:20,890 --> 00:00:28,660
So what we are going to go over is how to set up a kind of that object oriented programming multiple

6
00:00:28,660 --> 00:00:33,450
file situation here in Sea Lion.

7
00:00:34,750 --> 00:00:36,490
And then I'm going to.

8
00:00:37,830 --> 00:00:42,990
Show you a new concept called a decent destructor.

9
00:00:43,020 --> 00:00:46,470
It is similar to a constructor, but kind of the opposite thing.

10
00:00:46,950 --> 00:00:53,430
So rather than constructing an object, it will destroy an object and then we will finish up by looking

11
00:00:53,430 --> 00:00:59,490
at some pointers to objects just like we had pointers districts and we'll do dynamically allocated objects.

12
00:01:00,900 --> 00:01:02,340
So let's go ahead and get started.

13
00:01:03,150 --> 00:01:05,880
So right here, I just have made I've made a new project in here.

14
00:01:05,880 --> 00:01:09,660
I called it object oriented programming pointers to a folder.

15
00:01:10,260 --> 00:01:15,420
So I'm going to right click this folder and I'm going to say new and you notice that all this stuff

16
00:01:15,930 --> 00:01:16,440
pops up.

17
00:01:17,610 --> 00:01:26,100
Well, I'm actually going to say is, I'm going to say a C slash C++ header file, and I'm actually

18
00:01:26,100 --> 00:01:31,200
just going to call this car because I'm going to use the same car class.

19
00:01:32,100 --> 00:01:34,190
So I'm going to say a car.

20
00:01:34,200 --> 00:01:37,040
You can just leave this checked ad to targets.

21
00:01:37,050 --> 00:01:40,920
It doesn't really matter the targets as far as the compilation process, and we don't really need to

22
00:01:40,920 --> 00:01:45,570
compile the headers, but I'm just going to let it be added to the targets because it won't cause any

23
00:01:45,570 --> 00:01:46,260
problems right now.

24
00:01:48,390 --> 00:01:54,000
So I go ahead and add this, do you notice it nicely puts these macro guards here for us?

25
00:01:56,020 --> 00:02:00,280
So we get the macro guides here, and that even puts a little common here on that.

26
00:02:00,280 --> 00:02:04,330
And if so, all I do is copy paste some code here.

27
00:02:05,080 --> 00:02:09,310
So let me just go ahead and grab some code.

28
00:02:09,340 --> 00:02:14,800
I'm getting it from the window and I'm going to pay you the car class.

29
00:02:14,800 --> 00:02:17,350
So this is the definition.

30
00:02:18,440 --> 00:02:24,140
And then I'm actually going to show you how you can make some.

31
00:02:26,750 --> 00:02:33,110
A cool air and sea lion to deal with the implementation, so I'm actually getting it right, click this

32
00:02:33,110 --> 00:02:38,780
and say new, and this time I'm going to say C++ source file.

33
00:02:40,230 --> 00:02:43,920
So I will say, see slash C++ source file.

34
00:02:44,400 --> 00:02:50,850
We also could have done both of these by doing the C++ class, but I just am doing them separate.

35
00:02:52,090 --> 00:02:57,520
So this is going to be the class would have handled both of these at the same time, but now I'm actually

36
00:02:57,520 --> 00:03:03,400
saying explicitly, I'm going to make another source file, which this deep file.

37
00:03:06,610 --> 00:03:10,150
And then so what I can do is go in here and if I.

38
00:03:11,230 --> 00:03:16,450
If I do this, it can say generate definition for constructor car.

39
00:03:17,020 --> 00:03:23,110
So you notice I click that and what it does is it automatically created the constructor inside of this

40
00:03:23,110 --> 00:03:24,670
car that CBP file.

41
00:03:25,030 --> 00:03:32,230
It also went ahead and included Carnot CP for us, which is pretty cool.

42
00:03:34,840 --> 00:03:40,390
So we can do that for actually all of these, so I can just hover over this, I can say generally definition

43
00:03:40,390 --> 00:03:41,020
for that.

44
00:03:41,740 --> 00:03:46,260
Go back here, I could say generate definition for this and you can just keep going and it does all

45
00:03:46,280 --> 00:03:46,930
this code for you.

46
00:03:46,930 --> 00:03:53,050
It sets up the member function implementations for you in this implementation file card.

47
00:03:54,580 --> 00:03:56,680
So pretty nice thing for sure.

48
00:04:04,480 --> 00:04:07,180
Yeah, but you replace this.

49
00:04:08,780 --> 00:04:14,860
But yeah, I can go ahead and I'm going to finish just copying the rest because I don't really want

50
00:04:14,860 --> 00:04:22,870
to have to deal deal with all that, so I'm going to go ahead and grab it from afar right now.

51
00:04:25,560 --> 00:04:31,710
This is just a little too rough for me to type right now, so that's why I'm going to just kind of replace

52
00:04:31,710 --> 00:04:33,540
all this stuff here.

53
00:04:33,630 --> 00:04:40,260
Copy and paste, OK, so copy paste in all the stuff that we had previously in the implementation file

54
00:04:40,260 --> 00:04:41,250
of our car class.

55
00:04:42,540 --> 00:04:51,360
And then so now what I want to do is talk about this first new topic, which is the destructor.

56
00:04:51,660 --> 00:04:59,010
So the destructor is something that's very similar to the constructor, but it kind of cleans up the

57
00:04:59,010 --> 00:05:01,020
object in memory.

58
00:05:01,030 --> 00:05:04,920
So here is where we initialize that object, right?

59
00:05:05,550 --> 00:05:10,680
So what we're going to do to make it destructor is have something that looks a lot like a constructor,

60
00:05:11,370 --> 00:05:17,280
but the only difference is that it's going to have a tilde in front of it.

61
00:05:17,280 --> 00:05:24,720
So I'm going to say, tell the car and this one, I don't need to put anything inside of.

62
00:05:25,720 --> 00:05:31,870
These parentheses here, because I'm not it's not like we're passing explicit things to it.

63
00:05:32,680 --> 00:05:36,850
You know, you can also have a default constructor here, too.

64
00:05:37,330 --> 00:05:41,560
You notice that this right here is like a parameter based constructor.

65
00:05:42,070 --> 00:05:48,010
So we have our constructor accepting a bunch of parameters that the user sets, but we do not have like

66
00:05:48,010 --> 00:05:51,630
a basic variable default constructor, so we could put that as well.

67
00:05:51,630 --> 00:05:52,870
And you notice that highlights.

68
00:05:55,500 --> 00:05:59,310
So, you know, you could generate a definition for this, but C++.

69
00:05:59,880 --> 00:06:06,180
Well, go ahead and just have a default constructor for you as well, kind of automatically so you don't

70
00:06:06,180 --> 00:06:10,860
have to do that, but we do want to generate destructor just to have it in there.

71
00:06:10,870 --> 00:06:12,480
So I'll go ahead and click that.

72
00:06:12,480 --> 00:06:14,970
And now it makes a nice destructor for us here.

73
00:06:16,130 --> 00:06:17,900
In our implementation file.

74
00:06:19,100 --> 00:06:27,440
And so the thing about the destructor is it's kind of just called automatically when objects go out

75
00:06:27,440 --> 00:06:34,520
of scope and when I say that, I mean, like stack allocated objects, not dynamically allocated objects.

76
00:06:34,530 --> 00:06:37,970
So let's go ahead and make an example here.

77
00:06:38,030 --> 00:06:39,860
So I'm just going to put some code in here.

78
00:06:39,870 --> 00:06:43,970
Actually, I think I'll need to include

79
00:06:46,730 --> 00:06:47,660
extreme.

80
00:06:49,890 --> 00:06:55,560
And then one of my do is down here, I'm just going to print out a little message here, so I'm just

81
00:06:55,560 --> 00:07:06,210
going to say see out and inside the structure, cleaning up object.

82
00:07:08,770 --> 00:07:09,250
Cool.

83
00:07:09,490 --> 00:07:17,380
So I'm going to have to do this, so I'm not using namespace nastily, so Anderson here, just so you

84
00:07:17,380 --> 00:07:19,810
can see when the destructor is getting cold.

85
00:07:19,810 --> 00:07:25,030
So let's head over to main and I'm actually just going to copy paste some code here in Maine as well

86
00:07:25,040 --> 00:07:25,930
that we have before.

87
00:07:27,100 --> 00:07:33,700
So this is just making our car and car two objects, all that good stuff.

88
00:07:34,450 --> 00:07:37,720
So go ahead and paste that code here.

89
00:07:38,080 --> 00:07:44,380
I'm actually going to need to include a card h.

90
00:07:46,200 --> 00:07:50,010
So let me go ahead and paste this in here.

91
00:07:51,810 --> 00:08:00,780
So now I make a couple cars and we print out all of the information and stuff that we were before.

92
00:08:01,440 --> 00:08:06,420
Let's go ahead and try and compile and run this to see what prints out, and then we can see where the

93
00:08:06,420 --> 00:08:08,970
destructor is actually being called.

94
00:08:09,150 --> 00:08:12,450
So, you know, here is our here's our destructor.

95
00:08:12,450 --> 00:08:15,350
So we will see whenever we see this message print out.

96
00:08:15,360 --> 00:08:22,410
We know that in comparison to win this other stuff prints out, we can tell that once the objects are

97
00:08:22,410 --> 00:08:29,490
kind of out of scope, which means they're out of Maine's stack frame, then the structure will be called

98
00:08:29,490 --> 00:08:30,330
to clean that up.

99
00:08:31,400 --> 00:08:36,740
And just in a moment, we will be looking at a dynamically allocated objects using pointers and stuff,

100
00:08:36,740 --> 00:08:43,430
and we'll notice that the destructor can actually help us clean that up to accept that we have to explicitly

101
00:08:43,430 --> 00:08:46,340
delete dynamically allocated memory, remember?

102
00:08:46,880 --> 00:08:51,740
So let's go ahead and run this and see what we get.

103
00:08:54,440 --> 00:08:58,610
Cool, so you notice here that it printed out twice at the end of everything.

104
00:08:59,330 --> 00:09:00,470
So I'm going to try.

105
00:09:00,530 --> 00:09:01,060
Oh, cool.

106
00:09:01,070 --> 00:09:04,670
I can zoom in on the output as well, so hopefully that's easier for you to see.

107
00:09:05,390 --> 00:09:09,050
So this is all the standard stuff that we've seen print out before, right?

108
00:09:09,230 --> 00:09:17,180
We got these car miles like the initialized miles here that were sent to that constructor to add parameters.

109
00:09:17,960 --> 00:09:20,360
And then we see 12 blue, three seven black.

110
00:09:20,360 --> 00:09:23,760
This is all just from this code right here that we've seen before.

111
00:09:23,760 --> 00:09:25,160
It makes sense that it's printed out.

112
00:09:26,280 --> 00:09:29,360
You know, this is the last card I get Andy Card.

113
00:09:29,360 --> 00:09:32,000
I get paint right here on these two lines.

114
00:09:32,390 --> 00:09:39,400
And then once we leave the scope here, we notice that the destructor is called not once, but twice.

115
00:09:39,410 --> 00:09:40,740
And why is it called twice?

116
00:09:40,760 --> 00:09:44,750
Well, it's called for each object, and we made two objects.

117
00:09:45,230 --> 00:09:48,680
You can think of this class.

118
00:09:49,050 --> 00:09:55,760
Each object is a instance of all of this, you know, all of this file and all of this file.

119
00:09:56,660 --> 00:10:03,040
If you can think of it as a copy of this, like we have, you know, car is a copy of all of this and

120
00:10:03,040 --> 00:10:07,870
all of this together, and then Car two is a copy of all of this file and all of this file together.

121
00:10:08,890 --> 00:10:14,290
And so that's why when we made those two objects in Maine, once we leave the scope of Maine, it's

122
00:10:14,290 --> 00:10:16,630
time to clean up the destructing.

123
00:10:16,630 --> 00:10:19,510
It's called not once, but twice because we have two objects.

124
00:10:21,910 --> 00:10:24,700
So that's kind of just what the destructor does.

125
00:10:24,850 --> 00:10:26,890
It's not, you know.

126
00:10:27,810 --> 00:10:34,020
Incredibly interesting right now, maybe, but it is very important because we don't want memory leaks,

127
00:10:34,020 --> 00:10:34,380
right?

128
00:10:34,770 --> 00:10:36,420
We talked about memory leaks.

129
00:10:38,510 --> 00:10:46,040
With not cleaning up dynamically allocated objects, and, you know, this is something that is important

130
00:10:46,040 --> 00:10:52,430
just to clean up the object and its most common use case is that it will be called to clean up objects.

131
00:10:53,360 --> 00:10:59,270
If that is the case, something that you'll see later on is that if we have a class, there might be

132
00:10:59,270 --> 00:11:03,470
more code in here if we have a class that, let's say,

133
00:11:06,200 --> 00:11:09,050
has dynamically allocated.

134
00:11:10,830 --> 00:11:17,280
Members or something like that, one need to clean those up, like if we have some sort of container

135
00:11:17,490 --> 00:11:25,350
or data structure, something like that that is dynamically allocated and that is one of the data members,

136
00:11:25,380 --> 00:11:30,120
the like, you know, private or public, but most likely private data members.

137
00:11:30,510 --> 00:11:35,610
We all have to handle deleting that in this destructor.

138
00:11:35,610 --> 00:11:41,880
So we might have to like, iterate over certain containers that contain dynamically allocated objects

139
00:11:41,880 --> 00:11:45,060
and, you know, get rid of all the stuff in there.

140
00:11:45,060 --> 00:11:48,150
So if we had some sort of let's say we have like a vector.

141
00:11:49,500 --> 00:11:54,770
Of this class that was like right here, we had some vector in it, held a bunch of dynamically allocated

142
00:11:55,100 --> 00:11:57,650
memory like pointers to dynamically allocated memory.

143
00:11:57,830 --> 00:12:02,870
You might want to handle cleaning that up in the destructor of the class.

144
00:12:03,800 --> 00:12:08,270
So just kind of, you know, a little foreshadow of some stuff we might need to do in the future of

145
00:12:08,270 --> 00:12:08,870
this course.

146
00:12:09,260 --> 00:12:13,790
Most likely more towards the data structures and algorithms portion.

147
00:12:13,790 --> 00:12:18,620
But you never know we might encounter it in this first half of the course as well.

148
00:12:20,080 --> 00:12:24,970
So that shows where the instructors call, let's look at making some dynamically allocated objects here,

149
00:12:24,970 --> 00:12:32,440
so I'm going back to Maine, so I can say, I'm just say car.

150
00:12:37,490 --> 00:12:46,490
Same car cleaner car equals new car.

151
00:12:47,890 --> 00:12:52,270
So what I'm doing right now is I'm using this constructor.

152
00:12:53,320 --> 00:12:56,650
That has the perimeter as a surprise constructor.

153
00:12:56,710 --> 00:13:00,640
It's the one that takes parameters, not the default constructor, but.

154
00:13:02,980 --> 00:13:07,120
What I'm doing here, let me explain this line, so this is the data type right here.

155
00:13:07,150 --> 00:13:10,630
This is saying that the type of this data is car.

156
00:13:11,470 --> 00:13:17,590
This right here is, of course, saying, OK, well, it's going to be a pointer.

157
00:13:17,740 --> 00:13:21,610
You know, this is really actually going to be a pointer to a card data type.

158
00:13:21,610 --> 00:13:25,540
And the variable of this point is going to be called car.

159
00:13:26,410 --> 00:13:31,330
And then right here on the other side of the equals where of course, using the new keyword to dynamically

160
00:13:31,330 --> 00:13:34,540
allocate memory and create a new car object.

161
00:13:35,050 --> 00:13:41,230
So kind of similar to how he did structs, but what we're doing right now is we're doing we're using

162
00:13:41,230 --> 00:13:46,300
the parameter rise constructor of car, which is, you know, in the header.

163
00:13:46,630 --> 00:13:47,350
It is this.

164
00:13:47,950 --> 00:13:51,130
And in the implementation, of course it is this at the top.

165
00:13:51,760 --> 00:13:56,380
This is what we're calling when we're making this dynamically allocated object and we're creating a

166
00:13:56,380 --> 00:13:57,130
pointer to it.

167
00:13:57,470 --> 00:14:02,260
Of course, you notice these all turn red right now, and that's because we need to use an arrow, an

168
00:14:02,260 --> 00:14:02,650
arrow.

169
00:14:02,660 --> 00:14:08,290
You can see that it suggests that we can use the arrow so we can go ahead and click that, and it will

170
00:14:08,290 --> 00:14:13,180
change it automatically for us here as well so I can go through, hover and click.

171
00:14:14,280 --> 00:14:18,330
We're over this and click and just do that and all of this, which is kind of nice, honestly, because

172
00:14:18,330 --> 00:14:22,170
my wrist is a little bit hurt, so.

173
00:14:24,330 --> 00:14:30,600
And then you noticed that these are all just for car, but of course, we have car too is not being

174
00:14:30,600 --> 00:14:36,270
asked to be changed because we have not changed that to a dynamically allocated object.

175
00:14:37,500 --> 00:14:45,700
So since we dynamically allocate this, we're going to need to handle its deletion.

176
00:14:45,720 --> 00:14:48,120
So right now, we would have a memory leak.

177
00:14:48,360 --> 00:14:49,920
Let's go ahead and run the program.

178
00:14:52,740 --> 00:14:59,580
You notice that it only calls the destructor once now, and that's because the destructor got automatically

179
00:14:59,580 --> 00:15:06,150
called when this was a stack allocated object, which is like car to car to is the one that is responsible

180
00:15:06,150 --> 00:15:09,030
for this printing out right here, the destructor clean of the object.

181
00:15:09,030 --> 00:15:16,350
But since we have not explicitly called the delete key word on this dynamically allocated object, it's

182
00:15:16,350 --> 00:15:19,140
not deleting it and therefore it becomes a memory leak.

183
00:15:19,770 --> 00:15:20,680
So that's not good.

184
00:15:20,700 --> 00:15:22,860
So let's go ahead and handle the deletion of this.

185
00:15:22,860 --> 00:15:31,920
So down here I will say if you delete car and let's see what happens when I do that.

186
00:15:35,690 --> 00:15:42,800
So now you notice that, in fact, cause the destructor twice because it knows we wanted to delete that

187
00:15:42,800 --> 00:15:49,760
car object, it deletes not only car two this time, but it also first deletes car the original car,

188
00:15:49,760 --> 00:15:53,510
not car one, but this right here, which we had a point or two.

189
00:15:55,190 --> 00:15:58,550
So I'm just kind of kind of leave it at that.

190
00:15:59,030 --> 00:16:04,280
I think in the next lecture, I'm going to go over kind of an interesting concept.

191
00:16:04,280 --> 00:16:10,610
I wasn't going to do this at this point in time, but I think that it might be good to go over.

192
00:16:12,900 --> 00:16:20,610
Type of pointer that cleans itself up where you don't have to use the delete key word, so I might be

193
00:16:20,610 --> 00:16:26,880
going over that because I think that could be kind of useful, although I might not always use it in

194
00:16:26,880 --> 00:16:28,830
the in the course.

195
00:16:29,040 --> 00:16:34,770
But it is a really good thing to know because in modern C++, a lot of people are trying to use these

196
00:16:34,770 --> 00:16:40,710
things called smart pointers, where it's a little bit safer and you don't have to delete that, it'll

197
00:16:40,710 --> 00:16:41,670
clean up itself.

198
00:16:42,510 --> 00:16:48,540
So with that, I am going to finish up this lecture and I will see you in the next lecture.
