单选题 有如下程序:
#include<iostream>
using namespace std;
class Base {
int x;
public:
Base(int n=0): x(n) {cout<<n;}
int getX()const {return x;}
};
class Derived: public Base {
int y;
public:
Derived(int m, int n): y(m), Base(n) {cout<<m;}
Derived(int m): y(m) {cout<<m;}
};
int main()
{
Derived d1(3), d2(5, 7);
return 0;
}
执行这个程序的输出结果是______。
【正确答案】 C
【答案解析】[解析] 本题考查派生类的构造函数和析构函数,在定义一个派生类的对象时,先调用基类的构造函数,然后再执行派生类的构造函数,对象释放时,先执行派生类的析构函数,再执行基类的析构函数。本题中定义了一个对象d1,先执行基类的构造函数输出0,再执行派生类的构造函数输出3,然后定义了一个对象d2(5, 7),其中需要调用基类的构造函数输出7,最后输出5,所以答案为C。