- [Instructor] Hi, this is Abstract Classes,
Virtual and Pure Virtual Functions.
In the previous video we got to know
about polymorphism.
In this video, we'll make a class abstract,
and try and understand abstract classes
with an example.
An abstract class is a class
that cannot be instantiated, and therefore
cannot be made into an object.
So it's code that will never be used then?
No, that's like paying an architect
to design your home and then never building it.
If we, or the designer of a class,
want to force its users to inherit it
before using their class,
they can make a class abstract.
Then we cannot make an object from it,
therefore, we must extend it first
and make an object from the sub-class.
To do so, we can make a function pure virtual
and not provide any definition,
then that function must be overridden,
rewritten, in any class that extends it.
Let's take a look at an example.
It will help.
We make a class abstract by adding
a pure virtual function, such as this
abstract animal class
that can only perform the generic action
of makeNoise.
As you can see, we add the C++ keyword
virtual before and equal to zero
after the function declaration.
Now any classes that extends inherits from animal
must override the makeNoise function.
This might make sense
since different types of animal
make very different types of noise.
We could perhaps have assumed
that anybody who extends the animal class
is smart enough to notice
that the animal class cannot make a noise,
and that they will need to handle it.
But what if they don't notice?
The point is, that by making a pure virtual function,
we guarantee that they will because they have to.
Abstract classes are also useful
because sometimes we want a class
that can be used as a polymorphic type,
but we need to guarantee it can never be used
as an object.
For example, animal doesn't really make sense
on its own.
We don't talk about animals,
we talk about types of animal.
We don't say, oh, look at that lovely,
fluffy, white animal, or yesterday
we went to the pet shop and got an animal
and an animal bed.
It's just too, well, abstract.
So an abstract class is kind of like
a template to be used by any class
that extends it, inherits from it.
If we were building an industrial empire type game
where the player manages businesses
and their employees, we might want a worker class,
for example, and extend it to make miner,
steel worker, office worker, and, of course,
programmer, but what exactly does a plain worker do?
Why would we ever want to instantiate one?
The answer is, we wouldn't want to instantiate one,
but we might want to use it as a polymorphic type
so we can pass multiple worker sub classes
between functions and have data structures
that can hold all types of workers.
All pure virtual functions must be overridden
by any class that extends the parent class
that contains the pure virtual function.
This means that the abstract class
could provide some of the common functionality
that would be available in all its sub classes.
For example, the worker class might have
the m_AnnualSalary,
m_Productivity,
and m_Age member variables.
It might also have the getPayCheck function,
which is not pure virtual,
and is the same in all the sub classes,
but it might have a doWork function,
which is pure virtual and must be overridden
because all the different types of worker
will do work very differently.
By the way, virtual, as opposed to pure virtual,
is a function that can be optionally overridden.
You declare a virtual function the same way
as a pure virtual function, but leave
the equal to zero off the end.
In the current game project,
we will use a pure virtual function.
If any of this virtual, pure virtual,
or abstract stuff is unclear,
using it is probably the best way
to understand it.
In this video, we have learned about
abstract classes, virtual and pure virtual functions.
Awesome.
Next up, we'll see
the building the PlayableCharacter class.