WEBVTT

00:00.150 --> 00:04.380
Hello again! In this video, we are going to look at base and derived classes.

00:06.660 --> 00:12.930
We're going to create a simple hierarchy which has a vehicle class and an aeroplane class.

00:14.130 --> 00:17.760
The vehicle is the basic or the more generic one,

00:17.760 --> 00:22.800
so that is going to be the base of the hierarchy. And the aeroplane is more specific,

00:23.220 --> 00:25.200
so that is going to be the derived class.

00:28.320 --> 00:33.730
And we are going to give them a member. So the vehicle has a maximum speed, and the aeroplane has a maximum

00:33.730 --> 00:34.140
height.

00:36.830 --> 00:38.090
To define a derived

00:38.190 --> 00:44.180
class, we make the usual class definition. Then, after the name of the class, we put a colon.

00:44.840 --> 00:51.920
And then the keyword, which will almost always be "public", and then the name of the class that we deriving

00:51.920 --> 00:53.000
from - the base class.

00:53.570 --> 00:57.440
So this says this aeroplane is derived from vehicle.

00:59.420 --> 01:05.420
And then this aeroplane class. The member functions will be able to call any member functions in the

01:05.420 --> 01:08.330
vehicle class, provided they are not private.

01:08.930 --> 01:14.330
And it can also access any data members of the vehicle class. Again, if they are not private.

01:15.850 --> 01:22.000
It can also write its own version of the base class member functions. So instead of calling a member

01:22.000 --> 01:26.680
function from the base class, it can define a member function, which has the same name, and use that

01:26.680 --> 01:27.130
instead.

01:31.860 --> 01:38.820
An object of a derived class is laid out in memory as a base class object, followed by the derived

01:38.820 --> 01:39.480
class part.

01:39.810 --> 01:43.440
So we have some memory here, which is an aeroplane object.

01:44.160 --> 01:49.320
The first part of this is going to be the vehicle part of the object, and the rest of it will be the

01:49.320 --> 01:51.570
bits that are specific to an aeroplane.

01:52.560 --> 01:57.180
When we create this object, the aeroplane constructor is called, but this will call the vehicle constructor

01:57.180 --> 01:57.600
first.

01:57.990 --> 02:04.590
So this bit gets initialized before any of the aeroplane parts get initialized. And for destruction,

02:04.590 --> 02:05.520
it is the opposite way

02:05.520 --> 02:11.460
round. The aeroplane destructor is called and returns before the base class

02:11.670 --> 02:12.720
destructor is called.

02:16.470 --> 02:23.040
So we need to call the base class constructed, and we do that using the same syntax as if the base

02:23.040 --> 02:26.370
class was a member of the aeroplane class.

02:27.410 --> 02:32.030
So we just put a colon, the base class name, and any arguments in the brackets.

02:36.160 --> 02:42.340
(Just find myself some room!) if there are arguments to the constructors, then each class will in effect

02:42.340 --> 02:43.780
initialize its own members.

02:44.140 --> 02:50.980
So if we have the max speed and max height member variables again, the vehicle class will initialize

02:51.310 --> 02:52.900
the max_speed variable.

02:53.680 --> 02:58.600
And when the aeroplane constructor calls the vehicle constructor, it'll need to pass the max_speed to

02:58.600 --> 02:58.900
that.

02:59.710 --> 03:02.920
Then the aeroplane will initialize its own max_height variable.

03:05.260 --> 03:11.410
So this is our hierarchy, with the vehicle class and the max_speed member, and the aeroplane class, which

03:11.410 --> 03:14.950
is derived from the vehicle and has a max_height member.

03:16.310 --> 03:21.200
So the member functions in this can call any member functions from vehicle, and they can also access

03:21.200 --> 03:25.760
the max speed member of vehicle, as though it was a member of aeroplane.

03:28.140 --> 03:29.940
So I have added the constructors.

03:30.360 --> 03:33.330
I'm going to print out a statement, just so we can see what will happen.

03:34.080 --> 03:38.670
So there is the vehicle constructor, which is going to initialize the vehicles private member.

03:40.030 --> 03:46.630
The aeroplane constructor will call the vehicle constructor, and pass the arguments to the vehicle constructor.

03:47.170 --> 03:50.560
And then it will initialize the max_height member of the aeroplane.

03:52.880 --> 03:58.460
And then, in our main() routine, we just create a couple of objects, one for each class.

04:00.530 --> 04:05.240
So when we create the vehicle object, that calls the vehicle construction. So that is just a normal

04:05.240 --> 04:11.750
class. When we create an aeroplane object, that called the vehicle constructor first and then the aeroplane

04:11.810 --> 04:12.410
constructor.

04:14.430 --> 04:15.600
And finally, we can.

04:16.590 --> 04:21.960
And finally, we can carry on deriving. So we can derive another class from aeroplane. So we could

04:21.960 --> 04:28.080
have, for example, a fighter plane, which is a more specialized aeroplane. And we use the same syntax

04:28.080 --> 04:32.030
agains, so class fighter plane, colon, public aeroplane.

04:32.400 --> 04:35.040
So fighter plane is derived from aeroplane.

04:36.120 --> 04:39.840
It gets a bit confusing talking about base and derived classes in this context.

04:40.320 --> 04:42.330
So we often talk about parent and child.

04:45.020 --> 04:51.380
So vehicle is the parent of aeroplane. Aeroplane is the child of vehicle. Aeroplane is the parent

04:51.380 --> 04:55.400
of fighter plane, and fighter plane is the child of aeroplane.

04:56.670 --> 04:59.220
So this is what the entire hierarchy would look like.

04:59.550 --> 05:06.300
We have the vehicle class as the base, then we have the aeroplane, which is a child of the vehicle. And

05:06.300 --> 05:09.780
then we have the fighter plane, which is the child of the aeroplane class.

05:10.410 --> 05:16.320
So this fighter plane has the aeroplane as his parent, and the aeroplane has the vehicle as its parent,

05:16.320 --> 05:16.860
in turn.

05:17.880 --> 05:19.500
Okay, so that is it for this video.

05:20.010 --> 05:23.010
I will see you next time, but until then, keep coding!
