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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值