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

admin2019-05-06  23

问题 有如下程序:
    #include
    using namespace std;
    class Base{
    public:
    Base(int x=0):valB(x){cout<    ~Base(){cout<    private:
    int valB;
    };
    class Derived:public Base{
    public:
    Derived(int x=0,int y=0):Base(x),valD(y){cout<    ~Derived(){cout<    private:
    int valD;
    };
    int main(){
    Derived obj12(2,3);
    return 0;
    }
    运行时的输出结果是(    )。

选项 A、2332
B、2323
C、3232
D、3223

答案A

解析 在主函数中创建类Derived对象obj12(2,3)时,因为类Derived是从基类Base派生而来,所以首先调用基类的构造函数,输出2,再调用派生类构造函数,输出3。当主函数运算结果时,首先调用派生类的析构函数,输出3,再调用基类的析构函数输出2。所以输出结果为2332。
转载请注明原文地址:https://kaotiyun.com/show/aKAp777K
0

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