So we're going to wrap up polymorphism with one more sub topic, so it's a pretty short portion of the
course, but there is one more thing to discuss and that is abstract base classes.
So you've seen what we've done so far.
We basically have a shape based class and then we have triangle and rectangle and circle as the derive
classes.
So in the last lesson we talked about instantiating these and having them be in dynamic memory as a
pointer, and we showed how we could kind of use some of the memory functions from the shape class and
even call them a shape pointer.
But they would be resolved through the methods and the data members to the correct and derived class.
So if you notice the main CBP, you remember we say Shape Pointer as an R.A. and C, and this was respectively
rectangle triangle and circle.
And even though we were calling the same named methods, it was able to resolve them correctly.
So what are we going to talk about now is the fact that shape isn't really something that we should
be instantiating really, because it's actually not specified, a shape is kind of an ambiguous thing
like you must have a type of shape, right?
So if someone says, oh, draw a shape, you would be like, Well, what shape do I try to draw rectangle
or triangle or circle something more complex?
So for that reason, we shouldn't really have shape be something that can be instantiated in the client
code.
And so that leads us to talk about this notion of an abstract base class.
So if we go into this shape based class.
What we can do is make some small changes, and we will turn this into just an abstraction where it
can be used as a base scaffolding for the rest of the derived classes.
So it's not meant to be instantiated, but it's just meant to help create a structure.
So when you're designing your program, you might actually want to start out with an abstract base class
just to solidify your idea of, Hey, I'm going to have different shapes.
Why don't I make a base class college shape and put some of the shared information between shapes here
in this base class?
But I don't want people to be able to create a shape object.
I only want them to be able to create specific types of shaped objects like triangle, circle and rectangle.
So we only need to do a few things to change our project here into something that uses an abstract base
class.
So all I'm going to do is I am going to go to these virtual functions and I'm going to put in equals
zero right here and then commented out.
So you notice that I'm not having any implementation here, the return zero is completely committed
out here and the return shape is come into doubt as well.
On this tape member function the equals zero might seem kind of weird, but it's really just a syntax
to say, Hey, this is an abstracted member function.
It is not something that's meant to be implemented in this class.
It is something that actually must and I say must, because it's required now that you have an area
and a type of function in every one of the classes that inherits from the shape class.
So this triangle circle and rectangle must all have both area and type member functions now.
And that is because of this syntax that we just put here.
With the equals zero.
So once again, that makes it so these are not really ever going to be used in the shape class and really,
that's not going to ever be a problem because we're never going to instantiate a shape object here.
But it's required now that every class that inherits from shape must implement any one of the functions
here that you have in the base class that say equals zero.
These are something called pure virtual functions.
So this one and this one are both pure virtual functions.
Another thing that happens by doing this is it turns this base class from a normal based class into
an abstract base class, which abstract base class?
This is what we're talking about in this specific video.
And really, the main thing it means is that the fact that we are not going to instantiate it.
So since we're not going to instantiate it, let's go back to Maine and see what happens after we've
modified these and it's turned our shape class into an abstract base class.
So I go back here and noticed that this is underlined in red now.
If I hover over it, it says allocating an object of abstract class type shape, and it gives me two
options right here.
So look, it says unemployment pure virtual method area in shape and implement pure virtual method type
in shape.
And remember also, I've said this reminder a few times, but whenever I say method that is synonymous
with memory function and you see that even though we call new member functions, sea lion even puts
method in here.
So it gives you the option to click both of one or both of these to get rid of that pure virtual method
area in shape.
So if I click on this?
Quick unemployment, that method, it takes me here, so I could actually just if I wanted to get rid
of that, but I don't want to, I still want to be an abstract base class.
And you know, it's giving us this problem.
So what I'm going to need to do is comment this out or completely get rid of it because we can no longer
implement a shape.
Because of the House, and you get rid of this because this was as was the actual shape, and we're
not going to be trying to print out any of this information about the generic shape.
We need a specific type of shape now.
So all I have to do is comment out this line here on Line 10 and these two lines 15 in line 16, assuming
bit the rest, we left the same.
So I'll go ahead and run this.
And you notice that everything still runs fine, except now we are using it how we're supposed to,
where we're using the actual type of shape rather than just some ambiguous shape object, right?
So we have the red rectangle, the yellow triangle and the green circle with their areas being printed
out appropriately as well.
So we didn't have to change a whole lot.
This part of polymorphism is really about the design idea, so it's a really important part of object
oriented programming.
And the idea is that when you're coming up with your design for your object or in your programming classes
and program overall, the structure you can be thinking about how you can come up with a base class
that will allow you to build out a derived class.
So think of different things that have a generic commonality like maybe you know, a car like we've
dealt with car before, right?
We have a car and we have certain types of car or like an airplane class.
And then you have certain types of airplane like you're not just going to instantiate a generic airplane
type, you're going to only instantiate certain types of airplane, right?
Just like shape.
You know, like we said in the beginning, if I say draw a shape, you would probably ask, well, what
type of shape?
Because you wouldn't know what to draw.
You would just have to dress whatever type of shape that you know of, right?
But shape is something to ambiguous to abstract, hence the name abstract base class.
So that's why we use this just as a base to build off of, right?
We have some common methods or member functions here that go along with every type of shape, right
triangle circle and rectangle.
They all have their own type of area that should be implemented.
Write their own formulas for it.
They also have a name for their type of shape, right?
So they're going to be able to return the specific string that's associated with the name or the type
of the shape.
That's why these are appropriate to be pure virtual functions because we know we're going to implement
these in every type of shape or every derived class that comes from these shape.
Abstract base class.
OK, so that's really all I have for this lesson, and this pretty much wraps up polymorphism, so make
sure that you review anything that you're not completely sold on or go back to the video.
We will probably still be using it throughout the class and in the next section, we're going to build
off of our knowledge and go into templates.
All right.
So with that, I will see you in the next lesson.