- [Narrator] Hi, this is Section Two:
Advanced OOP - Inheritance and Polymorphism.
We'll begin this section by learning inheritance
and polymorphism.
Then, we'll take a look at abstract classes
and building the Playable Character class.
Lastly, we'll explore building
the Thomas and Bob classes and updating the game engine
to use Thomas and Bob.
Now, let's get started with the first video
of this section that deals with inheritance.
In this video, we're going to take a look
at inheritance and how we extend a class.
We have seen how we can use other people's hard work
by instantiating, creating objects from the classes
of the SFML library.
But what if there's a class that has loads
of useful functionality in it but is not quite
what we want?
In this situation, we can inherit
from the other class.
Just like it sounds, inheritance means
we can harness all the features and benefits
of other people's classes, including the encapsulation
while further refining or extending the code
specifically to our situation.
With all this in mind, let's take a look
at an example class and see how we can extend it,
just to see the syntax and as a first step.
First, we define a class to inherit from.
This is no different from how we created
any of our other classes.
Take a look at this hypothetical Soldier class declaration.
In this code, first we define a Soldier class.
It has four private variables:
m_Health, m_Armour, m_Range,
and m_ShotPower.
It has four public functions:
setHealth, setArmour, setRange, and setShotPower.
We don't need to see the definition of the functions.
They will simply initialize the appropriate variable
that their name makes obvious.
We can also imagine that a fully implemented
Soldier class would be much more in-depth than this.
It would probably have functions such as Shoot,
GoProne, and others.
If we implemented a Soldier class
in an SFML project, it would likely have a sprite object
as well as an Update and a GetPosition function.
Now, the simple scenario presented here
is suitable for learning about inheritance.
Look at this code, especially the highlighted part.
By adding the public Soldier code
to the Sniper class declaration,
Sniper inherits from Soldier.
But what does this mean exactly?
Sniper is a Soldier.
It has all the variables and functions of Soldier.
Inheritance is more than this however.
Notice that in this code, we declare a Sniper constructor.
This constructor is unique to Sniper.
We have not only inherited from Soldier,
we have extended Soldier.
All the functionality, definitions of the Soldier class,
are handled by the Soldier class.
But the definition of the Sniper constructor
must be handled by the Sniper class.
This is what the hypothetical Sniper constructor
definition might look like.
We could go ahead and write a bunch
of other classes that are an extension
of the Soldier class, perhaps Commando
and Infantryman.
Each would have the exact same variables
and functions, but each would also have
a unique constructor that initializes
those variables appropriate to the type of soldier.
Commando might have a very high m_Health
and m_ShotPower but really puny m_Range.
Infantryman might be in-between Commando and Sniper
with mediocre values for each variable.
The terminology we might like to learn here
is that the class that is extended
from super class is the sub class.
We can also say parent and child class.
Let's now take a look at protected variables.
There is an access specifier for class variables
and functions called protected.
You can think of protected variables
as being somewhere between public and private.
Here is a quick summary of access specifiers
along with more details about the protected specifier.
Public variables and functions can be accessed
and used by anyone.
Private variables and functions can only be accessed
and used by the internal code of the class.
This is good for encapsulation
and when we need to access change private variables.
We can provide public getter and setter functions
such as getSprite and so on.
If we extend a class that has private variables
and functions, that child class cannot directly access
the private data of its parent.
Protected variables and functions
are almost the same as private.
They cannot be accessed/used directly
by an instance of the class.
However, they can be used directly by any class
that extends the class they are declared in.
So it is like they're private,
except at child classes.
To fully understand what protected variables
and functions are and how they can be useful,
we'll look at another topic first
before we can see them in action.
In this video, we've learned about inheritance.
Cool!
In the next video, we'll take a look at polymorphism.