Abstract classes - virtual and pure virtual functions

An abstract class is a class that cannot be instantiated and therefore cannot be made into an object.

Tip

Some terminology we might like to learn here is concrete class. A concrete class is any class that isn't abstract. In other words, all the classes we have written so far have been concrete classes and can be instantiated into usable objects.

So, it's code that will never be used, then? But 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 (re-written) in any class that extends it.

Let's 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:

Class Animal 
   private: 
      // Private stuff here 
 
   public: 
 
      void virtual makeNoise() = 0; 
 
      // More public stuff here 
}; 

As you can see, we add the C++ keyword, virtual, before, and = 0 after the function declaration. Now, any class 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, "Ooh, 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, Steelworker, OfficeWorker, 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 can 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 doWork very differently.

Note

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 = 0 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.132.123