第8章 函数探幽复习题

1

使用高度频繁,而且操作内容简单的情况下,用inline函数。这是C++为了取代宏定义而设置的函数

2

a

void song(char *name, int times = 1);

b

无需修改

## c

```cpp
void song(const char *name = "O.My Papa",int times);

3

void iquote(int a)
{
cout<<"\""<<a<<"\'"<<endl;
}
void iquote(double a)
{
cout<<"\""<<a<<"\""<<endl;
}
void iquote(string a)
{
cout<<"\""<<a<<"\""<<endl;
}

4

a

void show(box &b)
{
	cout << "maker: " << b.maker << endl;
	cout << "height: " << b.height << endl;
	cout << "width: " << b.width << endl;
	cout << "length: " << b.length << endl;
	cout << "volume: " << b.volume << endl;
}

b

void set(box &b)
{
	volume = height * width * length;
}

5

a

//默认函数
doubel mass(double density, double volume = 1);
//函数重载
double mass(double density, double volume);
double mass(double density);

b

//函数重载
void show(int n, char *name);
void show(char *name);

c

//函数重载
int avg(int, int);
double avg(double, double);

d

两种方法都无法完成

6

template <class Any>
void Maximum(Any &a, Any &b);

Any Maximum(Any &a, Any &b)
{
	if (a >= b);
		return a;
	else
		return b;
}

7

template mass(box a, box b)
{
return (a.volume > b.volume) ? a : b;
}

发布了42 篇原创文章 · 获赞 1 · 访问量 1591

猜你喜欢

转载自blog.csdn.net/qq_32631105/article/details/104224983