OK, so we are continuing on with our polymorphic project, but we are going to transition into something
that isn't necessarily tied to polymorphism, it's kind of a stand alone topic, but it will be one
of our last topics we're talking about here in the programming half of this course.
So before I tell you what this topic is, I just kind of want to talk about a potential function that
we might want to add to this class, so this is not some super portant function.
But let's say that in each one of the derive classes we wanted, a function called is bigger to see
if one shape was bigger than the shape of the current shape of this class.
So let's say I just have something like a Boolean function, and I want to say like, call it, let's
say, is a bigger shape.
And what we want to know is, is that this object, which is the object that's being called upon, is
it area bigger than a specific shape that we would pass to this?
And what if we don't want to limit it to triangles?
What if we want it to be a triangle or a circle or a rectangle?
So the thing about that is it creates kind of a problem, right, because like, let's say we want to
have this in each one of the classes, not that really even matters, but I don't know what type to
put here.
If I wanted to compare a circle, you know, I want something to like, inherit.
We're not in here to want something to include the circle class, so I can have access to it to know
this type.
But like, not even considering that, what if I don't want to limit it to a circle?
What if I want it to be maybe another triangle or I want it to be a rectangle or something like that?
So, you know, that's kind of that's kind of a problem.
That I that I can't do any of those, I won't know which one it is.
So how would we resolve that?
And this is where the new Typekit comes in and this is something called a template.
So what we can do is we can kind of define and be ambiguous type and use that ambiguous type as the
data type here inside of our parameters.
That way, it won't really be set to a specific data type.
It'll kind of just be resolved at the time in which something is passed to this function.
So how would we do something like that?
Well, I'm just going to implement it just in triangle, and then I'm going to challenge you to implement
it across all of the classes by also adding it as an abstract method to the base class.
And then I'll go ahead and do that so you can see how that would work.
But first off, I'm just going to show you how a function template can work here just by using the triangle
class as an example.
So what I'm going to do is I'm going to type the word template universe and see line.
It's nice because it brings up not only the word template for me, but it also shows the next syntax
right here.
It needs to go inside of angle brackets similar to like when you used a vector.
So if you had a vector and you said vector of type and we're going to do something similar where we
say template and then we're just going to put some type of like class name in here, ambiguous class
name.
So I press tab and it already gives me those little angle brackets.
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
our ambiguous templated data type here.
So I have to put this above the member function that I'm interested in having be a template, no function,
so.
And here I'm going to use the capital T as the data type now, and I'm just going to say T shape.
So the type of the shape is kind of this ambiguous template, ambiguous template, and we're going to
call that type T because we have this class T in here.
So once I put that I actually need to have in C++ the definition of the function and the implementation
of the function in the same file.
So I can't just prototype the template and throw it into another file like this, I have to actually
put it.
In here in this header, since we're just using the header, it would be like that anyways, right?
We have our implementations here, so no big deal.
So I'm going to go ahead and just check to see whether the area of the this object is bigger or less
than the area of the T shape past object to the method.
So just one liner here, really.
I'm just going to say return and I'm going to say this, which is the object that's being called upon,
right?
So it'll be our triangle.
I will say if this arrow.
Area is greater than or I'm just going to say return, sorry.
What we want to do is we want to see if this shape in here has a greater area than this object.
So I'm just going to say return this area less than shape and shape.
I could have.
This is what I'm going to do is just reference the pointer, so I'll just do a dot.
So I'm going to say shape dot area.
And then so what I'll do is when I'm in the client file.
Since I have pointers here, I am going to just reference the pointer so I could pass an actual object
into here, like a sharp object, like some ambiguous one, some, you know, type of shape object,
whether it be a triangle circle or rectangle, but it's not going to be a pointer.
So that's why I'm going to expect just a shape.
That's why I'm using the dot here.
So now I have this template here and so I can use this T.
And we can automatically resolve what type of shape is going to get passed here and see, since they
all have an area, we know that we can just use this line and call area on both this and the shaped
area.
So let's go ahead and test this out, right?
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
sort of.
If check here and let's see what happens when I try and use this.
So let's say we put it in triangle, right?
So let's go ahead and see if we can use it in triangle.
So if I go t ts a point, right?
And I go arrow.
Notice that it's not giving me the that function from triangle.
So these functions were fine, right?
You notice that these two functions here were ones that were virtual functions in the abstract base
class for all of the shapes in all of the drive class shapes.
So they all have to have these.
So it's because I'm actually declaring the pointer as shape Typekit.
It's not really letting me access that.
So if I try and force this, let's see what's it called is bigger shape.
If I try and say Arrow is bigger shape, it doesn't like that doesn't recognize that function.
So sometimes we can do to resolve this is I'm actually just going to change all of these to order.
So now if I go.
And I do this arrow.
You notice I now have access to the is bigger shape memory function, so I can say if.
Is bigger shape and then let me pass a shape, right?
So how about we pass on little reference like this rectangle?
So I'll say star to reference it, right?
A star?
How do you reference it and give me not the pointer, but the actual rectangle object?
And so if what I'm checking right here is, I'm saying like, Oh, let's check it to see, is it true
or false, whether the rectangles area is bigger than the triangles area, right?
So in this case, I'll just print something out and I'll say on the rectangle is bigger.
I can even do something like this like, look, I could put the tape.
So I'll just say our tape
is bigger than.
Uh, I guess I should put the right.
Well, they say the
are Typekit is bigger than me, and I'll just say T type.
Cool.
So if the rectangle is in fact bigger than the triangle, then that should print out, OK?
So I didn't put any go.
So you notice right here, look at our rectangle, so it has a width of one and a height of one.
And look at a triangle here, let's just say if we change this to like.
Or.
And seven something like that.
You know, this would be twenty eight divided by two.
So there would be 14 step and mean bigger than the rectangle, right, so we can even double check that
here.
So why don't we just print out and uh, let's just say.
The are type one.
Why don't we do this
with area and we'll do our area?
It's bigger than a T tie
with area.
And the key area is kind of a long running line there.
But let's go ahead and that out, so I can only do this like decimals, we can make changes to you
like anything as well, so let's go ahead and just run this.
So it looks like it's not bigger.
So what if I change this to like 10 and I changed this to like 11?
Cool, so you notice that I put that with and without that there.
So let's go ahead and read that again about the space.
So the rectangle with area one sixteen point six is bigger than the triangle with area fifteen point
seven five.
So I should put a space there to.
So pretty cool, right?
So it's able to actually resolve this for anything like what if we change it to something else?
So I do the see for the circle.
And now we're passing the circle instead.
So let's go ahead and do that.
See notice now, it says.
We'd want to put the.
Different type for that, we'd want to put this circle right?
So the chances to see.
Because I change that to see.
So let's see what it says here, so says the circle with area to seventy seven point eighty seven speaking
in the triangle area fifteen point seven five.
So pretty cool, right?
So it's checking that that's better or bigger.
So what I want to challenge you to do now is to be able to kind of incorporate this with our polymorphic
design.
So you should be able to go into shape and make this present in each one of the.
Drive classes here.
So you have some sort of is bigger shape, so see if you can actually make that happen.
You may not be able to make it an abstract base class.
Sorry, an abstract function.
You know, that's pure ritual.
But just go ahead and try and see if you can make it work.
All right.
So with that, I will see you in the next video in which I will go over that solution with you.
And then we will talk about not only using templates for templated memory functions or just functions
in general.
We'll also be talking about using templates for classes.