C++ primer plus 第六版 第八章 复习题

1.

执行时间很短,经常被调用的函数适合作为内联函数。

2.

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

b.不需要

//c.可以,如下
void song(const char * name = "O.My Papa", int times = 1);

3.

void iquote(int n)//int
{
    cout << "\"" << n << "\"";
}
void iquote(double a)//double
{
    cout << "\"" << a << "\"";
}
void iquote(const char * str)//string
{
    cout << '"' << str << '"';
}

4.

//a
void show(const box & fb)
{
    using namespace std;
    cout << "Maker: " << fb.maker << '\n';
    cout << " Height: " << fb.height << '\t';
    cout << "Width: " << fb.width << '\t';
    cout << "Length: " << fb.lenght << '\t';
    cout << "Volume: " << fb.volume << '\n';    
}
//b
void show(box & fb)
{
    using namespace std;
    fb.volume = fb.height * fb.width * fb.length;
    cout << "Volume: " << fb.volume << '\n';    
}

5.

6.

//a 可以使用函数重载
double mass(double density, double volume)
{
    return density * volume;
}

double mass(double density)
{
    const double volume = 1.0;
    return density * volume;    
}
//也可以用默认函数
double mass(double density, double volume = 1.0)
{
    return density * volume;
}
//b 只能函数重载
repeat(int times, const char * str);
repeat(const char * str);
//c 只能函数重载
average(int a, int b);
average(double m, double n);

d 不行,特征标相同

7.

template<typename T>//函数模板
T bigger(T a, T b)
{
    return a > b ? a : b; 
}

8.

template <> box max(box b1, box b2)
{
    return b1.volume > b2.volume ? b1: b2; 
}

9.

1)float 2)float& 3)float& 4)int 5)double

猜你喜欢

转载自blog.csdn.net/weixin_41882882/article/details/81288616