以下答案本人在linux vscode中均已亲自测试编译通过,完美运行.
8.1
#include<iostream>
using namespace std;
void fun(const char *, int flag = 0);
int main(void)
{
const int size = 20;
char str[size] = "I am is a str!!";
fun(str); //count == 1
fun(str); //count == 2
fun(str,2020); //count == 3, 总共打印5遍字符串
}
void fun(const char * p, int flag)
{
static int count = 0;
count++;
if(!flag)
cout << p << endl;
else
{
for(int i = 0; i < count; i++)
cout << p << endl;
}
}
8.2
#include<iostream>
#include<cstring>
using namespace std;
struct CandyBar
{
char name[20];
double weight;
int Calories;
};
void setCandyBar(CandyBar & CBr, const char * Name = "Millennium Munch", double Wg = 2.85, int Cal = 350);
void show(const CandyBar & CBr);
int main(void)
{
CandyBar CB;
char name[20]{0};
double wg;
int cal;
cout << "***现在是默认参数:" << endl;
setCandyBar(CB);
show(CB);
cout << "请输入品牌名称:";
cin.getline(name, 30);
cout << "请输入重量和热量:";
cin >> wg >> cal;
setCandyBar(CB, name, wg, cal);
cout << "***现在不是默认参数:"