C++面向对象程序设计 006:奇怪的类复制 ---- (北大Mooc)

文章目录


原题题目

在这里插入图片描述

#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;
}



代码实现

    Sample(const Sample & a)
    {
    
    
        v = (a.v+2);
    }
    Sample(int a)
    {
    
    
        v = a;
    }
    Sample()
    {
    
    
        v = 22;
    }

猜你喜欢

转载自blog.csdn.net/qq_37500516/article/details/114703302