1
00:00:01,006 --> 00:00:04,003
- [Instructor] Hi, this
is Abstract Classes,

2
00:00:04,003 --> 00:00:07,001
Virtual and Pure Virtual Functions.

3
00:00:07,001 --> 00:00:08,008
In the previous video we got to know

4
00:00:08,008 --> 00:00:10,009
about polymorphism.

5
00:00:10,009 --> 00:00:13,006
In this video, we'll
make a class abstract,

6
00:00:13,006 --> 00:00:15,008
and try and understand abstract classes

7
00:00:15,008 --> 00:00:17,004
with an example.

8
00:00:17,004 --> 00:00:19,001
An abstract class is a class

9
00:00:19,001 --> 00:00:21,003
that cannot be instantiated, and therefore

10
00:00:21,003 --> 00:00:23,004
cannot be made into an object.

11
00:00:23,004 --> 00:00:26,001
So it's code that will never be used then?

12
00:00:26,001 --> 00:00:28,004
No, that's like paying an architect

13
00:00:28,004 --> 00:00:31,002
to design your home and
then never building it.

14
00:00:31,002 --> 00:00:33,004
If we, or the designer of a class,

15
00:00:33,004 --> 00:00:35,004
want to force its users to inherit it

16
00:00:35,004 --> 00:00:37,001
before using their class,

17
00:00:37,001 --> 00:00:39,004
they can make a class abstract.

18
00:00:39,004 --> 00:00:41,008
Then we cannot make an object from it,

19
00:00:41,008 --> 00:00:43,008
therefore, we must extend it first

20
00:00:43,008 --> 00:00:46,008
and make an object from the sub-class.

21
00:00:46,008 --> 00:00:50,000
To do so, we can make
a function pure virtual

22
00:00:50,000 --> 00:00:51,009
and not provide any definition,

23
00:00:51,009 --> 00:00:54,003
then that function must be overridden,

24
00:00:54,003 --> 00:00:57,004
rewritten, in any class that extends it.

25
00:00:57,004 --> 00:00:59,000
Let's take a look at an example.

26
00:00:59,000 --> 00:01:00,004
It will help.

27
00:01:00,004 --> 00:01:02,002
We make a class abstract by adding

28
00:01:02,002 --> 00:01:04,004
a pure virtual function, such as this

29
00:01:04,004 --> 00:01:06,002
abstract animal class

30
00:01:06,002 --> 00:01:07,009
that can only perform the generic action

31
00:01:07,009 --> 00:01:09,008
of makeNoise.

32
00:01:09,008 --> 00:01:13,001
As you can see, we add the C++ keyword

33
00:01:13,001 --> 00:01:15,005
virtual before and equal to zero

34
00:01:15,005 --> 00:01:17,004
after the function declaration.

35
00:01:17,004 --> 00:01:20,002
Now any classes that
extends inherits from animal

36
00:01:20,002 --> 00:01:22,007
must override the makeNoise function.

37
00:01:22,007 --> 00:01:23,008
This might make sense

38
00:01:23,008 --> 00:01:25,004
since different types of animal

39
00:01:25,004 --> 00:01:27,007
make very different types of noise.

40
00:01:27,007 --> 00:01:29,000
We could perhaps have assumed

41
00:01:29,000 --> 00:01:31,003
that anybody who extends the animal class

42
00:01:31,003 --> 00:01:32,005
is smart enough to notice

43
00:01:32,005 --> 00:01:34,007
that the animal class cannot make a noise,

44
00:01:34,007 --> 00:01:36,002
and that they will need to handle it.

45
00:01:36,002 --> 00:01:38,007
But what if they don't notice?

46
00:01:38,007 --> 00:01:41,009
The point is, that by making
a pure virtual function,

47
00:01:41,009 --> 00:01:44,008
we guarantee that they
will because they have to.

48
00:01:44,008 --> 00:01:47,001
Abstract classes are also useful

49
00:01:47,001 --> 00:01:48,008
because sometimes we want a class

50
00:01:48,008 --> 00:01:51,000
that can be used as a polymorphic type,

51
00:01:51,000 --> 00:01:53,003
but we need to guarantee
it can never be used

52
00:01:53,003 --> 00:01:54,006
as an object.

53
00:01:54,006 --> 00:01:57,001
For example, animal
doesn't really make sense

54
00:01:57,001 --> 00:01:58,002
on its own.

55
00:01:58,002 --> 00:01:59,007
We don't talk about animals,

56
00:01:59,007 --> 00:02:01,006
we talk about types of animal.

57
00:02:01,006 --> 00:02:03,001
We don't say, oh, look at that lovely,

58
00:02:03,001 --> 00:02:05,005
fluffy, white animal, or yesterday

59
00:02:05,005 --> 00:02:07,005
we went to the pet shop and got an animal

60
00:02:07,005 --> 00:02:08,009
and an animal bed.

61
00:02:08,009 --> 00:02:11,004
It's just too, well, abstract.

62
00:02:11,004 --> 00:02:13,007
So an abstract class is kind of like

63
00:02:13,007 --> 00:02:15,008
a template to be used by any class

64
00:02:15,008 --> 00:02:18,004
that extends it, inherits from it.

65
00:02:18,004 --> 00:02:21,004
If we were building an
industrial empire type game

66
00:02:21,004 --> 00:02:22,009
where the player manages businesses

67
00:02:22,009 --> 00:02:25,007
and their employees, we
might want a worker class,

68
00:02:25,007 --> 00:02:28,002
for example, and extend it to make miner,

69
00:02:28,002 --> 00:02:30,009
steel worker, office
worker, and, of course,

70
00:02:30,009 --> 00:02:34,008
programmer, but what exactly
does a plain worker do?

71
00:02:34,008 --> 00:02:37,006
Why would we ever want to instantiate one?

72
00:02:37,006 --> 00:02:40,003
The answer is, we wouldn't
want to instantiate one,

73
00:02:40,003 --> 00:02:43,000
but we might want to use
it as a polymorphic type

74
00:02:43,000 --> 00:02:45,007
so we can pass multiple worker sub classes

75
00:02:45,007 --> 00:02:47,008
between functions and have data structures

76
00:02:47,008 --> 00:02:50,000
that can hold all types of workers.

77
00:02:50,000 --> 00:02:52,009
All pure virtual functions
must be overridden

78
00:02:52,009 --> 00:02:55,006
by any class that extends the parent class

79
00:02:55,006 --> 00:02:58,002
that contains the pure virtual function.

80
00:02:58,002 --> 00:03:00,001
This means that the abstract class

81
00:03:00,001 --> 00:03:02,001
could provide some of
the common functionality

82
00:03:02,001 --> 00:03:04,009
that would be available
in all its sub classes.

83
00:03:04,009 --> 00:03:07,001
For example, the worker class might have

84
00:03:07,001 --> 00:03:09,007
the m_AnnualSalary,

85
00:03:09,007 --> 00:03:11,006
m_Productivity,

86
00:03:11,006 --> 00:03:14,004
and m_Age member variables.

87
00:03:14,004 --> 00:03:17,000
It might also have the
getPayCheck function,

88
00:03:17,000 --> 00:03:18,005
which is not pure virtual,

89
00:03:18,005 --> 00:03:20,009
and is the same in all the sub classes,

90
00:03:20,009 --> 00:03:23,002
but it might have a doWork function,

91
00:03:23,002 --> 00:03:25,009
which is pure virtual
and must be overridden

92
00:03:25,009 --> 00:03:28,000
because all the different types of worker

93
00:03:28,000 --> 00:03:30,004
will do work very differently.

94
00:03:30,004 --> 00:03:34,000
By the way, virtual, as
opposed to pure virtual,

95
00:03:34,000 --> 00:03:37,001
is a function that can
be optionally overridden.

96
00:03:37,001 --> 00:03:39,004
You declare a virtual
function the same way

97
00:03:39,004 --> 00:03:41,006
as a pure virtual function, but leave

98
00:03:41,006 --> 00:03:44,000
the equal to zero off the end.

99
00:03:44,000 --> 00:03:45,005
In the current game project,

100
00:03:45,005 --> 00:03:48,001
we will use a pure virtual function.

101
00:03:48,001 --> 00:03:50,003
If any of this virtual, pure virtual,

102
00:03:50,003 --> 00:03:52,006
or abstract stuff is unclear,

103
00:03:52,006 --> 00:03:54,002
using it is probably the best way

104
00:03:54,002 --> 00:03:56,001
to understand it.

105
00:03:56,001 --> 00:03:57,007
In this video, we have learned about

106
00:03:57,007 --> 00:04:01,009
abstract classes, virtual
and pure virtual functions.

107
00:04:01,009 --> 00:04:03,003
Awesome.

108
00:04:03,003 --> 00:04:04,003
Next up, we'll see

109
00:04:04,003 --> 00:04:07,004
the building the PlayableCharacter class.

