问题:
解答:
#include <iostream>
using namespace std;
typedef struct _CandyBar
{
string brand;
float weight;
int hot;
}CandyBar;
void fill(CandyBar& cb, const char* name="Millennium Munch", double w=2.85, int h=350)
{
cb.brand = name;
cb.weight = w;
cb.hot = h;
}
void show(const CandyBar& cb)
{
cout << "糖果棒的品牌为:" << cb.brand << endl;
cout << "糖果棒的重量为:" << cb.weight << endl;
cout << "糖果棒的热量为:" << cb.hot<< endl;
}
int main()
{
CandyBar cb;
fill(cb);
show(cb);
cout << endl;
fill(cb, "德芙", 2.3, 231);
show(cb);
return 0;
}
运行结果:
考查点:
- 默认参数
注意:
- 默认参数只能从参数最左边开始设置.
2024年9月1日20:48:24