1#include <iostream>2#include <ctime>3#include <stdio.h>4#include <cstring>5#include <cstdlib>6#include <map>7#include <string>8 using namespacestd;9 classATen { One Public: A A () { -cout <<"A ()"<<Endl; - } the -~A () { -cout <<"~a ()"<<Endl; - } + Virtual voidFuncintval =1) - { +Std::cout <<"a->"<< Val <<Std::endl; A } at - Virtual voidTest () - { - func (); - } - }; in - classB: PublicA to { + Public: - B () { thecout <<"B ()"<<Endl; * } $ Panax Notoginseng~B () { -cout <<"~b ()"<<Endl; the } + voidFuncintval =0) A { theStd::cout <<"b->"<< Val <<Std::endl; + } - }; $ $ #if1 - intMain () { -B*p =NewB; theP->test (); - Deletep;Wuyi } the #endif
View Code
Since P points to an object inheriting Class B, after calling Test in a, the virtual function func () invokes the function that inherits Class B, and the default parameter still uses the parameter 1 of the Class A where test is located, thereby outputting the b->1.
Default parameter value issues in class inheritance