有如下程序: #include using namespace std; class Base { int x; public: Base(int n=0): x(n) { cout

admin2020-06-29  16

问题 有如下程序:
#include
using namespace std;
class Base {
int x;
public:
Base(int n=0): x(n) { cout<int getX()const { return x; }
};
class Derived: public Base {
int y;
public:
Derived(int m, int n): y(m), Base(n) { cout<Derived(int m): y(m) { cout<};
int main()
{
Derived d1(3), d2(5,7);
return 0;
}
执行这个程序的输出结果是(    )。

选项 A、375
B、357
C、0375
D、0357

答案C

解析 本题考查派生类的构造函数和析构函数,在定义一个派生类的对象时,先调用基类的构造函数,然后再执行派生类的构造函数,对象释放时,先执行派生类的析构函数,再执行基类的析构函数。本题中定义了一个对象d1,先执行基类的构造函数输出0,再执行派生类的构造函数输出3,然后定义了一个对象d2(5,7),其中需要调用基类的构造函数输出7,最后输出5,所以答案为C。
转载请注明原文地址:https://jikaoti.com/ti/Qrl0FFFM
0

相关试题推荐
随机试题
最新回复(0)