1
00:00:00,300 --> 00:00:07,590
OK, so we're kind of continuing on with the inheritance thing, but moving on to the next step.

2
00:00:08,310 --> 00:00:15,090
So this next step is something called polymorphism, and I'm kind of going to get into the topic by

3
00:00:15,240 --> 00:00:19,170
using an example with some very simple inheritance.

4
00:00:19,560 --> 00:00:24,240
And then we're going to kind of build upon that and I'm going to explain this topic of polymorphism.

5
00:00:24,240 --> 00:00:27,800
So let's take a look at the code base I have here already.

6
00:00:27,810 --> 00:00:35,340
So it's basically just a simple shape project, very bare bones using some inheritance or stuff you've

7
00:00:35,340 --> 00:00:35,970
seen before.

8
00:00:36,390 --> 00:00:39,000
We have a shape class here, the access, our base class.

9
00:00:39,360 --> 00:00:44,130
It has a constructor where you composite a color and an X and Y coordinate.

10
00:00:44,460 --> 00:00:47,700
It has a set color, a color area and a type.

11
00:00:48,120 --> 00:00:49,920
I'm going to go ahead and move these.

12
00:00:53,830 --> 00:00:55,810
This virtual has to do with.

13
00:00:57,430 --> 00:01:03,910
What I want to do first is just kind of explain this inheritance situation, so we have area and type.

14
00:01:06,440 --> 00:01:13,550
So the area and type are also used by the derived classes, so if we go in here, this rectangle it

15
00:01:13,550 --> 00:01:15,110
inherits from the base class shape.

16
00:01:16,120 --> 00:01:19,480
And it has its own area and its own type.

17
00:01:21,220 --> 00:01:27,190
One thing that you might have not seen syntax wise, is this initialization list constructor syntax

18
00:01:27,190 --> 00:01:27,490
here.

19
00:01:27,500 --> 00:01:34,110
I'm actually calling the shape base class right here, and I'm passing the color in the X in the way.

20
00:01:34,110 --> 00:01:35,020
And why am I doing that?

21
00:01:35,290 --> 00:01:42,730
Well, because all of the rectangle, the triangle and circle and shape, they share these attributes

22
00:01:42,730 --> 00:01:46,090
like color and X coordinate and Y coordinate.

23
00:01:46,810 --> 00:01:52,120
So that's why we're just going to use the base class to hold those values and we'll still have access

24
00:01:52,120 --> 00:01:54,940
to them if we want right because I put protected here.

25
00:01:55,240 --> 00:01:58,390
So that will make it so the drive classes have access to it.

26
00:02:00,440 --> 00:02:06,710
So let's take a look at Triangle is pretty much the same thing I call the base class constructor here

27
00:02:06,830 --> 00:02:12,410
in the Triangle Constructor and then it has a base and the height they're all having their own version

28
00:02:12,410 --> 00:02:13,670
of area, right?

29
00:02:13,670 --> 00:02:18,260
And they're all having their own version of type, which is returns a string, which is whatever type

30
00:02:18,260 --> 00:02:18,920
the shape is.

31
00:02:19,130 --> 00:02:23,330
This is base times height divided by two because it's basically a right triangle, even though I'd call

32
00:02:23,330 --> 00:02:24,020
it triangle.

33
00:02:24,950 --> 00:02:30,410
If you go to circle, it's just doing PR squared here for the area and it returns a circle because it's

34
00:02:30,410 --> 00:02:31,100
a circle, right?

35
00:02:31,250 --> 00:02:36,830
Notice that this Circle Constructor is also calling the base class constructor shape.

36
00:02:38,400 --> 00:02:41,640
And the only other thing we really have is radio, so these are all like really bare bones.

37
00:02:41,670 --> 00:02:46,920
Another thing you might notice is I'm implementing everything in the header files, so I'm not even

38
00:02:46,920 --> 00:02:49,470
using CPP implementation files.

39
00:02:49,680 --> 00:02:56,530
The only CPP file I have with that extension is actually just the client file may not keep the rest.

40
00:02:56,550 --> 00:02:58,110
I can just put in the header file.

41
00:02:58,110 --> 00:02:59,100
So this is totally possible.

42
00:02:59,100 --> 00:03:04,260
You can just put them in the header file if you would like you noticed that I have like implementations

43
00:03:04,260 --> 00:03:09,750
between the curly brackets here and the initialization list as well for the constructor and like I pointed

44
00:03:09,760 --> 00:03:10,290
out already.

45
00:03:10,980 --> 00:03:18,330
So if I go over to Main, what I'm doing is I'm explicitly instantiating each one of these classes the

46
00:03:18,660 --> 00:03:24,630
the base class and then the three drive class with rectangle triangle and circle appropriately.

47
00:03:25,980 --> 00:03:32,740
So then all I do is I use ASCII color, for example, for shape and type and area.

48
00:03:32,760 --> 00:03:38,310
I do that for every one of the shapes, the base class and the three derived class shapes.

49
00:03:39,990 --> 00:03:43,050
And notice that these are just not intimately allocated or anything.

50
00:03:43,050 --> 00:03:44,370
They're just all like on the stack.

51
00:03:44,370 --> 00:03:49,800
They're just being instantiated here in this mean function, normally without pointers or anything like

52
00:03:49,800 --> 00:03:50,070
that.

53
00:03:51,490 --> 00:03:54,400
So all stuff that you've seen before, right?

54
00:03:54,580 --> 00:03:57,190
Pretty basic, so where does the polymorphism come in?

55
00:03:57,490 --> 00:03:59,680
Well, let's go ahead and run this right now.

56
00:04:01,330 --> 00:04:03,400
So everything looks good, right?

57
00:04:03,640 --> 00:04:09,430
I'm able to print this stuff out and says that there's a shape with area zero red rectangle with area

58
00:04:09,430 --> 00:04:12,490
of line, yellow triangle area above and circle with area.

59
00:04:12,980 --> 00:04:18,790
So if I go to save, you might be wondering, Well, why did you even put area and type in shape if

60
00:04:18,790 --> 00:04:21,930
we don't really know what the area of some ambiguous shape is?

61
00:04:21,930 --> 00:04:27,190
Is this base class and some type of like, you know, what is the actual type?

62
00:04:27,190 --> 00:04:29,170
It's just not having a type, it's a shape.

63
00:04:30,460 --> 00:04:37,140
So you might want to put these things in a base class for a design purpose.

64
00:04:37,150 --> 00:04:42,910
So sometimes it's good to kind of making designing a program with this base class in mind.

65
00:04:43,360 --> 00:04:50,320
Well, one of my drive classes is going to be like, this is a blatant is a relationship, right?

66
00:04:50,320 --> 00:04:53,980
Like a rectangle is a shape, a triangle is a shape, a circle is a shape.

67
00:04:55,790 --> 00:05:01,910
So you might be wanting to kind of set this class as a scaffolding for the derived classes.

68
00:05:03,700 --> 00:05:10,750
And so that kind of leads into the discussion of this polymorphism thing, so what is polymorphism even

69
00:05:10,750 --> 00:05:11,110
meet?

70
00:05:11,140 --> 00:05:16,090
Well, polymorphism means taking like one thing, taking many forms.

71
00:05:16,690 --> 00:05:21,610
And so in this specific, intense instance, it means like a shape can have many forms.

72
00:05:21,610 --> 00:05:23,320
And even more specifically.

73
00:05:25,110 --> 00:05:32,640
A shape could have, you know, could different shapes could share the same methods, but like shape

74
00:05:32,640 --> 00:05:36,510
at any time might have different implementations of those methods.

75
00:05:36,750 --> 00:05:40,020
So that's like code white, what code way is what we're trying to do?

76
00:05:40,410 --> 00:05:42,960
And we're kind of doing accomplishing that right here, right?

77
00:05:42,970 --> 00:05:43,200
Like.

78
00:05:44,310 --> 00:05:48,050
These all have their own versions of area, and they're all being called right.

79
00:05:49,440 --> 00:05:57,850
But what if I wanted to just say, well, do I have to explicitly call these rectangle triangle circle?

80
00:05:57,870 --> 00:05:59,170
Aren't they all shapes?

81
00:05:59,370 --> 00:06:01,260
Can I just say that these are all shapes?

82
00:06:01,740 --> 00:06:09,060
Maybe I had some sort of program where I was just making like a bunch of different shapes, and I just

83
00:06:09,060 --> 00:06:17,400
wanted it to automatically resolve which type of shape it was, even though I said that the data type

84
00:06:17,400 --> 00:06:18,330
was shape.

85
00:06:19,110 --> 00:06:24,930
Maybe if I use these constructors, it will know automatically which type of shape it is and it will

86
00:06:24,930 --> 00:06:29,880
call the right area method in the right area, the right type method.

87
00:06:30,330 --> 00:06:33,930
So when I say method, I really mean the member function, right?

88
00:06:34,170 --> 00:06:34,920
No function.

89
00:06:34,920 --> 00:06:37,920
So hopefully it'll call the right area and type.

90
00:06:39,210 --> 00:06:42,920
As you can probably guess, this might, you know, this is probably not going to work as you can see

91
00:06:42,920 --> 00:06:47,090
that it's getting great out, it says functional area hides a non virtual function from class shape.

92
00:06:48,870 --> 00:06:50,760
So they're not even really being used.

93
00:06:52,120 --> 00:06:54,070
So is this functional area is never used.

94
00:06:54,100 --> 00:06:56,410
So let's see what happens when we run this code now.

95
00:06:58,880 --> 00:07:03,170
So look at the output now, it actually just calls all of these a shape.

96
00:07:03,560 --> 00:07:08,300
And then it says the area is zero, so it's pretty obvious that what it's doing is that even though

97
00:07:08,300 --> 00:07:15,410
we call these constructors, when we get down here, it's actually just only using this area in this

98
00:07:15,410 --> 00:07:21,770
type from the base class, it's not able to resolve it to know which type of shape it really is.

99
00:07:22,990 --> 00:07:24,640
So how would we make that happen?

100
00:07:26,170 --> 00:07:31,000
Well, something that we could do is.

101
00:07:32,370 --> 00:07:33,780
I'll go back here to the shape.

102
00:07:35,240 --> 00:07:37,880
You are kind of already saw this because I left it there.

103
00:07:38,060 --> 00:07:45,950
But what we can do syntax wise to kind of enforce this polymorphism is to put the word virtual in front

104
00:07:45,950 --> 00:07:47,210
of these methods.

105
00:07:48,020 --> 00:07:57,890
So if I put the keyword virtual, what it does is basically let the C++ compiler know which method to

106
00:07:57,920 --> 00:07:58,610
use.

107
00:07:59,360 --> 00:08:04,970
So when I put virtual on the base class, when rectangle inherits from shape and triangle and circle

108
00:08:04,970 --> 00:08:11,630
as well, it's going to automatically and it actually calls this bindings, going to automatically bind

109
00:08:12,080 --> 00:08:20,810
the correct method to the object based on the type at the time when it's like during runtime you.

110
00:08:21,260 --> 00:08:24,980
But if the compile time, it kind of make that connection, that binding connection.

111
00:08:24,990 --> 00:08:25,370
So.

112
00:08:27,180 --> 00:08:28,240
It's pretty cool.

113
00:08:28,320 --> 00:08:29,850
You can just add this one word.

114
00:08:29,880 --> 00:08:36,960
Let's go ahead and change Maine, though, so one thing that we have to do is what I'm going to change

115
00:08:36,990 --> 00:08:42,900
up here is I'm going to actually make these pointers.

116
00:08:42,990 --> 00:08:50,790
So I'm going to say shape star se equals new shape.

117
00:08:54,890 --> 00:09:00,260
Equals new rectangle, so why make these dynamically allocated now?

118
00:09:09,320 --> 00:09:14,150
And I'm going to have to change these two points, right?

119
00:09:28,620 --> 00:09:33,360
So kind of, you know, it was me here is painfully slow at this.

120
00:09:43,460 --> 00:09:48,860
So essentially what we're doing is we are now just making it a shape pointer.

121
00:09:50,530 --> 00:09:55,390
But what's happening is these constructors are getting called and it's actually dynamically allocating

122
00:09:55,390 --> 00:09:58,780
memory for these types.

123
00:09:59,350 --> 00:10:04,180
But we're saying that the point in time is shape, but the fact that we put that virtual keyword is

124
00:10:04,190 --> 00:10:14,110
going to make us able to actually use the correct area and Typekit member functions for the correct

125
00:10:14,410 --> 00:10:15,820
objects right that we made.

126
00:10:15,820 --> 00:10:22,540
Even though we said that the type that's being pointed to where the pointer is, it can dynamically

127
00:10:22,540 --> 00:10:24,700
allocated shape.

128
00:10:25,510 --> 00:10:29,920
It's actually going to be able to resolve it because of the virtual keyword, and it's going to know

129
00:10:30,340 --> 00:10:32,740
that each one of these are actually rectangle, triangle and circle.

130
00:10:34,150 --> 00:10:37,210
So let's go ahead and run this.

131
00:10:40,550 --> 00:10:45,740
And look at that, he was actually able to resolve them, so we're back to what we saw before, when

132
00:10:45,740 --> 00:10:49,940
we explicitly put tape, rectangle and triangle and tight circle here.

133
00:10:51,920 --> 00:10:54,230
So pretty cool, right?

134
00:10:54,500 --> 00:10:58,970
All we had to do was we kind of changed things over to pointers, but we call them shape.

135
00:10:59,300 --> 00:11:05,030
And then we went in here and we put virtual in front of these functions.

136
00:11:06,950 --> 00:11:12,860
So I'm not going to go into too much more detail in this lesson, but in the next one, we will get

137
00:11:12,860 --> 00:11:23,600
into kind of more polymorphism and really using this as a base class, that's just like a structural

138
00:11:23,600 --> 00:11:27,200
thing, like it's just a scaffolding for the rest of the classes.

139
00:11:27,200 --> 00:11:30,110
We're actually going to make it so we don't even instantiate shape.

140
00:11:30,110 --> 00:11:31,940
We don't even make a shape object.

141
00:11:32,420 --> 00:11:39,500
We're just going to design our program around a shape, and we're actually going to have these virtual

142
00:11:39,500 --> 00:11:45,650
front functions, which are going to be pure virtual functions, and it'll force us to have to implement

143
00:11:45,650 --> 00:11:49,550
those virtual functions in all these classes no matter what.

144
00:11:50,240 --> 00:11:55,790
But what that does is it just kind of enforces good design and the idea of polymorphism, you know,

145
00:11:55,790 --> 00:12:04,340
one thing taking many forms is a pivotal part of object oriented programming, and it's kind of nice.

146
00:12:04,340 --> 00:12:08,030
You may be thinking, Well, why don't you just explicitly put rectangle triangle circle?

147
00:12:08,660 --> 00:12:15,590
And the cool thing is just, you know, it makes it easier for every developer is doing the client side

148
00:12:15,590 --> 00:12:15,890
code.

149
00:12:16,190 --> 00:12:22,020
You can just call it a shape and you can make whatever one of these types of shapes that you want.

150
00:12:22,040 --> 00:12:26,650
Maybe you have some type of like loop and you're making like multiple shapes.

151
00:12:26,660 --> 00:12:29,450
You have some logic or something.

152
00:12:31,230 --> 00:12:37,400
Well, it's a lot easier to just kind of have pointers, all of the same type, and the cool thing is

153
00:12:37,400 --> 00:12:39,260
is that let's say you made a point sheep type.

154
00:12:39,260 --> 00:12:47,210
You could just basically refer to the address of any other object, know any subtype like any derived

155
00:12:47,210 --> 00:12:47,570
class.

156
00:12:47,570 --> 00:12:53,330
And it would be totally fine to assign that memory to a sheep pointer that already makes it a lot more

157
00:12:53,330 --> 00:12:54,950
flexible on the client side, right?

158
00:12:55,790 --> 00:13:01,160
So it does have its use cases, and it's a very important part of API in general.

159
00:13:02,240 --> 00:13:03,990
So not a lot change, right?

160
00:13:04,010 --> 00:13:10,670
We still use the inheritance polymorphism as kind of, you know, goes hand in hand with inheritance.

161
00:13:10,670 --> 00:13:17,990
The fact that we have a base class and the right classes is a really important part of polymorphism.

162
00:13:18,740 --> 00:13:26,330
So since we have this base class available, we were able to make these virtual functions that are then

163
00:13:26,330 --> 00:13:29,540
implemented differently across all of these rectangles.

164
00:13:29,900 --> 00:13:35,420
And even though they're implemented differently, we were able to still have a shape pointer to all

165
00:13:35,420 --> 00:13:39,170
of these and have it resolved the correct method to call it.

166
00:13:40,200 --> 00:13:46,290
OK, so with that, I will leave it in this lecture and in the next lecture, we will go over abstract

167
00:13:46,290 --> 00:13:50,730
base classes and spiritual functions for polymorphism.
