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:
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>
Class a{public:virtual void Out2 (string s) {cout<< "A (OUT2):" <<s<<endl; }};
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>
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":
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>
Class a{public:virtual void Out1 (string s) = 0; virtual void Out2 (string s) {cout<< "A (OUT2):" <<s<<endl; }};
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>
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
650 "this.width=650;" src= "http:// Common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>
#include <iostream>using namespace std;class A{public: VIRTUAL&NBSP;VOID&NBSP;OUT1 () =0; ///implements virtual ~a () {}; by subclasses virtual void out2 () ///default implementation { cout<< "A (out2)" <<endl; } void &NBSP;OUT3 () ///Mandatory implementation { cout<< "A (OUT3)" <<endl; }};class b:public a{public: virtual ~b () {};&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;VOID&NBSP;OUT1 () { cout<< "B (OUT1)" <<endl; } void out2 () { cout<< "B (out2)" <<endl; } &NBSP;&NBSP;&NBSP;VOID&NBSP;OUT3 () { cout<< "B (OUT3)" <<endl; }};int main () { a * AB=NEW&NBSP;B;&NBSP;&NBSP;&NBSP;&NBSP;AB->OUT1 (); ab->out2 (); &NBSP;AB->OUT3 (); cout<< "************************" <<endl; &NBSP;&NBSP;B&NBSP;*BB=NEW&NBSP;B;&NBSP;&NBSP;&NBSP;&NBSP;BB->OUT1 (); bb->out2 (); &NBSP;&NBSP;&NBSP;&NBSP;BB->OUT3 (); delete ab; delete bb; return 0;}
650) this.width=650; "src=" Http://common.cnblogs.com/images/copycode.gif "alt=" Copy Code "style=" Border:none; "/>
Execution Result:
650) this.width=650; "src=" Http://images.cnitblog.com/blog/466768/201402/262307107295135.x-png "style=" border:0px ; "/>
This article from the "GDT Commentary" blog, reproduced please contact the author!
The difference between virtual function, pure virtual function and ordinary function in C + + class in inheritance