c++prime plus 编程答案8.2

本文介绍了一个使用C++编写的简单程序,该程序定义了一个名为CandyBar的结构体,并演示了如何通过构造函数进行初始化及成员变量的赋值。此外,还展示了如何设置和显示结构体成员的具体内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include<iostream>
#include<string>
using namespace std;
struct CandyBar
{
char* name;
double weight;
int hot;
};
void Set(CandyBar &t,char *name="mlie",double weight=2.85,int hot = 350);
void show(const CandyBar &t);  //这个错误找了很久,const常量,声明和定义不一致了
void main()
{
struct CandyBar p ={"china",3.9,30};
Set(p);
show(p);
system("pause");
}
void Set(CandyBar &t,char *name ,double weight ,int hot)
{
int size =sizeof(name);
t.name = new char[size+1];
strcpy(t.name,name);
t.weight = weight ;
t.hot = hot;
}
void show(const CandyBar &t)
{
cout <<t.name<<"  "  <<t.hot<<"  "<<t.weight<<endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值