有以下程序,在横线应添加( )。 #include using namespace std; class TestClass { public: TestClass(int n){number=n;}

admin2020-07-23  36

问题 有以下程序,在横线应添加(    )。
    #include
    using namespace std;
    class TestClass
    {
    public:
    TestClass(int n){number=n;}
    //拷贝构造函数
    ~TestClass(){}
    private:
    intnumber;
    };
    TestClass fun(TestClass p)
    {
    TestClass temp(p);
    retum temp;
    }
    int main()
    {
    TestClass obj1(10),obj2(0);
    TestClass obj3(obj1);
    obj2=fun(obj3);
    return 0:
    }

选项 A、TestClass(TestClass
    &other){number=other.number;}
B、TestClass {TestClass
    other){number=other.number;}
C、TestClass(TestClass&other){number;}
D、TestClass(&other){number=other.number;}

答案A

解析 拷贝构造函数也是构造函数,但它只有一个参数,这个参数是本类的对象,即other,所以赋值操作将本类的参数other.number赋值给number;而且采用对象的引用的形式,也就是&other。
转载请注明原文地址:https://jikaoti.com/ti/F8l0FFFM
0

最新回复(0)