- Virtual function declarations such as the following:
Virtual returntype functionname (Parameter)
The virtual function must be implemented, even if the null implementation, if not implemented, the compiler will error. Its purpose is to enable it to be rewritten in subclasses to be polymorphic and, of course, not to rewrite.
- Pure virtual function declarations such as the following:
Virtual returntype functionname (Parameter) = 0;
A pure virtual function must not be defined, just a function declaration, pure virtual function is used to standardize the behavior of derived classes, that is, interfaces. Classes that include pure virtual functions are abstract classes, and abstract classes cannot be instantiated, but can be used only after being inherited and overriding their pure virtual functions. Pure virtual functions must be implemented within subclasses.
- virtual function of the class is used to implement inheritance, the same time inherited interfaces inherit the implementation of the parent class, pure virtual function is concerned about the unity of the interface, the implementation has a subclass complete.
- For virtual functions, both the parent and child classes have their own version number. Dynamically bound when called by polymorphic mode.
- A subclass of a pure virtual function is implemented, and the pure virtual function becomes a virtual function in the subclass.
- Virtual functions are the mechanisms used in C + + to implement polymorphism. The core idea is to access the functions defined by the derived class through the base class.
C + +-virtual functions and pure virtual functions