1. Virtual function (impure virtual)
The virtual function of C + + has the function of " runtime polymorphism ", which provides the implementation of virtual function in the parent class, and provides the default function implementation for subclasses .
Subclasses can override the virtual functions of the parent class to implement the specialization of subclasses.
The following is a virtual function in a parent class:
Class a{public: void Out2 (string s) {cout<<"A (OUT2):" < <s<<Endl;}};
2. Purely virtual functions (pure virtual)
A class containing pure virtual functions in C + + is referred to as an "abstract class." Abstract classes cannot use the new out object, only subclasses that implement the pure virtual function can be new out of the object.
Pure virtual Functions in C + + are more like "provide only declaration, no implementation", are the constraints of a subclass, and are " interface Inheritance ".
Pure virtual Functions in C + + are also a "run-time polymorphism".
If the following class contains pure virtual functions, it is "abstract class":
Class a{public: void Out1 (string s) =0void Out2 (string s) {cout<< "A (OUT2):" <<s<<Endl;};
3. Common function (no-virtual)
Normal functions are statically compiled, with no run-time polymorphism, and only call their own normal functions based on pointers or referenced "literal" class objects .
The normal function is the " enforced implementation " provided by the parent class for the child class.
Therefore, in an inheritance relationship, subclasses should not override the normal functions of the parent class, because the invocation of the function is related to the literal value of the class object.
4. Program Synthesis Examples
#include <iostream>using namespacestd;classa{ Public: Virtual voidOUT1 () =0;///Subclass Implementation Virtual~A () {}; Virtual voidOut2 ()///Default Implementation{cout<<"A (OUT2)"<<Endl; } voidOUT3 ()///Force Implementation{cout<<"A (OUT3)"<<Endl; }};classB: Publica{ Public: Virtual~B () {}; voidout1 () {cout<<"B (OUT1)"<<Endl; } voidOut2 () {cout<<"B (OUT2)"<<Endl; } voidout3 () {cout<<"B (OUT3)"<<Endl; }};intMain () {A(A =NewB; AB-out1 (); AB-Out2 (); AB-out3 (); cout<<"************************"<<Endl; B*bb=NewB; BB-out1 (); BB-Out2 (); BB-out3 (); DeleteAB; DeleteBB; return 0;}
C + + In the inheritance of virtual functions, pure virtual functions, ordinary functions, the difference between the three