1
00:00:01,040 --> 00:00:10,610
OK, so we are continuing on with our polymorphic project, but we are going to transition into something

2
00:00:10,610 --> 00:00:17,360
that isn't necessarily tied to polymorphism, it's kind of a stand alone topic, but it will be one

3
00:00:17,360 --> 00:00:22,580
of our last topics we're talking about here in the programming half of this course.

4
00:00:23,780 --> 00:00:31,670
So before I tell you what this topic is, I just kind of want to talk about a potential function that

5
00:00:31,670 --> 00:00:37,430
we might want to add to this class, so this is not some super portant function.

6
00:00:37,910 --> 00:00:46,460
But let's say that in each one of the derive classes we wanted, a function called is bigger to see

7
00:00:46,460 --> 00:00:51,610
if one shape was bigger than the shape of the current shape of this class.

8
00:00:51,620 --> 00:00:59,180
So let's say I just have something like a Boolean function, and I want to say like, call it, let's

9
00:00:59,180 --> 00:01:03,230
say, is a bigger shape.

10
00:01:04,520 --> 00:01:13,340
And what we want to know is, is that this object, which is the object that's being called upon, is

11
00:01:13,340 --> 00:01:17,540
it area bigger than a specific shape that we would pass to this?

12
00:01:17,960 --> 00:01:20,540
And what if we don't want to limit it to triangles?

13
00:01:20,540 --> 00:01:24,290
What if we want it to be a triangle or a circle or a rectangle?

14
00:01:25,800 --> 00:01:34,380
So the thing about that is it creates kind of a problem, right, because like, let's say we want to

15
00:01:34,380 --> 00:01:39,390
have this in each one of the classes, not that really even matters, but I don't know what type to

16
00:01:39,390 --> 00:01:40,110
put here.

17
00:01:40,710 --> 00:01:45,300
If I wanted to compare a circle, you know, I want something to like, inherit.

18
00:01:45,900 --> 00:01:50,850
We're not in here to want something to include the circle class, so I can have access to it to know

19
00:01:50,850 --> 00:01:51,480
this type.

20
00:01:52,770 --> 00:01:58,270
But like, not even considering that, what if I don't want to limit it to a circle?

21
00:01:58,270 --> 00:02:06,900
What if I want it to be maybe another triangle or I want it to be a rectangle or something like that?

22
00:02:06,910 --> 00:02:10,530
So, you know, that's kind of that's kind of a problem.

23
00:02:11,950 --> 00:02:16,240
That I that I can't do any of those, I won't know which one it is.

24
00:02:16,360 --> 00:02:18,070
So how would we resolve that?

25
00:02:18,550 --> 00:02:24,040
And this is where the new Typekit comes in and this is something called a template.

26
00:02:25,090 --> 00:02:34,780
So what we can do is we can kind of define and be ambiguous type and use that ambiguous type as the

27
00:02:34,780 --> 00:02:38,950
data type here inside of our parameters.

28
00:02:39,370 --> 00:02:43,180
That way, it won't really be set to a specific data type.

29
00:02:43,210 --> 00:02:48,820
It'll kind of just be resolved at the time in which something is passed to this function.

30
00:02:49,990 --> 00:02:52,150
So how would we do something like that?

31
00:02:52,180 --> 00:03:00,100
Well, I'm just going to implement it just in triangle, and then I'm going to challenge you to implement

32
00:03:00,100 --> 00:03:06,730
it across all of the classes by also adding it as an abstract method to the base class.

33
00:03:07,330 --> 00:03:11,530
And then I'll go ahead and do that so you can see how that would work.

34
00:03:12,220 --> 00:03:19,630
But first off, I'm just going to show you how a function template can work here just by using the triangle

35
00:03:19,630 --> 00:03:21,640
class as an example.

36
00:03:22,750 --> 00:03:27,240
So what I'm going to do is I'm going to type the word template universe and see line.

37
00:03:27,250 --> 00:03:34,330
It's nice because it brings up not only the word template for me, but it also shows the next syntax

38
00:03:34,330 --> 00:03:35,040
right here.

39
00:03:35,050 --> 00:03:40,680
It needs to go inside of angle brackets similar to like when you used a vector.

40
00:03:40,690 --> 00:03:46,990
So if you had a vector and you said vector of type and we're going to do something similar where we

41
00:03:46,990 --> 00:03:54,130
say template and then we're just going to put some type of like class name in here, ambiguous class

42
00:03:54,130 --> 00:03:54,520
name.

43
00:03:55,300 --> 00:03:58,810
So I press tab and it already gives me those little angle brackets.

44
00:03:59,410 --> 00:04:06,220
So what I'm going to say is inside of here, I'm just going to say Class T, and this is going to represent

45
00:04:06,220 --> 00:04:09,430
our ambiguous templated data type here.

46
00:04:10,590 --> 00:04:18,810
So I have to put this above the member function that I'm interested in having be a template, no function,

47
00:04:18,810 --> 00:04:19,170
so.

48
00:04:20,290 --> 00:04:30,480
And here I'm going to use the capital T as the data type now, and I'm just going to say T shape.

49
00:04:30,990 --> 00:04:37,830
So the type of the shape is kind of this ambiguous template, ambiguous template, and we're going to

50
00:04:37,830 --> 00:04:41,430
call that type T because we have this class T in here.

51
00:04:43,170 --> 00:04:52,860
So once I put that I actually need to have in C++ the definition of the function and the implementation

52
00:04:52,860 --> 00:04:54,930
of the function in the same file.

53
00:04:54,930 --> 00:05:00,420
So I can't just prototype the template and throw it into another file like this, I have to actually

54
00:05:00,420 --> 00:05:00,810
put it.

55
00:05:02,130 --> 00:05:07,290
In here in this header, since we're just using the header, it would be like that anyways, right?

56
00:05:07,290 --> 00:05:10,380
We have our implementations here, so no big deal.

57
00:05:11,340 --> 00:05:21,150
So I'm going to go ahead and just check to see whether the area of the this object is bigger or less

58
00:05:21,150 --> 00:05:28,320
than the area of the T shape past object to the method.

59
00:05:29,220 --> 00:05:31,260
So just one liner here, really.

60
00:05:31,260 --> 00:05:39,330
I'm just going to say return and I'm going to say this, which is the object that's being called upon,

61
00:05:39,330 --> 00:05:39,600
right?

62
00:05:39,870 --> 00:05:41,010
So it'll be our triangle.

63
00:05:41,460 --> 00:05:43,710
I will say if this arrow.

64
00:05:45,470 --> 00:05:54,100
Area is greater than or I'm just going to say return, sorry.

65
00:05:54,380 --> 00:06:00,230
What we want to do is we want to see if this shape in here has a greater area than this object.

66
00:06:00,240 --> 00:06:11,150
So I'm just going to say return this area less than shape and shape.

67
00:06:11,570 --> 00:06:12,230
I could have.

68
00:06:12,230 --> 00:06:17,300
This is what I'm going to do is just reference the pointer, so I'll just do a dot.

69
00:06:17,930 --> 00:06:23,270
So I'm going to say shape dot area.

70
00:06:25,610 --> 00:06:29,060
And then so what I'll do is when I'm in the client file.

71
00:06:29,420 --> 00:06:37,520
Since I have pointers here, I am going to just reference the pointer so I could pass an actual object

72
00:06:38,000 --> 00:06:45,290
into here, like a sharp object, like some ambiguous one, some, you know, type of shape object,

73
00:06:45,290 --> 00:06:48,830
whether it be a triangle circle or rectangle, but it's not going to be a pointer.

74
00:06:49,130 --> 00:06:51,830
So that's why I'm going to expect just a shape.

75
00:06:51,830 --> 00:06:53,000
That's why I'm using the dot here.

76
00:06:54,620 --> 00:06:58,860
So now I have this template here and so I can use this T.

77
00:07:00,580 --> 00:07:06,220
And we can automatically resolve what type of shape is going to get passed here and see, since they

78
00:07:06,220 --> 00:07:12,720
all have an area, we know that we can just use this line and call area on both this and the shaped

79
00:07:12,860 --> 00:07:13,210
area.

80
00:07:15,580 --> 00:07:19,270
So let's go ahead and test this out, right?

81
00:07:19,480 --> 00:07:25,990
So I'm going to go ahead and go to my main and then what I'm going to do is I'm just going to put some

82
00:07:25,990 --> 00:07:26,590
sort of.

83
00:07:30,050 --> 00:07:35,030
If check here and let's see what happens when I try and use this.

84
00:07:35,210 --> 00:07:38,330
So let's say we put it in triangle, right?

85
00:07:38,340 --> 00:07:41,630
So let's go ahead and see if we can use it in triangle.

86
00:07:42,560 --> 00:07:45,040
So if I go t ts a point, right?

87
00:07:45,050 --> 00:07:46,190
And I go arrow.

88
00:07:47,450 --> 00:07:52,240
Notice that it's not giving me the that function from triangle.

89
00:07:52,250 --> 00:07:55,540
So these functions were fine, right?

90
00:07:55,610 --> 00:08:05,960
You notice that these two functions here were ones that were virtual functions in the abstract base

91
00:08:05,960 --> 00:08:10,120
class for all of the shapes in all of the drive class shapes.

92
00:08:10,120 --> 00:08:11,440
So they all have to have these.

93
00:08:11,450 --> 00:08:16,500
So it's because I'm actually declaring the pointer as shape Typekit.

94
00:08:16,500 --> 00:08:18,370
It's not really letting me access that.

95
00:08:18,380 --> 00:08:23,240
So if I try and force this, let's see what's it called is bigger shape.

96
00:08:23,780 --> 00:08:30,320
If I try and say Arrow is bigger shape, it doesn't like that doesn't recognize that function.

97
00:08:30,320 --> 00:08:35,960
So sometimes we can do to resolve this is I'm actually just going to change all of these to order.

98
00:08:45,590 --> 00:08:47,630
So now if I go.

99
00:08:49,190 --> 00:08:50,280
And I do this arrow.

100
00:08:50,300 --> 00:08:56,900
You notice I now have access to the is bigger shape memory function, so I can say if.

101
00:08:58,400 --> 00:09:00,780
Is bigger shape and then let me pass a shape, right?

102
00:09:00,800 --> 00:09:06,560
So how about we pass on little reference like this rectangle?

103
00:09:06,560 --> 00:09:09,630
So I'll say star to reference it, right?

104
00:09:09,680 --> 00:09:10,610
A star?

105
00:09:12,560 --> 00:09:16,610
How do you reference it and give me not the pointer, but the actual rectangle object?

106
00:09:17,150 --> 00:09:22,580
And so if what I'm checking right here is, I'm saying like, Oh, let's check it to see, is it true

107
00:09:22,580 --> 00:09:27,290
or false, whether the rectangles area is bigger than the triangles area, right?

108
00:09:28,830 --> 00:09:39,990
So in this case, I'll just print something out and I'll say on the rectangle is bigger.

109
00:09:41,130 --> 00:09:43,890
I can even do something like this like, look, I could put the tape.

110
00:09:44,010 --> 00:09:48,870
So I'll just say our tape

111
00:09:55,290 --> 00:09:58,260
is bigger than.

112
00:10:01,440 --> 00:10:04,590
Uh, I guess I should put the right.

113
00:10:05,920 --> 00:10:08,290
Well, they say the

114
00:10:11,380 --> 00:10:18,460
are Typekit is bigger than me, and I'll just say T type.

115
00:10:24,670 --> 00:10:24,960
Cool.

116
00:10:25,520 --> 00:10:32,450
So if the rectangle is in fact bigger than the triangle, then that should print out, OK?

117
00:10:33,840 --> 00:10:38,850
So I didn't put any go.

118
00:10:40,220 --> 00:10:45,330
So you notice right here, look at our rectangle, so it has a width of one and a height of one.

119
00:10:45,440 --> 00:10:52,490
And look at a triangle here, let's just say if we change this to like.

120
00:10:54,670 --> 00:10:55,150
Or.

121
00:10:56,200 --> 00:10:58,480
And seven something like that.

122
00:10:59,830 --> 00:11:03,790
You know, this would be twenty eight divided by two.

123
00:11:05,630 --> 00:11:11,660
So there would be 14 step and mean bigger than the rectangle, right, so we can even double check that

124
00:11:11,660 --> 00:11:12,000
here.

125
00:11:12,020 --> 00:11:17,450
So why don't we just print out and uh, let's just say.

126
00:11:19,820 --> 00:11:21,320
The are type one.

127
00:11:21,320 --> 00:11:22,370
Why don't we do this

128
00:11:25,670 --> 00:11:32,750
with area and we'll do our area?

129
00:11:34,750 --> 00:11:36,430
It's bigger than a T tie

130
00:11:40,480 --> 00:11:42,670
with area.

131
00:11:46,300 --> 00:11:55,420
And the key area is kind of a long running line there.

132
00:11:57,670 --> 00:12:04,210
But let's go ahead and that out, so I can only do this like decimals, we can make changes to you

133
00:12:07,030 --> 00:12:10,060
like anything as well, so let's go ahead and just run this.

134
00:12:12,560 --> 00:12:14,370
So it looks like it's not bigger.

135
00:12:14,390 --> 00:12:19,370
So what if I change this to like 10 and I changed this to like 11?

136
00:12:23,250 --> 00:12:27,070
Cool, so you notice that I put that with and without that there.

137
00:12:27,090 --> 00:12:30,060
So let's go ahead and read that again about the space.

138
00:12:30,630 --> 00:12:36,360
So the rectangle with area one sixteen point six is bigger than the triangle with area fifteen point

139
00:12:36,720 --> 00:12:38,130
seven five.

140
00:12:38,520 --> 00:12:40,300
So I should put a space there to.

141
00:12:43,850 --> 00:12:46,070
So pretty cool, right?

142
00:12:46,430 --> 00:12:50,780
So it's able to actually resolve this for anything like what if we change it to something else?

143
00:12:50,780 --> 00:12:52,940
So I do the see for the circle.

144
00:12:53,270 --> 00:12:55,970
And now we're passing the circle instead.

145
00:12:56,360 --> 00:12:58,190
So let's go ahead and do that.

146
00:13:00,960 --> 00:13:02,880
See notice now, it says.

147
00:13:05,040 --> 00:13:06,120
We'd want to put the.

148
00:13:07,180 --> 00:13:09,420
Different type for that, we'd want to put this circle right?

149
00:13:09,570 --> 00:13:11,970
So the chances to see.

150
00:13:14,350 --> 00:13:15,760
Because I change that to see.

151
00:13:17,460 --> 00:13:24,000
So let's see what it says here, so says the circle with area to seventy seven point eighty seven speaking

152
00:13:24,000 --> 00:13:27,030
in the triangle area fifteen point seven five.

153
00:13:30,820 --> 00:13:32,140
So pretty cool, right?

154
00:13:33,190 --> 00:13:36,640
So it's checking that that's better or bigger.

155
00:13:36,940 --> 00:13:43,990
So what I want to challenge you to do now is to be able to kind of incorporate this with our polymorphic

156
00:13:43,990 --> 00:13:44,530
design.

157
00:13:44,540 --> 00:13:51,190
So you should be able to go into shape and make this present in each one of the.

158
00:13:54,320 --> 00:13:55,460
Drive classes here.

159
00:13:56,520 --> 00:14:04,620
So you have some sort of is bigger shape, so see if you can actually make that happen.

160
00:14:05,550 --> 00:14:08,490
You may not be able to make it an abstract base class.

161
00:14:09,450 --> 00:14:11,310
Sorry, an abstract function.

162
00:14:11,480 --> 00:14:12,870
You know, that's pure ritual.

163
00:14:13,270 --> 00:14:16,860
But just go ahead and try and see if you can make it work.

164
00:14:17,760 --> 00:14:18,110
All right.

165
00:14:18,150 --> 00:14:23,460
So with that, I will see you in the next video in which I will go over that solution with you.

166
00:14:23,850 --> 00:14:31,650
And then we will talk about not only using templates for templated memory functions or just functions

167
00:14:31,650 --> 00:14:32,310
in general.

168
00:14:33,330 --> 00:14:35,790
We'll also be talking about using templates for classes.
