有如下程序: #include<iostream> using narnespace std; class test{ private: int a; public: test0{cout+’’con

admin2020-04-07  14

问题 有如下程序:
    #include<iostream>
    using narnespace std;
    class test{
    private:
    int a;
    public:
    test0{cout+’’constructor’’+endl;}
    test(int a){cout+a+endl;}
    test(const test&test)
    {
    a=test.a;
    cout+’’copy constructor’’+en.d1;
    }
    test(){ cout+’’destructor"+endl;}
    };
    int main()
    {
    testA(3);
    return 0;
   }
    运行时输出的结果是(    )。

选项 A、3
B、constructor
destructor
C、copy constructor
destructor
D、3
destructor

答案D

解析 此题考查的是构造函数和析构函数。一个类可以有多个构造函数,但只能有一个析构函数。每一个对象在被创建时,都会隐含调用众多构造函数中的一个,而在销毁时又会隐含调用唯一的析构函数。此题中,主函数创建对象A是会隐含调用。test(int a)这个构造函数,输出3;接着主函数结束,对象A销毁又隐含调用~test()析构函数输出destructor。
转载请注明原文地址:https://kaotiyun.com/show/YR8p777K
0

最新回复(0)