C++ primer plus第六版课后编程题答案8.2


 

8.2

#include <iostream>
#include <string>
using namespace std;
struct CandyBar{
	char *name;
	double weight;
	int kaluli;

};
void set(CandyBar &cb,char *name="Millennium Munch",double weight=2.85,int kaluli=350);//默认初始化
void show(const CandyBar &cb);
void main82()
{
	CandyBar c1,c2;
	set(c1,"DEFU",20.5,800);
	set(c2);
	system("pause");


}
void set(CandyBar &cb,char *name,double weight,int kaluli)
{
	int size=sizeof(name);
	cb.name=new char(size+1);
	strcpy(cb.name,name);
	cb.weight=weight;
	cb.kaluli=kaluli;
	show(cb);

}
void show(const CandyBar &cb)
{
	cout<<"name:"<<cb.name<<"\nweight:"<<cb.weight
		<<"\nkaluli:"<<cb.kaluli<<endl;
	cout<<"show end!"<<endl<<endl;

}

 

 


发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注