1
00:00:01,004 --> 00:00:03,002
- [Narrator] Hi, this is Section Two:

2
00:00:03,002 --> 00:00:06,008
Advanced OOP - Inheritance
and Polymorphism.

3
00:00:06,008 --> 00:00:08,009
We'll begin this section
by learning inheritance

4
00:00:08,009 --> 00:00:10,003
and polymorphism.

5
00:00:10,003 --> 00:00:12,009
Then, we'll take a look
at abstract classes

6
00:00:12,009 --> 00:00:15,003
and building the Playable Character class.

7
00:00:15,003 --> 00:00:17,000
Lastly, we'll explore building

8
00:00:17,000 --> 00:00:20,002
the Thomas and Bob classes
and updating the game engine

9
00:00:20,002 --> 00:00:22,002
to use Thomas and Bob.

10
00:00:22,002 --> 00:00:24,005
Now, let's get started
with the first video

11
00:00:24,005 --> 00:00:27,003
of this section that
deals with inheritance.

12
00:00:27,003 --> 00:00:29,003
In this video, we're going to take a look

13
00:00:29,003 --> 00:00:32,005
at inheritance and how we extend a class.

14
00:00:32,005 --> 00:00:35,004
We have seen how we can use
other people's hard work

15
00:00:35,004 --> 00:00:38,006
by instantiating, creating
objects from the classes

16
00:00:38,006 --> 00:00:40,002
of the SFML library.

17
00:00:40,002 --> 00:00:42,003
But what if there's a class that has loads

18
00:00:42,003 --> 00:00:44,008
of useful functionality
in it but is not quite

19
00:00:44,008 --> 00:00:45,009
what we want?

20
00:00:45,009 --> 00:00:47,009
In this situation, we can inherit

21
00:00:47,009 --> 00:00:49,006
from the other class.

22
00:00:49,006 --> 00:00:51,005
Just like it sounds, inheritance means

23
00:00:51,005 --> 00:00:53,007
we can harness all the
features and benefits

24
00:00:53,007 --> 00:00:56,008
of other people's classes,
including the encapsulation

25
00:00:56,008 --> 00:00:59,001
while further refining
or extending the code

26
00:00:59,001 --> 00:01:02,000
specifically to our situation.

27
00:01:02,000 --> 00:01:04,002
With all this in mind, let's take a look

28
00:01:04,002 --> 00:01:07,002
at an example class and
see how we can extend it,

29
00:01:07,002 --> 00:01:10,008
just to see the syntax
and as a first step.

30
00:01:10,008 --> 00:01:13,004
First, we define a class to inherit from.

31
00:01:13,004 --> 00:01:15,001
This is no different from how we created

32
00:01:15,001 --> 00:01:17,001
any of our other classes.

33
00:01:17,001 --> 00:01:21,001
Take a look at this hypothetical
Soldier class declaration.

34
00:01:21,001 --> 00:01:24,003
In this code, first we
define a Soldier class.

35
00:01:24,003 --> 00:01:26,004
It has four private variables:

36
00:01:26,004 --> 00:01:30,009
m_Health, m_Armour, m_Range,

37
00:01:30,009 --> 00:01:33,005
and m_ShotPower.

38
00:01:33,005 --> 00:01:35,007
It has four public functions:

39
00:01:35,007 --> 00:01:39,006
setHealth, setArmour,
setRange, and setShotPower.

40
00:01:39,006 --> 00:01:42,005
We don't need to see the
definition of the functions.

41
00:01:42,005 --> 00:01:45,000
They will simply initialize
the appropriate variable

42
00:01:45,000 --> 00:01:48,001
that their name makes obvious.

43
00:01:48,001 --> 00:01:50,001
We can also imagine
that a fully implemented

44
00:01:50,001 --> 00:01:53,007
Soldier class would be much
more in-depth than this.

45
00:01:53,007 --> 00:01:56,004
It would probably have
functions such as Shoot,

46
00:01:56,004 --> 00:01:58,002
GoProne, and others.

47
00:01:58,002 --> 00:02:00,001
If we implemented a Soldier class

48
00:02:00,001 --> 00:02:03,006
in an SFML project, it would
likely have a sprite object

49
00:02:03,006 --> 00:02:07,004
as well as an Update and
a GetPosition function.

50
00:02:07,004 --> 00:02:09,007
Now, the simple scenario presented here

51
00:02:09,007 --> 00:02:12,000
is suitable for learning
about inheritance.

52
00:02:12,000 --> 00:02:15,002
Look at this code, especially
the highlighted part.

53
00:02:15,002 --> 00:02:17,001
By adding the public Soldier code

54
00:02:17,001 --> 00:02:19,000
to the Sniper class declaration,

55
00:02:19,000 --> 00:02:21,002
Sniper inherits from Soldier.

56
00:02:21,002 --> 00:02:23,005
But what does this mean exactly?

57
00:02:23,005 --> 00:02:24,009
Sniper is a Soldier.

58
00:02:24,009 --> 00:02:27,009
It has all the variables
and functions of Soldier.

59
00:02:27,009 --> 00:02:30,002
Inheritance is more than this however.

60
00:02:30,002 --> 00:02:34,002
Notice that in this code, we
declare a Sniper constructor.

61
00:02:34,002 --> 00:02:36,006
This constructor is unique to Sniper.

62
00:02:36,006 --> 00:02:38,005
We have not only inherited from Soldier,

63
00:02:38,005 --> 00:02:40,004
we have extended Soldier.

64
00:02:40,004 --> 00:02:43,004
All the functionality,
definitions of the Soldier class,

65
00:02:43,004 --> 00:02:45,003
are handled by the Soldier class.

66
00:02:45,003 --> 00:02:47,001
But the definition of
the Sniper constructor

67
00:02:47,001 --> 00:02:50,003
must be handled by the Sniper class.

68
00:02:50,003 --> 00:02:52,005
This is what the hypothetical
Sniper constructor

69
00:02:52,005 --> 00:02:54,009
definition might look like.

70
00:02:54,009 --> 00:02:56,005
We could go ahead and write a bunch

71
00:02:56,005 --> 00:02:58,004
of other classes that are an extension

72
00:02:58,004 --> 00:03:00,008
of the Soldier class, perhaps Commando

73
00:03:00,008 --> 00:03:02,002
and Infantryman.

74
00:03:02,002 --> 00:03:04,003
Each would have the exact same variables

75
00:03:04,003 --> 00:03:06,004
and functions, but each would also have

76
00:03:06,004 --> 00:03:08,004
a unique constructor that initializes

77
00:03:08,004 --> 00:03:11,006
those variables appropriate
to the type of soldier.

78
00:03:11,006 --> 00:03:15,000
Commando might have a very high m_Health

79
00:03:15,000 --> 00:03:20,000
and m_ShotPower but really puny m_Range.

80
00:03:20,000 --> 00:03:23,005
Infantryman might be
in-between Commando and Sniper

81
00:03:23,005 --> 00:03:26,004
with mediocre values for each variable.

82
00:03:26,004 --> 00:03:28,007
The terminology we
might like to learn here

83
00:03:28,007 --> 00:03:30,003
is that the class that is extended

84
00:03:30,003 --> 00:03:33,005
from super class is the sub class.

85
00:03:33,005 --> 00:03:36,005
We can also say parent and child class.

86
00:03:36,005 --> 00:03:39,005
Let's now take a look
at protected variables.

87
00:03:39,005 --> 00:03:42,006
There is an access specifier
for class variables

88
00:03:42,006 --> 00:03:44,008
and functions called protected.

89
00:03:44,008 --> 00:03:46,004
You can think of protected variables

90
00:03:46,004 --> 00:03:49,001
as being somewhere between
public and private.

91
00:03:49,001 --> 00:03:51,009
Here is a quick summary
of access specifiers

92
00:03:51,009 --> 00:03:55,000
along with more details about
the protected specifier.

93
00:03:55,000 --> 00:03:57,002
Public variables and
functions can be accessed

94
00:03:57,002 --> 00:03:59,000
and used by anyone.

95
00:03:59,000 --> 00:04:01,006
Private variables and
functions can only be accessed

96
00:04:01,006 --> 00:04:04,005
and used by the internal
code of the class.

97
00:04:04,005 --> 00:04:06,002
This is good for encapsulation

98
00:04:06,002 --> 00:04:09,001
and when we need to access
change private variables.

99
00:04:09,001 --> 00:04:11,009
We can provide public
getter and setter functions

100
00:04:11,009 --> 00:04:14,007
such as getSprite and so on.

101
00:04:14,007 --> 00:04:17,000
If we extend a class that
has private variables

102
00:04:17,000 --> 00:04:19,009
and functions, that child
class cannot directly access

103
00:04:19,009 --> 00:04:22,003
the private data of its parent.

104
00:04:22,003 --> 00:04:24,000
Protected variables and functions

105
00:04:24,000 --> 00:04:25,008
are almost the same as private.

106
00:04:25,008 --> 00:04:28,003
They cannot be accessed/used directly

107
00:04:28,003 --> 00:04:29,007
by an instance of the class.

108
00:04:29,007 --> 00:04:32,000
However, they can be used
directly by any class

109
00:04:32,000 --> 00:04:34,000
that extends the class
they are declared in.

110
00:04:34,000 --> 00:04:35,005
So it is like they're private,

111
00:04:35,005 --> 00:04:37,005
except at child classes.

112
00:04:37,005 --> 00:04:39,004
To fully understand
what protected variables

113
00:04:39,004 --> 00:04:42,000
and functions are and
how they can be useful,

114
00:04:42,000 --> 00:04:43,006
we'll look at another topic first

115
00:04:43,006 --> 00:04:46,001
before we can see them in action.

116
00:04:46,001 --> 00:04:49,000
In this video, we've
learned about inheritance.

117
00:04:49,000 --> 00:04:49,009
Cool!

118
00:04:49,009 --> 00:04:54,000
In the next video, we'll
take a look at polymorphism.

