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;

}


posted @ 2014-04-10 14:04  天下纵横C++  阅读(243)  评论(0编辑  收藏  举报