有如下程序: #include using namespace std; class Base1 { public: Base1(int d) { cout

admin2015-06-27  21

问题 有如下程序:
#include
using namespace std;
class Base1
{
public:
Base1(int d) { cout<~Base1() { }
};
class Base2
{
public:
Base2(int d) { cout<~Base2() { }
};
class Derived : public Base1,Base2
{
public:
Derived(int a,int b, int c, int d):Base1(b), Base2(a), b1(d), b2(c) { }
private:
int b1;
int b2;
};
int main()
{
Derived d(1,2,3,4);
return 0;
}
执行这个程序的输出结果是(     )。

选项 A、1234
B、2134
C、12
D、21

答案D

解析 本题考查派生类的构造函数和析构函数,在定义一个派生类的对象时,先调用基类的构造函数,然后再执行派生类的构造函数,对象释放时,先执行派生类的析构函数,再执行基类的析构函数。本题中定义一个派生类对象时,分别调用基类的构造函数,所以分别输出21。
转载请注明原文地址:https://kaotiyun.com/show/wBBD777K
0

最新回复(0)