1
00:00:00,630 --> 00:00:00,990
OK.

2
00:00:01,020 --> 00:00:08,550
Welcome back to a another lesson on inheritance in this lesson, I am going to be kind of extending

3
00:00:08,640 --> 00:00:15,930
what we talked about before and adding one or two new things to our knowledge base about inheritance.

4
00:00:16,680 --> 00:00:17,940
It's going to be kind of a short lecture x.

5
00:00:17,940 --> 00:00:23,220
I'm only going to go over these few things, but definitely pay attention because they will be important.

6
00:00:23,940 --> 00:00:30,270
So what we're going to go over is how the constructors and D structures work in inheritance.

7
00:00:31,330 --> 00:00:34,840
As in like, when is which constructor called?

8
00:00:35,830 --> 00:00:42,640
And we're also going to go over this thing that we talked about, which I said you couldn't ignore in

9
00:00:42,640 --> 00:00:47,860
the last lecture because I just wanted you to know that you could use this for inheritance, but this

10
00:00:47,860 --> 00:00:52,510
word right here can be changed from public to other words as well.

11
00:00:53,320 --> 00:01:00,040
And we're going to talk about this base class access specification, which is what this is right here

12
00:01:00,340 --> 00:01:08,020
and how it relates to the base class and the difference between that and this right here.

13
00:01:08,020 --> 00:01:13,600
Because remember, this used to be private at some point and we changed it to protect it so we could

14
00:01:13,600 --> 00:01:18,400
have access inside of this friend.

15
00:01:18,400 --> 00:01:23,500
Function, right, are overloaded upstream operator and we wanted access to the private members assist

16
00:01:23,500 --> 00:01:24,480
declared in Karjat.

17
00:01:24,490 --> 00:01:24,790
Eight.

18
00:01:24,850 --> 00:01:26,770
It is a protected member.

19
00:01:27,130 --> 00:01:32,980
Protected means that we have access to these in the base class.

20
00:01:32,980 --> 00:01:40,780
These members inside of the derived classes rightly derive classes have access to these now if we put

21
00:01:40,780 --> 00:01:41,320
protected.

22
00:01:42,970 --> 00:01:47,350
So the point is, though, is that we're going to talk about this access, specify a lot and what it

23
00:01:47,350 --> 00:01:48,320
means if we change it.

24
00:01:48,340 --> 00:01:51,700
So first off, we're going to talk about the constructors and D structures, though.

25
00:01:52,630 --> 00:01:59,410
So in each class, in each derive class and base class, I have added a destructor.

26
00:02:00,710 --> 00:02:10,790
And I have put a printout line in the destructor saying that we are in it, so we are in the sedan destructor.

27
00:02:11,660 --> 00:02:17,520
I'm doing that for each destructor and I put a little sea out in the constructor as well.

28
00:02:17,540 --> 00:02:23,660
So constructor and destructor, I have little printout statements that will tell us that we are in there,

29
00:02:23,660 --> 00:02:24,840
so we'll know when it's called.

30
00:02:24,840 --> 00:02:28,220
And I did that for the base class and all of the drive classes.

31
00:02:29,420 --> 00:02:34,730
So what I'm going to do is go ahead and run this program now, but I'm not going to print out info about

32
00:02:34,730 --> 00:02:39,140
the cars using the overloaded operator because that will create unnecessary clutter.

33
00:02:39,170 --> 00:02:46,580
What we want to do is just kind of see the printouts and know what order the constructors are called

34
00:02:46,580 --> 00:02:52,070
in and what order the structures are called in in terms of the base class versus the derived class.

35
00:02:53,150 --> 00:02:57,860
So let's go ahead and run this and we will see what the output is.

36
00:03:03,140 --> 00:03:06,230
OK, so let's go ahead and zoom in on this a little bit.

37
00:03:06,980 --> 00:03:15,770
I'm going to scroll up a bit, so we see that we are first in the car constructor and then we are in

38
00:03:15,770 --> 00:03:19,550
the truck constructor, then car, then sedan, then car, then SUV.

39
00:03:20,510 --> 00:03:26,430
So notice that we instantiate our objects in this order truck and then sit in an SUV.

40
00:03:26,450 --> 00:03:33,200
So what happens with the constructor in inheritance is that the base class constructor will be called

41
00:03:33,200 --> 00:03:35,440
first before the derived class constructor.

42
00:03:35,450 --> 00:03:37,550
So you notice we instantiate a truck, right?

43
00:03:38,240 --> 00:03:44,270
So what C++ needs to do is it needs to sees that you instantiate a truck and then it needs to look at

44
00:03:44,270 --> 00:03:47,630
the superclass or base class of truck, which is car, right?

45
00:03:48,080 --> 00:03:50,570
So it goes to car called the car constructor.

46
00:03:50,870 --> 00:03:53,330
Then it comes back and does the truck constructor.

47
00:03:54,650 --> 00:03:57,170
When we do a sedan, it has to repeat the same process over.

48
00:03:57,180 --> 00:03:59,940
It says, OK, what's the superclass of sedan with the car?

49
00:04:00,200 --> 00:04:05,220
I go to a car called Car Constructor, then calls a day and it does the same thing for SUV.

50
00:04:05,220 --> 00:04:06,530
You see car and then SUV.

51
00:04:07,190 --> 00:04:10,550
The destructor is the mirror image of this right here.

52
00:04:10,610 --> 00:04:18,350
It's basically inverted, so you notice that it destroys the most recently created derived class object.

53
00:04:18,650 --> 00:04:22,790
So SUV and then it goes to the car, right?

54
00:04:22,790 --> 00:04:25,700
And then the sedan and then car and truck and then car.

55
00:04:25,910 --> 00:04:33,200
So the whole ordering is reversed in terms of this and also it is reversed in terms of whether the base

56
00:04:33,200 --> 00:04:35,240
class or drive class is called first.

57
00:04:35,240 --> 00:04:42,920
So the derived class destructor is called before the base class destructor opposite of the constructor,

58
00:04:42,920 --> 00:04:46,400
where the base class constructor is called before the derived class constructor.

59
00:04:47,210 --> 00:04:51,650
So you can just see that it happens in this order, and then it's just a mirror image right here.

60
00:04:51,860 --> 00:04:58,550
So this part right here is the inverse of the constructor up here as far as ordering.

61
00:05:00,050 --> 00:05:04,160
So just wanted to show you that because it's kind of important to know what's going on and what's happening

62
00:05:04,160 --> 00:05:04,820
and what order.

63
00:05:06,170 --> 00:05:14,030
So the next thing that I want to talk to you about is this base class access specification keyword here.

64
00:05:15,230 --> 00:05:17,210
So right now we have public.

65
00:05:17,720 --> 00:05:24,260
So what this word really means is it describes how the.

66
00:05:25,710 --> 00:05:33,120
Functions here and members and everything are going to be inherited into the derived class.

67
00:05:34,960 --> 00:05:36,790
So since we say public.

68
00:05:38,190 --> 00:05:42,130
That means that all of these methods here.

69
00:05:42,150 --> 00:05:48,810
Sorry, all of these member functions here, I keep calling them methods because that's a way to refer

70
00:05:48,810 --> 00:05:50,550
to them in other programming languages.

71
00:05:52,010 --> 00:06:01,370
All of these member functions here are going to be inherited into Sudan and they are going to be made

72
00:06:01,370 --> 00:06:02,480
accessible.

73
00:06:03,480 --> 00:06:11,010
To Maine, for example, from Sudan as public member functions, so.

74
00:06:12,090 --> 00:06:14,430
It says, you know, these are public right here.

75
00:06:16,510 --> 00:06:23,290
It's that means that what's going to happen when we inherit this as public, it's going to make those

76
00:06:23,290 --> 00:06:26,480
functions public through the sedan class as well.

77
00:06:26,500 --> 00:06:33,550
So imagine that we just take all this highlighted stuff here and we just copy paste it here into this

78
00:06:33,550 --> 00:06:34,300
public section.

79
00:06:34,360 --> 00:06:35,830
That's basically what's happening.

80
00:06:36,520 --> 00:06:44,710
It means that all of these highlighted member functions here are going to become public in Sudan and

81
00:06:44,710 --> 00:06:46,390
so we can use them in Maine.

82
00:06:47,590 --> 00:06:54,550
What I can do is change this if I change this to protected.

83
00:06:57,510 --> 00:07:04,440
What happens is that you notice that there was a protected word right here, right, and that gave us

84
00:07:04,440 --> 00:07:10,680
access to these inside of the derived classes, right?

85
00:07:10,690 --> 00:07:12,390
All the derived classes.

86
00:07:13,830 --> 00:07:15,780
If I put a protected.

87
00:07:17,690 --> 00:07:26,120
Right here, what it means is that it's going to when we copy all of this and like invisibly make it

88
00:07:26,120 --> 00:07:27,320
available here.

89
00:07:28,300 --> 00:07:32,500
In our sedan class, it's not going to be public anymore.

90
00:07:32,540 --> 00:07:36,550
These are not going to be public anymore when they're here in sedan.

91
00:07:36,580 --> 00:07:39,340
They're going to be protected in what is protected.

92
00:07:39,340 --> 00:07:47,850
Mean again, it means that they're only available to classes that derive from the classes as protected.

93
00:07:47,860 --> 00:07:56,770
So if we had another class that was deriving from sedan as a sedan was the super class, then that means

94
00:07:56,770 --> 00:08:00,130
that the protected car.

95
00:08:01,270 --> 00:08:05,630
Member functions here would be available to that class that was deriving from it.

96
00:08:05,650 --> 00:08:10,780
But the problem is is it's not available elsewhere in the client code.

97
00:08:10,810 --> 00:08:12,790
Watch what happens when I go to Maine.

98
00:08:14,180 --> 00:08:22,370
Immediately, these two methods coming from car, right, set fuel and set paint are actually coming

99
00:08:22,370 --> 00:08:23,280
from car.

100
00:08:23,630 --> 00:08:25,040
You see set fuel and step paint.

101
00:08:26,980 --> 00:08:31,300
They're red now because we cannot access them in Maine because they are protected.

102
00:08:31,480 --> 00:08:40,150
They are only available to whatever classes may derive from Sudan because they're being exposed to this

103
00:08:40,150 --> 00:08:43,360
client code in a protected manner, in a public manner.

104
00:08:43,870 --> 00:08:49,240
If I go right here, it says that fuel is a protected member of car member of car that's just telling

105
00:08:49,240 --> 00:08:51,640
us that we cannot access that from here in Maine.

106
00:08:51,790 --> 00:08:54,310
But notice how swetnam doors is still fine.

107
00:08:54,700 --> 00:08:55,540
Why is that?

108
00:08:56,170 --> 00:09:01,800
Because we put Satnam Dawes is actually a member of Sudan.

109
00:09:01,810 --> 00:09:03,070
It's not a member of car.

110
00:09:04,400 --> 00:09:07,220
And what did we say about the members of Sudan?

111
00:09:07,760 --> 00:09:10,040
We said that their public right here, right?

112
00:09:10,820 --> 00:09:13,250
That's why this is accessible in Maine.

113
00:09:13,880 --> 00:09:17,540
Yet these set feelings that paint are not accessible in Maine.

114
00:09:17,540 --> 00:09:17,920
Why?

115
00:09:17,930 --> 00:09:20,120
Because they are protected.

116
00:09:20,120 --> 00:09:21,530
We said protected car.

117
00:09:22,500 --> 00:09:26,260
So now when we grab all this, these are all protected, they're not available to me.

118
00:09:26,460 --> 00:09:33,720
That's why when we tried to call those functions here in Maine, set fuel, set paint, they are actually

119
00:09:33,720 --> 00:09:34,970
not available to me.

120
00:09:35,010 --> 00:09:37,680
So we would want to leave this as public, right?

121
00:09:38,460 --> 00:09:39,810
We put it back to public.

122
00:09:40,320 --> 00:09:41,820
You notice that these disappear.

123
00:09:43,570 --> 00:09:48,370
So there is one more thing to do, and that is private.

124
00:09:49,570 --> 00:09:56,560
And it's the same thing is protected, except rather than exposing the inherited member functions here

125
00:09:56,560 --> 00:10:03,670
as protected, it's now exposing them as private so they're not available either in Maine.

126
00:10:03,700 --> 00:10:08,860
So if I go here now, it says Fairfield is a private member of CAR.

127
00:10:10,450 --> 00:10:15,430
So that means that it's private, constrained by private inheritance here, and I click there, and

128
00:10:15,430 --> 00:10:19,630
it brings us to here and it's just like, Hey, you made it private, you made everything that we're

129
00:10:19,630 --> 00:10:26,230
inheriting from car all private, so it is inaccessible to any client code.

130
00:10:27,880 --> 00:10:34,270
So that's just something to note, that's what the access specified means, the access, the base class

131
00:10:34,270 --> 00:10:39,340
access specify air, which is the way that you inherit stuff from the base class.

132
00:10:41,450 --> 00:10:48,710
Whatever word you put here that you're just describing, the way that it's going to be accessible from

133
00:10:48,710 --> 00:10:49,370
Sudan.

134
00:10:49,490 --> 00:10:57,470
So remember, protected one might be nice if you're wanting to make another derived class of Sudan,

135
00:10:57,470 --> 00:11:02,390
where Sudan is like a super class and now you have multiple levels of inheritance.

136
00:11:03,830 --> 00:11:12,740
That way, you would expose all of car to that like derived class of Sudan in a protected manner, so

137
00:11:12,740 --> 00:11:16,220
you could use it in that derived class from Sudan.

138
00:11:17,370 --> 00:11:19,890
Public will make stuff from car

139
00:11:22,500 --> 00:11:26,790
be accessible to client code just like these are.

140
00:11:26,820 --> 00:11:28,100
I mean, these are public, right?

141
00:11:28,110 --> 00:11:34,170
So if you're like, well, I'm also going to want in my client code to call all of cars member functions

142
00:11:34,170 --> 00:11:36,630
here, then you need to make this public.

143
00:11:38,400 --> 00:11:38,760
All right.

144
00:11:38,760 --> 00:11:41,870
So that was kind of it for this lesson.

145
00:11:41,880 --> 00:11:45,230
I just you know what this means and how you can change it.

146
00:11:45,240 --> 00:11:49,560
You know, if you're using multiple levels of inheritance, then you might be putting protected here.

147
00:11:50,520 --> 00:11:54,390
And there are some reasons why you might want to put money.

148
00:11:54,420 --> 00:11:55,650
You might want to put private.

149
00:11:55,800 --> 00:12:00,330
Maybe you still want to just use some of the methods in here.

150
00:12:02,400 --> 00:12:08,550
You know, stuff is still stuff is still accessible to car.

151
00:12:09,120 --> 00:12:12,420
If it's protected, you know, it doesn't, it doesn't matter what you feel like.

152
00:12:12,420 --> 00:12:20,190
If I put private here, even I could still use stuff from car in sedan.

153
00:12:20,370 --> 00:12:20,760
Right?

154
00:12:21,090 --> 00:12:25,170
So for example, I can go over here to sedan, even though this is private, you know, this doesn't

155
00:12:25,170 --> 00:12:25,920
work here anymore.

156
00:12:26,880 --> 00:12:30,330
But if I go to sedan, let's just put something super random.

157
00:12:30,330 --> 00:12:30,720
So.

158
00:12:32,100 --> 00:12:38,490
Let's just say in setting some doors, we also want to just randomly display.

159
00:12:40,730 --> 00:12:43,880
Let's see, like the odometer or something like that.

160
00:12:44,570 --> 00:12:45,170
I don't know.

161
00:12:48,250 --> 00:12:52,720
Let's just say get pain or something like that.

162
00:12:53,020 --> 00:12:55,390
Like, what if we just want to show you the pain as well?

163
00:12:55,390 --> 00:13:00,580
And it's like, so whenever you set the number of doors, we just have this thing that's like, uh,

164
00:13:01,510 --> 00:13:07,750
you know, by the way, the paint color is.

165
00:13:10,090 --> 00:13:10,720
Like that.

166
00:13:12,070 --> 00:13:17,200
So what I can do in here is actually use this function in here, get paint.

167
00:13:17,950 --> 00:13:28,750
So if I go back here, I can say, by the way, the paint color is in, I can just say get paint because

168
00:13:28,750 --> 00:13:32,170
I have access to this function because it is inherited.

169
00:13:32,320 --> 00:13:32,710
All right.

170
00:13:33,190 --> 00:13:33,610
So.

171
00:13:34,670 --> 00:13:42,290
It's not really saying, like all this stuff is public in terms of my sedan class, when I say private,

172
00:13:42,290 --> 00:13:45,290
it means that when I'm using these.

173
00:13:46,740 --> 00:13:51,870
When I copy all these over to Sudan, they become they're basically going to be put down here in this

174
00:13:51,870 --> 00:13:52,410
private.

175
00:13:53,160 --> 00:13:54,180
That's what you can imagine.

176
00:13:54,180 --> 00:13:59,370
Like they're they're basically like put down here in the private list because all of these methods are

177
00:13:59,610 --> 00:14:05,340
private in terms of the Sudan in which it's saying that I'm exposing these in a private way to the clan

178
00:14:05,350 --> 00:14:05,670
code.

179
00:14:06,240 --> 00:14:09,780
So we still have full accessibility to all of this in the Sudan class.

180
00:14:09,990 --> 00:14:16,410
So no matter what word you put right here, private protected public, you're still going to have access

181
00:14:16,410 --> 00:14:20,190
to everything here in car because you're inheriting it right?

182
00:14:21,580 --> 00:14:29,380
If you were to put all of this highlighted junk down here in protected and changes to private or something,

183
00:14:29,680 --> 00:14:34,270
then they would be private to car and the inheriting class would not have access to it.

184
00:14:35,290 --> 00:14:37,420
So you notice, like, let's take a look here.

185
00:14:38,470 --> 00:14:39,790
This is totally fine, right?

186
00:14:40,810 --> 00:14:42,550
And I can go ahead and test this out.

187
00:14:42,550 --> 00:14:43,460
I can print it out.

188
00:14:43,480 --> 00:14:44,820
We'll go back to Maine.

189
00:14:44,830 --> 00:14:46,570
I've commented this stuff out.

190
00:14:47,200 --> 00:14:47,500
All right.

191
00:14:47,530 --> 00:14:50,230
So let's go ahead and run this.

192
00:14:54,950 --> 00:14:59,540
So you notice this says, by the way, the pink color is white, so you see that right there, right?

193
00:15:01,190 --> 00:15:07,880
So if I can go if I go back here to car and I was to change this from public to private.

194
00:15:10,630 --> 00:15:15,520
We start running into a ton of issues, right, if I go back here to Main, you noticed that like none

195
00:15:15,520 --> 00:15:22,060
of these are accessible, like now, these aren't accessible because they're private members of car.

196
00:15:22,330 --> 00:15:26,170
And if I go to Sudan, this is red too, because it's a private number of cars.

197
00:15:26,170 --> 00:15:29,530
So I don't even have access inside of this sedan derived class.

198
00:15:29,980 --> 00:15:33,040
So that's why these should be public.

199
00:15:34,960 --> 00:15:39,970
And I just wanted to show you that if you were to change this to private, that makes that changes the

200
00:15:39,970 --> 00:15:42,520
accessibility inside the derived classes.

201
00:15:42,880 --> 00:15:50,590
But as far as inheritance, it changes the accessibility as far as anything that wants to get access

202
00:15:50,590 --> 00:15:58,810
to the sedan, class client code, whoever imports it, like up here, you know, like we have day and

203
00:15:58,960 --> 00:15:59,320
age.

204
00:15:59,950 --> 00:16:07,880
That means that the way that we describe the inheritance is kind of what affects the accessibility we

205
00:16:07,880 --> 00:16:12,640
would have in Maine to some of the methods that got inherited from car, right?

206
00:16:12,880 --> 00:16:19,120
All of this stuff it now becomes part of sedan is defined at the accessibility level by this word.

207
00:16:22,040 --> 00:16:22,910
All right, cool.

208
00:16:22,940 --> 00:16:26,540
So I'm actually going to make this public because I want that to be public, right?

209
00:16:26,540 --> 00:16:27,780
And I'm going to change this back.

210
00:16:27,800 --> 00:16:31,490
This makes no sense to have it here, so I get rid of that.

211
00:16:33,220 --> 00:16:33,610
Cool.

212
00:16:36,610 --> 00:16:42,070
So now everything you notice will be accessible in here, so if I in peace, they are accessible, and

213
00:16:42,070 --> 00:16:44,590
that's why I believe that as public in here.

214
00:16:44,890 --> 00:16:49,630
So that's pretty much all I had to say about those two kind of sub topics.

215
00:16:49,840 --> 00:16:53,290
It's important to know which order things are being called in as far as inheritance.

216
00:16:53,590 --> 00:16:58,540
And important to know the different types of access specification that you can do.

217
00:16:58,750 --> 00:17:05,260
If at some point you end up having multiple levels of inheritance, you'll need to know about protected

218
00:17:05,260 --> 00:17:06,390
and stuff like that, right?

219
00:17:06,400 --> 00:17:11,410
Because you may not want to make those members here accessible all the way down to the client.

220
00:17:11,410 --> 00:17:17,650
Could you maybe just want to have it accessible to a derived class of sedan or truck or SUV, whatever

221
00:17:17,650 --> 00:17:19,120
you end up trying to implement?

222
00:17:21,390 --> 00:17:24,510
All right, so with that, I will see you in the next lecture.
