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 (class) cannot be called abstract classes.
2. Virtual functions can be used directly, or the Quilt Class (sub Class) overloads are later called in polymorphic form, whereas pure virtual functions must be implemented in subclasses (sub Class) in order to be used, because pure virtual functions are only declared and not defined in the base class.
3. Virtual and pure virtual functions can be overloaded in subclasses (sub Class) and called in polymorphic form.
4. Virtual functions and pure virtual functions usually exist in the abstract base class (abstract base CLASS-ABC) and are inherited subclasses overloaded to provide a uniform interface.
5. Virtual function definition form: Virtual {method Body}
The definition of pure virtual function: virtual {} = 0;
There cannot be a static identifier in the definition of a virtual function or a pure virtual function, for the simple reason that the static modified function requires a prior bind at compile time, but the virtual function is dynamically bound (run-time bind) and is modified by both the function lifecycle (Life recycle ) is not the same.
6. The virtual function must be implemented, if not implemented, the compiler will give an error:
Error lnk****: unresolved external symbol "Public:virtual void __thiscall
Classname::virtualfunctionname (void) "
7. For virtual functions, both the parent and child classes have their own versions. Dynamically bound when called by polymorphic mode.
8. The subclass of the pure virtual function is implemented, and the virtual function is programmed in the subclass, the subclass of which is the grandson class can overwrite
The virtual function is dynamically bound when called by a polymorphic method.
9. Virtual functions are the mechanisms used to implement polymorphism (polymorphism) in C + +. The core idea is to access the derived class definition through the base class.
Function
10. Polymorphism refers to different implementation actions when the same object receives different messages or different objects receive the same message. C + + supports two kinds of polymorphism: compile-time polymorphism, run-time polymorphism.
A. Compile-time polymorphism: using overloaded functions
B Run-time polymorphism: implemented by virtual functions.
11. If a class contains pure virtual functions, any statements attempting to instantiate the class will result in an error, because the abstract base class (ABC) cannot be called directly. You must invoke a method of its subclass after the class inherits the overload, as required.
#include <iostream>using namespacestd;classvirtualbase{ Public: Virtual voidDemon () =0;//Prue virtual function Virtual voidBase () {cout <<"This is farther class"<<Endl;}};//Sub ClassclassSubvirtual: Publicvirtualbase{ Public: voidDemon () {cout<<"This is subvirtual!"<<Endl; } voidBase () {cout<<"This is subclass Base"<<Endl; } }; voidMain () {virtualbase* Inst =NewSubvirtual ();//multstate PointerInst-Demon (); Inst-Base (); //Inst = new Virtualbase (); //inst->base ()System"Pause"); return; }
The usage and difference of virtual function and pure virtual function in C + +