OK, so this will just be a short video on what I asked you to attempt in the last video, which was
to take this template template memory function that you created and implement it in the other derived
classes as well, and also attempt to combine it into the overall pie of polymorphic design by trying
to incorporate it into the base class and have it be, you know, a kind of abstracted function that
needs to be implemented in these other drive classes.
So let's go ahead and try then.
So we already have it here in triangle.
All I really need to do is copy and paste this for the base classes, so I'm going to go ahead and go
into a circle.
I'm going to assume in a little bit.
I noticed last year a little bit zoomed out, so hopefully you listen too hard for you to see it, but
I'm going to go ahead to the rectangle here.
Assuming a bit there to pace that as well, so we can just paste it right because it's called ambiguously.
It's bigger shape.
This is an ambiguous argument, you know, as far as the data type, and then we're just calling area
on whatever this shape may be, right?
This is referring to the class.
So that's why we can just paste it in shape since it's ambiguous shape.
This is fine.
Well, we don't want to change anything.
We literally just copy pasted it in the two other derived classes.
So what have we to here, though, in the base class and I say, OK, well, this is a virtual function
here, pure virtual function, and this is also a pure virtual function.
Well, I know that I want this to be in forcibly implemented into triangles, circle and rectangle,
all three of the drive classes, you know they should all have.
This is bigger things.
So what if I come here and I paste this?
I get rid of the implementation because it's not going to have something for shape that this will never
be instantiated, right?
But what if I say, you know, this is going to be a pure virtual function, I go here and I leave the
template and I save virtual and create a pure virtual function, both underline and read.
Why is it doing that?
Well, says virtual cannot be specified on member function templates.
If I over here.
Yeah, it's just a tied to this whole problem right here.
So why can I not do that?
Well, the problem is that you cannot declare a function, both virtual and also templated, and that
is because when it's templated, what actually happens is that it resolves the fact that there could
be different arguments at compile time and at compile time.
Let's say, for example, I well, let's just use this example, right?
I have like a potentially a triangle, a circle and a rectangle that can be passed to this right?
So a compile time was C++ compiler is actually going to do is it's going to generate generate three
different versions of this is bigger shape function.
So like, for example, if I go in here, what's going to happen is it's going to make an is bigger
shape or function where this would be a triangle.
It's going to make another version of it where this would be a circle and it's going to make another
version where this would be a rectangle.
So basically, it does that based on the fact that we would have three calls to this right now.
It might only consider the circle, but let's say that we have one of these calls for each one of the
shapes.
So, you know, like like, I would just take triangle, for example.
So we had it in triangle, right?
Let's say we go back to Maine and we actually had T. Arrow is bigger shape and we put a circle in here
and then we a T. Arrow is bigger shape.
And then we put that rectangle right with our star and then we put T is bigger shape with our T, which
is another triangle.
It would generate three different functions for this scene, the template it would know like, Oh,
I need to make three different versions of this for each type that could be passed to it.
So that's what happens with the templating of a member function.
It happens at compile time.
That's when I can reach those three things the virtual, though.
So let's go back to where the problem was right here.
Virtual is something that kind of resolves which function to call at runtime.
You know, when you make these virtual functions, each one of our drive classes has a different version
of it.
That type of resolution is something to happen at runtime, and C++ cannot handle having these two different
time periods of resolution on which thing to call.
They have not created support for that yet.
And it's just really difficult to make the compiler be able to handle such things.
So that is why you might have already noticed that this is not possible.
We cannot put a templated virtual spiritual function inside of the base class.
Not a total problem, though.
We can just delete this, all this and then each one will just have its own templated function and everything
will work fine and dandy.
So what we'd want to do at this point is go over to you may not see be and just kind of mix and match
this stuff.
So.
Maybe I'll just go in here and copy paste this.
So let's go right here and just say if maybe are as big a shape and then we'll put like tea in there
and it will just say on duty type with tea area.
Is bigger than the hour type.
With our area, and we could do it first circle as well, so I mean pace this year, and I'll just say,
see?
And then on this one this past like hour and we'll just say that the.
Our Typekit, our area is bigger than the sea type.
With the area, so now we can kind of.
Just refer to all of them, you know, and check on all of them where there's some other random shape,
you know, random mass and three different choices passed to it would be a bigger area wise.
So let's go ahead and run this.
So let's see, we only get one right now.
Was this the circle with this area is bigger than the triangle with this area, so we go ahead and have
to change some things.
Let's say what if we put like one hundred here?
So now we have one that sets a rectangle with area one one two six one six is bigger than the circle
with Area two seventy seven point two three seven.
Cool.
So you notice that are still working fine when we just implemented it in the derived classes, although
we weren't quite able to put it in here as a spiritual function like we wanted to, but that's OK.
We can just kind of resolve it by putting it in here and just leave it in the drive classes will still
have access to it.
OK.
So with that, I'm going to in this video and the next video, I'll go for what I said I would talk
about next, which is putting the templates on the classes as well.