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

admin2010-06-06  32

问题 有下列程序,在横线添加;    #include<iostream>    using namespace std;    class TestClass    {    public:    TestClass (int n){number=n;}    ______//拷贝构造函数    ~TestClass(){}    private:    int number;    };    TestClass fun(TestClass p)    {    TestClass temp(P);    return temp;    }    int main()    {    TestClsss 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/V1W0FFFM
0

最新回复(0)