January 2019
Intermediate to advanced
458 pages
10h 35m
English
At times, it doesn't make a lot of sense for a base class to have an implementation for a class method, yet at the same time we wish to force any derived classes to implement that method. The answer to this problem is virtual methods.
Take the following class definition:
class A {
public:
virtual bool methodA() = 0;
virtual bool methodB() = 0;
};
If we try to derive from this class, we must implement these two class methods or get a compiler error. Since both of the methods in the base class are virtual, the entire base class is referred to as a virtual base class. This is particularly useful for when you wish to define an interface that can be implemented by a range of different classes, yet keep the convenience of ...