Hello again! In this video, we are going to look at member functions and inheritance.
When we inherit from a parent class which has member functions, the child will, so-called, "inherit"
all these member functions, provided they are not private.
So this means that it will appear
that the child class defines the same functions as the parent does.
For example, if we have our Vehicle class and we give it start() member function which is public,
then our derived Aeroplane class will also appear to have the same start() public member function.
So in this code, if we create an Aeroplane object and then call the start() member function, this
is going to call the inherited member function. And there we go.
What happens if the child wants the member function to do something different? For example, with
an aeroplane, you cannot just jump in and turn the key, you have to do a lot more checks and so on.
In that case, the child class can implement the member function itself, so it can have its own definition,
which does something which is more appropriate.
So with the same code again, but with the child class defining its own member function which re-implements
the parent's.
So you can see, this just holds the child's member function and not the parent's one.
Sometimes the parent member function does something which is useful to the child, but the child
needs to do some more work.
So in that case, you could have re-implement the function and call the parent's member function at the
strategic moment.
So for example, with our aeroplane, we could carry out our checks.
Then we start the engine and then we tell the air traffic control that we are ready to take off.
So it will look like this.
So we do our thing, then we call the parent's member function, than we do some more work.
You might notice that I have been very careful with my wording.
I did not say that the child inherits public member functions.
I said that the child inherits member functions which are not private.
And there is a reason for that, which is that there are actually three access specifiers in C++.
So let's quickly recap.
So when we have a class with a public member, that means that member can be accessed from anywhere
in the code.
That is part of the interface that the class has to the rest of the code.
With a child class, the child will have access to all the parent's public members.
It does not have access to any of the private members.
So in that respect, the child class is no different from any other code.
Sometimes it's useful to have member functions in the parent, which can be used by child classes,
but not by any other code.
So that would be part of the class's implementation.
And to do that, we use the protected access specifier.
So we put "protected", followed by a colon.
And then everything which comes after that will be protected until the next access specifier, or at the
end of the class definition.
So this could be, for example, inside the Vehicle class definition.
And anything that goes in here will be accessible to classes which are derived from Vehicle, but
not to anything else.
You can make data members protected, but that is not very useful.
Normally, you would only make member functions protected.
So this acts as a kind of second interface, which is available within the Vehicle hierarchy, but not
anywhere outside it.
So this will maintain encapsulation. These implementation details will be hidden from the outside world,
but they will be available inside the hierarchy.
So let's try that out. So we have our vehicle class with a protected member function.
We have our Aeroplane class, which has a member function which calls the protected member functions, and
we create a Vehicle object.
So that does actually compile. We get a warning about not using this variable, but that does not matter
here.
So if we uncomment this, so we are going to call this protected member function in the main() function.
This is not code which is part of a member function in a derived class.
So this should fail.
And there we are. "Cannot access protected member declared in class Vehicle".
OK, so that is it for this video.
I will see you next time, but until then, keep coding!