我正在研究 C++。以下是我的代码:
#include <iostream>
using namespace std;
class base
{
public:
virtual void display(int a = 4)
{
cout << "base :: "<< a*a << endl;
}
};
class derived : public base
{
public:
void display(int b = 5)
{
cout << " Derived :: " << b*b*b << endl;
}
};
int main()
{
base *bobj;
derived dobj;
bobj = &dobj;
bobj->display();
return 0;
}
输出是:
Derived :: 64
基类的函数被调用,但使用派生函数的参数的默认值。为什么派生类方法 display() 采用基类方法参数值?