1
00:00:01,003 --> 00:00:03,000
- [Voiceover] Hi, welcome
to the second video

2
00:00:03,000 --> 00:00:05,003
of this section, polymorphism.

3
00:00:05,003 --> 00:00:09,002
In the previous video, we
learned about inheritance.

4
00:00:09,002 --> 00:00:12,005
In this video, we'll go through
the concept of polymorphism,

5
00:00:12,005 --> 00:00:14,006
and take a look at an example.

6
00:00:14,006 --> 00:00:16,007
Polymorphism allows us to write code

7
00:00:16,007 --> 00:00:18,003
that is less dependent on the types

8
00:00:18,003 --> 00:00:19,009
we are trying to manipulate.

9
00:00:19,009 --> 00:00:23,001
This can make our code
clearer and more efficient.

10
00:00:23,001 --> 00:00:25,002
It means different forms.

11
00:00:25,002 --> 00:00:26,003
If the objects that we code

12
00:00:26,003 --> 00:00:27,009
can be more than one type of thing,

13
00:00:27,009 --> 00:00:30,006
that we can take advantage of this.

14
00:00:30,006 --> 00:00:32,008
Boiled down to its simplest definition,

15
00:00:32,008 --> 00:00:34,007
polymorphism is this:

16
00:00:34,007 --> 00:00:36,004
Any sub-class can be used

17
00:00:36,004 --> 00:00:39,001
as part of the code that
uses the super-class.

18
00:00:39,001 --> 00:00:40,005
This means we can write code

19
00:00:40,005 --> 00:00:42,009
that is simpler and easier to understand,

20
00:00:42,009 --> 00:00:45,007
and also easier to modify or change.

21
00:00:45,007 --> 00:00:48,002
Also, we can write code
for the super-class,

22
00:00:48,002 --> 00:00:50,004
and rely on the fact that
no matter how many times

23
00:00:50,004 --> 00:00:53,004
it is sub-classed, within
certain parameters,

24
00:00:53,004 --> 00:00:55,002
the code will still work.

25
00:00:55,002 --> 00:00:57,002
Let's discuss an example.

26
00:00:57,002 --> 00:00:59,002
Suppose we want to use polymorphism

27
00:00:59,002 --> 00:01:01,000
to help write a zoo management game,

28
00:01:01,000 --> 00:01:04,001
where we have to feed and
tend to the needs of animals.

29
00:01:04,001 --> 00:01:07,004
We will probably want to have
a function such as "feed."

30
00:01:07,004 --> 00:01:09,004
We will also probably
want to pass an instance

31
00:01:09,004 --> 00:01:13,000
of the animal to be fed
into the feed function.

32
00:01:13,000 --> 00:01:15,004
A zoo of course has many types of animals.

33
00:01:15,004 --> 00:01:18,009
Lion, elephant and three-toed sloth.

34
00:01:18,009 --> 00:01:21,008
With our new knowledge of C++ inheritance,

35
00:01:21,008 --> 00:01:24,001
it will make sense to code an animal class

36
00:01:24,001 --> 00:01:27,003
and have all the different
types of animal inherit from it.

37
00:01:27,003 --> 00:01:29,005
If we want to write a function, "feed,"

38
00:01:29,005 --> 00:01:32,001
that we can pass lion,
elephant, and three-toed sloth

39
00:01:32,001 --> 00:01:33,007
into as a parameter,

40
00:01:33,007 --> 00:01:36,000
it might seem like we need
to write a feed function

41
00:01:36,000 --> 00:01:38,002
for each and every type of animal.

42
00:01:38,002 --> 00:01:40,008
However, we can write
polymorphic functions,

43
00:01:40,008 --> 00:01:43,007
with polymorphic return
types and arguments.

44
00:01:43,007 --> 00:01:45,002
Take a look at this definition

45
00:01:45,002 --> 00:01:47,002
of the hypothetical feed function:

46
00:01:47,002 --> 00:01:50,008
This function has the animal
reference as a parameter.

47
00:01:50,008 --> 00:01:53,003
Meaning that, any object
that is built from a class

48
00:01:53,003 --> 00:01:55,007
that extends animal can be passed into it.

49
00:01:55,007 --> 00:01:58,002
So you can even write code today,

50
00:01:58,002 --> 00:02:01,001
and make another sub-class
in a week, month, or year.

51
00:02:01,001 --> 00:02:03,003
And the very same functions
and data structures

52
00:02:03,003 --> 00:02:05,001
will still work.

53
00:02:05,001 --> 00:02:07,009
Also, we can enforce upon our sub-classes,

54
00:02:07,009 --> 00:02:10,009
a set of rules for what
they can and cannot do,

55
00:02:10,009 --> 00:02:12,008
as well as how they do it.

56
00:02:12,008 --> 00:02:14,008
So, good design in one stage

57
00:02:14,008 --> 00:02:18,000
can influence it at other stages.

58
00:02:18,000 --> 00:02:19,003
But will we really ever want to

59
00:02:19,003 --> 00:02:21,004
instantiate an actual animal?

60
00:02:21,004 --> 00:02:23,005
We'll find out.

61
00:02:23,005 --> 00:02:26,001
In this video, we've
looked at polymorphism.

62
00:02:26,001 --> 00:02:27,003
Great.

63
00:02:27,003 --> 00:02:30,003
Next, as I just said,
we'll see abstract classes,

64
00:02:30,003 --> 00:02:33,003
virtual and pure virtual functions.

