WEBVTT

00:00.210 --> 00:04.080
Hello again! In this video, we are going to look at member functions and inheritance.

00:05.520 --> 00:12.180
When we inherit from a parent class which has member functions, the child will, so-called, "inherit"

00:12.180 --> 00:15.300
all these member functions, provided they are not private.

00:15.810 --> 00:17.550
So this means that it will appear

00:17.550 --> 00:21.450
that the child class defines the same functions as the parent does.

00:22.290 --> 00:26.940
For example, if we have our Vehicle class and we give it start() member function which is public,

00:27.660 --> 00:33.900
then our derived Aeroplane class will also appear to have the same start() public member function.

00:37.120 --> 00:42.910
So in this code, if we create an Aeroplane object and then call the start() member function, this

00:42.910 --> 00:47.320
is going to call the inherited member function. And there we go.

00:51.160 --> 00:55.810
What happens if the child wants the member function to do something different? For example, with

00:55.810 --> 01:01.570
an aeroplane, you cannot just jump in and turn the key, you have to do a lot more checks and so on.

01:02.260 --> 01:07.450
In that case, the child class can implement the member function itself, so it can have its own definition,

01:07.450 --> 01:09.430
which does something which is more appropriate.

01:11.240 --> 01:16.670
So with the same code again, but with the child class defining its own member function which re-implements

01:16.670 --> 01:17.150
the parent's.

01:17.630 --> 01:22.140
So you can see, this just holds the child's member function and not the parent's one.

01:23.890 --> 01:29.020
Sometimes the parent member function does something which is useful to the child, but the child

01:29.020 --> 01:30.490
needs to do some more work.

01:31.240 --> 01:36.790
So in that case, you could have re-implement the function and call the parent's member function at the

01:37.150 --> 01:38.140
strategic moment.

01:38.650 --> 01:41.440
So for example, with our aeroplane, we could carry out our checks.

01:41.830 --> 01:46.510
Then we start the engine and then we tell the air traffic control that we are ready to take off.

01:49.370 --> 01:51.170
So it will look like this.

01:53.210 --> 01:58.760
So we do our thing, then we call the parent's member function, than we do some more work.

02:00.410 --> 02:03.440
You might notice that I have been very careful with my wording.

02:03.770 --> 02:07.940
I did not say that the child inherits public member functions.

02:08.300 --> 02:12.450
I said that the child inherits member functions which are not private.

02:12.980 --> 02:17.870
And there is a reason for that, which is that there are actually three access specifiers in C++.

02:18.530 --> 02:19.820
So let's quickly recap.

02:20.390 --> 02:25.520
So when we have a class with a public member, that means that member can be accessed from anywhere

02:25.520 --> 02:26.060
in the code.

02:26.630 --> 02:29.900
That is part of the interface that the class has to the rest of the code.

02:31.510 --> 02:35.860
With a child class, the child will have access to all the parent's public members.

02:36.490 --> 02:38.920
It does not have access to any of the private members.

02:39.640 --> 02:43.060
So in that respect, the child class is no different from any other code.

02:44.490 --> 02:50.670
Sometimes it's useful to have member functions in the parent, which can be used by child classes,

02:50.670 --> 02:52.410
but not by any other code.

02:52.920 --> 02:55.200
So that would be part of the class's implementation.

02:56.100 --> 02:59.460
And to do that, we use the protected access specifier.

02:59.790 --> 03:01.860
So we put "protected", followed by a colon.

03:02.460 --> 03:08.220
And then everything which comes after that will be protected until the next access specifier, or at the

03:08.220 --> 03:09.420
end of the class definition.

03:09.930 --> 03:13.440
So this could be, for example, inside the Vehicle class definition.

03:15.440 --> 03:20.240
And anything that goes in here will be accessible to classes which are derived from Vehicle, but

03:20.570 --> 03:21.650
not to anything else.

03:22.850 --> 03:26.390
You can make data members protected, but that is not very useful.

03:26.900 --> 03:29.270
Normally, you would only make member functions protected.

03:29.870 --> 03:35.030
So this acts as a kind of second interface, which is available within the Vehicle hierarchy, but not

03:35.360 --> 03:36.440
anywhere outside it.

03:37.600 --> 03:43.470
So this will maintain encapsulation. These implementation details will be hidden from the outside world,

03:43.780 --> 03:45.910
but they will be available inside the hierarchy.

03:48.950 --> 03:53.210
So let's try that out. So we have our vehicle class with a protected member function.

03:54.240 --> 04:00.510
We have our Aeroplane class, which has a member function which calls the protected member functions, and

04:00.510 --> 04:02.010
we create a Vehicle object.

04:04.780 --> 04:09.910
So that does actually compile. We get a warning about not using this variable, but that does not matter

04:09.910 --> 04:10.210
here.

04:13.030 --> 04:19.840
So if we uncomment this, so we are going to call this protected member function in the main() function.

04:20.110 --> 04:23.800
This is not code which is part of a member function in a derived class.

04:24.310 --> 04:25.720
So this should fail.

04:30.230 --> 04:34.610
And there we are. "Cannot access protected member declared in class Vehicle".

04:36.470 --> 04:38.090
OK, so that is it for this video.

04:38.480 --> 04:41.450
I will see you next time, but until then, keep coding!
