#include "stdafx.h" #include <iostream> #include <algorithm>using namespace std;class base{public:base (int i): M_num (i) {cout<< "Base Constructor" <<ENDL;} Virtual ~base () {cout<< "Base deconstructor" <<ENDL;} virtual void func (int num=-100) {cout<<num<<endl;cout<< "This is Base class" <<ENDL;} Private:int M_num;}; Class Derived:public base{public:derived (): Base (Ten) {cout<< "Derived constructor" <<ENDL;} ~derived () {cout<< "Derived deconstructor" <<ENDL;} void func (int num=100) {cout<<num<<endl;cout<< "This is Derived class" <<endl;}}; int main () {{base* pb=new Derived ();cout<< "-------------------------" <<endl;pb->func ();cout<< "-------------------------" <<endl;derived* pd=dynamic_cast<derived*> (Pb);pd->func ();cout<< "-------------------------" <<endl;delete PB;} System ("pause"); return 0;}
Summarize:
The default parameter of the virtual function, corresponding to the pointer type that called the virtual function