单选题
C++
题号 #2607030
有如下程序:
#inClude<iostream> using namespaCe std; Class Base{
publiC:
Base(int x=0){Cout<<x;} };
Class Derived:publiC Base{ publiC:
Derived(int X=0){Cout<<x;} private:
Base val; };
int main() {
Derived d(1); return 0;
}
执行这个程序的输出结果是( )。
参考答案
D
答案解析
本题考查派生类的构造函数和析构函数,在定义一个派生类的对象时,先调用基类的构造函数,然后再执行派生类的构造函数,对象释放时,先执行派生类的析构函数,再执行基类的析构函数。所以本题中定义了一个对象d,先执行基类的构造函数输出0,因为派生类中的私有数据成员为Base,所以还会执行一次基类的构造函数输出0,最后执行派生类的构造函数输出1,所以本题答案为D。
你可能感兴趣的试题
A.类Person是类Undergraduate的基类
B.类Undergraduate从类Student公有继承
C.类Student是类Person的派生类
D.类Undergraduate是类Person的派生类
A.1B
B.lD
C.2B
D.2D
A.BBB
B.BBD
C.DBB
D.DBD