Hello again! In this video, we are going to look at base and derived classes.
We're going to create a simple hierarchy which has a vehicle class and an aeroplane class.
The vehicle is the basic or the more generic one,
so that is going to be the base of the hierarchy. And the aeroplane is more specific,
so that is going to be the derived class.
And we are going to give them a member. So the vehicle has a maximum speed, and the aeroplane has a maximum
height.
To define a derived
class, we make the usual class definition. Then, after the name of the class, we put a colon.
And then the keyword, which will almost always be "public", and then the name of the class that we deriving
from - the base class.
So this says this aeroplane is derived from vehicle.
And then this aeroplane class. The member functions will be able to call any member functions in the
vehicle class, provided they are not private.
And it can also access any data members of the vehicle class. Again, if they are not private.
It can also write its own version of the base class member functions. So instead of calling a member
function from the base class, it can define a member function, which has the same name, and use that
instead.
An object of a derived class is laid out in memory as a base class object, followed by the derived
class part.
So we have some memory here, which is an aeroplane object.
The first part of this is going to be the vehicle part of the object, and the rest of it will be the
bits that are specific to an aeroplane.
When we create this object, the aeroplane constructor is called, but this will call the vehicle constructor
first.
So this bit gets initialized before any of the aeroplane parts get initialized. And for destruction,
it is the opposite way
round. The aeroplane destructor is called and returns before the base class
destructor is called.
So we need to call the base class constructed, and we do that using the same syntax as if the base
class was a member of the aeroplane class.
So we just put a colon, the base class name, and any arguments in the brackets.
(Just find myself some room!) if there are arguments to the constructors, then each class will in effect
initialize its own members.
So if we have the max speed and max height member variables again, the vehicle class will initialize
the max_speed variable.
And when the aeroplane constructor calls the vehicle constructor, it'll need to pass the max_speed to
that.
Then the aeroplane will initialize its own max_height variable.
So this is our hierarchy, with the vehicle class and the max_speed member, and the aeroplane class, which
is derived from the vehicle and has a max_height member.
So the member functions in this can call any member functions from vehicle, and they can also access
the max speed member of vehicle, as though it was a member of aeroplane.
So I have added the constructors.
I'm going to print out a statement, just so we can see what will happen.
So there is the vehicle constructor, which is going to initialize the vehicles private member.
The aeroplane constructor will call the vehicle constructor, and pass the arguments to the vehicle constructor.
And then it will initialize the max_height member of the aeroplane.
And then, in our main() routine, we just create a couple of objects, one for each class.
So when we create the vehicle object, that calls the vehicle construction. So that is just a normal
class. When we create an aeroplane object, that called the vehicle constructor first and then the aeroplane
constructor.
And finally, we can.
And finally, we can carry on deriving. So we can derive another class from aeroplane. So we could
have, for example, a fighter plane, which is a more specialized aeroplane. And we use the same syntax
agains, so class fighter plane, colon, public aeroplane.
So fighter plane is derived from aeroplane.
It gets a bit confusing talking about base and derived classes in this context.
So we often talk about parent and child.
So vehicle is the parent of aeroplane. Aeroplane is the child of vehicle. Aeroplane is the parent
of fighter plane, and fighter plane is the child of aeroplane.
So this is what the entire hierarchy would look like.
We have the vehicle class as the base, then we have the aeroplane, which is a child of the vehicle. And
then we have the fighter plane, which is the child of the aeroplane class.
So this fighter plane has the aeroplane as his parent, and the aeroplane has the vehicle as its parent,
in turn.
Okay, so that is it for this video.
I will see you next time, but until then, keep coding!