c++程序填空:请将Smaple类的声明补全,使程序可以输出9(换行)22(换行)5。

(注意,已有代码不可删除或修改) #include 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我还能理解,你其他两个输出没有说明任何规则,咋写?

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y

 


#include <iostream>

using namespace std; 
class Sample 
{ 
public: 
    int v; 
    Sample(int value = 0) {
        v = value;
    }
}; 

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

// Output

5                                                                                                                                                                                  
20                                                                                                                                                                                 
5 
#include<iostream>
using namespace std;
class Sample { 
public: 
	int v; 
	Sample(){};
	Sample(int v){
		this->v = 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;
}