1. Virtual functions and pure virtual functions can be defined in the same class, classes containing pure virtual functions are called abstract classes, and classes that contain only virtual functions cannot be called abstract classes.
2. Virtual functions can be used directly, or in the form of a multi-state overload, and the pure virtual function must be implemented in the subclass to be used, because the pure virtual function is declared in the base class and not defined.
3. Virtual and pure virtual functions can be overloaded in subclasses and called in polymorphic form.
4. Virtual functions and pure virtual functions usually exist in the abstract base class, and are inherited subclass overloads to provide a uniform interface.
5. The definition of virtual function: virtual{}; The definition of pure virtual function: virtual {} = 0; There cannot be a static identifier in the definition of a virtual function and a pure virtual function , for the simple reason that the static The modified function requires early binding at compile time, but the virtual function is dynamically bound , and the function life cycle of the two modifiers is not the same.
Virtual function fully embodies the two characteristics of inheritance and polymorphism in object-oriented thought, which is widely used in C + + language. For example, in the Microsoft MFC class Library, you will find that many functions have the virtual keyword, that is, they are virtual functions. No wonder some people even call virtual functions The essence of the C + + language .
A pure virtual function is defined so that the base class cannot be instantiated , since it is meaningless to instantiate such an abstract data structure itself or to give the implementation.
Pure virtual function is only an interface, is a function of the declaration, it is to be left in the subclass to achieve.
Virtual functions can also not be overloaded within subclasses, but pure virtual must be implemented in subclasses, just like Java interfaces. Usually we add a lot of functions to virtual, is a good habit, although sacrificing some performance, but increased the object-oriented polymorphism, because you can hardly anticipate the parent class inside the function is not to modify its implementation of the child class
C + + virtual function, pure virtual function