Pure virtual functions and abstract classes
A pure virtual function is a virtual function without a function body. What does it mean to have no function body? This is the function defined like this:
virtual return value function name (formal parameter) = 0;
As you can see, the definition of virtual and virtual function are the same before, with an =0 added at the end. Indicates that there is no function body, this is a pure virtual function . A class that contains pure virtual functions is an abstract class . An abstract class has at least one pure virtual function.
The existence of abstract classes is to provide a highly abstract, unified interface to the outside world, and then use their different methods through polymorphic features. This is one of the core ideas of C++ object-oriented design and software engineering.
The characteristics of abstract classes are summarized as follows:
1. An abstract class cannot instantiate an object. It can only be used as a base class to allow derived classes to complete the pure virtual functions in it, and then instantiate and use it.
2. The derived abstract class can still incompletely complete the pure virtual functions in the base class and continue to be derived as an abstract class. Until the definition of all pure virtual functions is given, it becomes a concrete class and the object cannot be instantiated.
3. Because abstract classes are abstract and cannot be embodied, they cannot be used as parameter types, return values, or cast types.
4. Continuing with the third item, abstract classes can define a pointer or reference type pointing to its derived class to achieve polymorphic features.