One, the member function is overloaded characteristic:
(1) the same range (in the same class); (2) The function has the same name; (3) the parameters are different;
(4) The virtual keyword is optional.
Second, the characteristics of coverage:
(1) different ranges (base classes and derived classes)
(2) The function has the same name;
(3) the same parameters;
(4) Thevirtual keyword must have (base class);
Third, the hidden characteristics:
(1) different ranges (base classes and derived classes)
(2) The function has the same name;
(3) Same parameters && no virtual keywords (base class);
(4) Different parameters, virtual keyword is optional;
What characteristics does the following code match?
1 classA {2 Public:3 Virtual voidFun (intB =Ten){4cout <<"A Fun:"<< B <<Endl;5 }6 };7 8 classD | PublicA {9 Public:Ten voidFun (intB = -){ Onecout <<"B Fun:"<< B <<Endl; A } - }; - the intMain () - { - b b; -A &a =b; + A.fun (); -}
The output here is B fun:10; This answer makes me think for a long time, B fun is due to the coverage mechanism but 10 how to get it? I reverse the EXE found that the 10 at compile time has been determined, then what is the mechanism to let the compiler determine this parameter value is 10? Rather than 20. A &a actually calls the function itself within a, so the function argument comes from the default parameter of a, int B = 10; You may have questions: (learning is to think more)
What characteristics does the following code match:
1 classA {2 Public:3 voidFun (intB =Ten){4cout <<"A Fun:"<< B <<Endl;5 }6 };7 8 classD | PublicA {9 Public:Ten voidFun (intB = -){ Onecout <<"B Fun:"<< B <<Endl; A } - }; - the intMain () - { - b b; -A &a =b; + A.fun (); -}
View Code
Might think it is the same code, look closely will find that the base class of virtual disappeared; Here the output is a fun:10; here a points to the address of "a" and calls the fun function;
Thinking of a strong turn problem (to be mended);
C + + overloads, overlays, hidden issues