當派生類和基類的虛構函數默認實參不同時;
派生類的實參將接收基類傳遞過來的默認參數
示列如下:
[cpp]
#include <iostream>
#include <ostream>
#include <string>
using namespace std;
class B{
public:
virtual void fun(int num = 0)
{
cout << "B = " << num << endl;
}
};
class D: public B{
public:
virtual void fun(int num = 1)
{
cout << "D = " << num << endl;
}
};
int main()
{
D d;
B *p = &d;
p -> fun();
B &b = d;
b.fun();
return 0;
}
#include <iostream>
#include <ostream>
#include <string>
using namespace std;
class B{
public:
virtual void fun(int num = 0)
{
cout << "B = " << num << endl;
}
};
class D: public B{
public:
virtual void fun(int num = 1)
{
cout << "D = " << num << endl;
}
};
int main()
{
D d;
B *p = &d;
p -> fun();
B &b = d;
b.fun();
return 0;
}
運行結果如下:
D = 0
D = 0