006:奇怪的类复制
总时间限制:
1000ms
内存限制:
65536kB
// 在此处补充你的代码
描述
程序填空,使其输出9 22 5
#include <iostream> using namespace std; class Sample { public: int v;
}; void PrintAndDouble(Sample o) { cout << o.v; cout << endl; } int main() { Sample a(5); Sample b = a; PrintAndDouble(b); Sample c = 20; PrintAndDouble(c); Sample d; d = a; cout << d.v; return 0; }
输入
无
输出
9
22
5
样例输入
None
样例输出
9 22 5
详细解答见代码中的注释
#include <iostream>
using namespace std;
class Sample {
public:
int v;
// 在此处补充你的代码
Sample(const int a):v(a+2) {}
Sample() {}
};
void PrintAndDouble(Sample o)
{
cout << o.v;
cout << endl;
}
int main()
{
Sample a(5);
PrintAndDouble(a); // 这个函数也执行了拷贝构造
Sample b = a; // 拷贝构造
PrintAndDouble(b);
Sample c = 20;
PrintAndDouble(c);
Sample d;
d = a; // 这里就是赋值,没有拷贝构造
cout << d.v;
system("pause");
return 0;
}