One question:
# Include <iostream> using namespace STD; Class A1 {public: Virtual int F (INT n = 5) {return 2 * n ;}}; Class B: Public A1 {public: int F (INT n = 10) {return 3 * n ;}}; int main () {A1 * P = new B (); cout <p-> F () <Endl; return 0 ;}
The output result is 15.
Result Analysis:
1,BFunctions inInt F (INT)It is also a virtual function.(Similar to the function prototype of the base class)So the pointer call is dynamic binding, that is, what is actually called here isBInF (INT).
2The default real parameters of the virtual function are determined by the type of the function called, and are not related to the dynamic type. The base class pointer is called here, so the real parameter is the default real parameter in the base class.5.
About dynamic binding:
1. It must be called by pointer or reference;
2. It must be a virtual function.
Both conditions must be met at the same time.
The function in the derived class must be exactly the same as the prototype of the virtual function of the base class: The function name, the parameter list is exactly the same, and the keyword virtual can be dispensable.
If the function prototype is not exactly the same, it will be a new function. At this time, the new function will overwrite the inherited virtual function.