8.7 复习题

1.经常被调用,逻辑简单,代码少,一般要求没有递归,没有循环。

2.

void song(const char * name, int times);
a.void song(const char * name, int times = 1);
b.加默认值只改原型就行了
c.void song( char * name = "oh, my god", int times = 1);

3.

void iquote(int x)
{cout << "\"" << x << "\""<<endl;
}
void iquote(double x)
{cout << "\"" << x << "\"" << endl;
}
void iquote(string x)
{cout << "\"" << x.c_str() << "\"" << endl;
}
int main()
{ iquote(1);iquote(2.123456);iquote("FableGame,http://blog.csdn.net/u012175089");cin.get();return 0;
}

4.

结构:

struct box
{char maker[40];float height;float width;float length;float volume;
};
//显示
void show(box& b)
{cout << "maker:" << b.maker << " height:" << b.height << " width:"<<b.width<< " length:" << b.length << " volume:"<< b.volume;
}
//计算体积
void computeVolume(box& b)
{b.volume = b.height * b.width * b.length;
}
//main函数
int main()
{box b{ "FableGame", 1, 2, 3, 0 };computeVolume(b);show(b);cin.get();return 0;
}

5.

//原来的代码

#include <array>
#include <string>
#include <set>
using namespace std;const int Seasons = 4;
const array<string, Seasons> Snames = { "Spring", "Summer", "Fall", "Winter" };
void fill(array<double, Seasons> * pa);
void show(array<double, Seasons> da);

int main()
{array<double, Seasons> expenses;fill(&expenses);show(expenses);cin.get();cin.get();return 0;
}

void fill(array<double, Seasons> * pa)
{for (int i = 0; i < Seasons; i++){cout << "Enter" << Snames[i] << " expenses: ";cin >> (*pa)[i];}
}

void show(array<double, Seasons> da)
{double total = 0.0;cout << "\nEXPENSES\n";for (int i = 0; i < Seasons; i++){cout << Snames[i] << ":$" << da[i] << endl;total += da[i];}cout << "Total Expenses: $" << total << endl;
}

//修改后的代码

#include <iostream>
#include <array>
#include <string>
#include <set>
using namespace std;const int Seasons = 4;
const array<string, Seasons> Snames = { "Spring", "Summer", "Fall", "Winter" };
void fill(array<double, Seasons>&pa);
void show(array<double, Seasons>& da);

int main()
{array<double, Seasons> expenses;fill(expenses);show(expenses);cin.get();cin.get();return 0;
}

void fill(array<double, Seasons>& pa)
{for (int i = 0; i < Seasons; i++){cout << "Enter" << Snames[i] << " expenses: ";cin >> pa[i];}
}

void show(array<double, Seasons>& da)
{double total = 0.0;cout << "\nEXPENSES\n";for (int i = 0; i < Seasons; i++){cout << Snames[i] << ":$" << da[i] << endl;total += da[i];}cout << "Total Expenses: $" << total << endl;
}

区别不是很大,不过原理是不同的。

6.

//a

double mass(double density, double volume = 1.0);

//或者重载

double mass(double density, double volume);
double mass(double density);

//b.默认函数只能放在后边,所以只能重载

void repeat(int times, const char* str);
void repeat(const char* str);

//c.可以使用模板函数

template< typename T> T average(T a, T b);

//也可以使用重载函数

int average(int a, int b);
double average(double a, double b);

//d.不能根据返回类型来决定函数的选择,两个版本的特征标相同。

7.

template<typename T> T getMax(T a, T b)
{return a > b ? a : b;
}

int main()
{cout << getMax(1, 2) << endl;cout << getMax(12.323, 3.333) << endl;cin.get();cin.get();return 0;
}

8.书上印错了,是复习题7的模板

#include <iostream>
#include <array>
#include <string>
#include <set>
using namespace std;struct box
{char maker[40];float height;float width;float length;float volume;
};
//显示
void show(box& b)
{cout << "maker:" << b.maker << " height:" << b.height << " width:" << b.width<< " length:" << b.length << " volume:" << b.volume<< endl;
}
//计算体积
void computeVolume(box& b)
{b.volume = b.height * b.width * b.length;
}

//函数模板
template<typename T> T getMax(T a, T b)
{return a > b ? a : b;
}
//模板的具体化
template<> box getMax(box a, box b)
{return a.volume > b.volume ? a : b;
}
//main
int main()
{cout << getMax(1, 2) << endl;cout << getMax(12.323, 3.333) << endl;box a{ "FableGame A", 12, 23, 33, 0 };computeVolume(a);show(a);box b{ "FableGame B", 41, 12, 13, 0 };computeVolume(b);show(b);box c = getMax(a, b);show(c);cin.get();cin.get();return 0;
}

9.对于变量加(),就是引用

int g(int x)
{return x;
}
int main()
{float m = 5.5f;float& rm = m;decltype(m) v1 = m;//floatdecltype(rm) v2 = m;//float&decltype((m)) v3 = m;//float&decltype(g(100)) v4 = g(12345);//intdecltype(2.0 * m) v5 = 2.0 * m;//doublecout << v1 << "\t" << v2 << "\t" << v3 << "\t" << v4 << "\t" << v5 << "\t" << endl;m = 123.123f;cout << v1 << "\t" << v2 << "\t" << v3 << "\t" << v4 << "\t" << v5 << "\t" << endl;cin.get();cin.get();return 0;
}

转载于:https://www.cnblogs.com/fablegame/p/6430254.html

《C++ Primer Plus(第六版)》(11)(第八章 函数探幽 复习题答案)相关推荐

  1. C++ Primer Plus 第六版 所有章节课后编程练习答案

    我的独立博客地址:www.blog4jimmy.com,欢迎大家关注 下面的是C++ Primer Plus 第六版所有章节的课后编程练习的答案,都是博主自己写的,有不对的地方请大家留言指出讨论讨论 ...

  2. C Primer Plus第六版(中文版)编程练习答案(完美修订版)汇总

    //本文是博主编写的C Primer Plus第六版(中文版)编程练习答案的所有链接; //使用超链接汇总于此,若是有用请点赞收藏并分享给他人; C Primer Plus 第六版(中文版)第二章(完 ...

  3. 深夜里学妹竟然问我会不会C?我直接把这篇文章甩她脸上(C Primer Plus 第六版基础整合)

    C Primer Plus 第六版 前言 第一章 初识C语言 一.C语言的起源 二.C语言的应用 三.C语言的特点 四.编译的过程 五.编码机制 1.简述 2.完成机制 六.在UNIX系统上使用C 七 ...

  4. C primer plus(第六版)第十一章源代码

    C primer plus(第六版)第十一章源代码 /* 11.1 */ #include<stdio.h> #define MSG "I am a symbolic strin ...

  5. C++ Primer Plus第六版第六章编程练习 第4题, 加入Benevolent Order of Programmer后,在BOP大会上

    /*************************************************************************************************** ...

  6. 数据库系统概念原书第六版黑皮书第一章课后习题作业答案

    文章目录 1.8列出文件处理系统和DBMS之间的四个显著区别. 1.9 解释物理数据独立性的概念,以及它在数据库系统中的重要性. 1.10 列出数据库管理系统的五个职责.对每个职责,说明当它不能被履行 ...

  7. C Primer Plus (第六版) 第七章 7.11编程练习 参考答案

    11.ABC 邮购杂货店出售的洋蓟售价为 2.05 美元/磅,甜菜售价为 1.15美元/磅,胡萝卜售价为 1.09美元/磅.在添加运费之前,100美元的订单有5%的打折优惠.少于或等于5磅的订单收取6 ...

  8. C Primer Plus(第六版)第三章 数据和C

    笔记记录 1.float 类型可以储存带小数的数字. 2.printf()中使用%f来处理浮点值.%.2f中的.2用于精确控制输出,指定输出的浮点数只显示小数点后面两位. 3.scanf()函数用于读 ...

  9. C Primer Plus 第六版 章节课后编程练习答案(下)(缘更)

    接上次写的前五章的答案,这次更新后面章节的 PS:目录在左边袄┗|`O′|┛ ~~ 第六章 6.1 #include <stdio.h> int main(void) {char lett ...

最新文章

  1. 高端人工智能服务器,产品技术-HPE Apollo6500 Gen10服务器:人工智能的高速引擎-新华三集团-H3C...
  2. 基于UDP的DDos反射放大攻击
  3. Linux学习:vim 的 3 种命令模式及基本操作
  4. 为KindEditor编辑器中的内容增加样式,使得自定义前台页面显示效果与编辑器效果一致
  5. 找不到托盘菜单配置文件_Windows 10最新更新导致用户文件丢失和加载错误配置文件...
  6. windows清理剪切板
  7. 30万条数据,搜索文本字段的各种方式对比
  8. mysql中select 的题型_MYSQL经典题型详情解析
  9. numpy 矩阵 秩_大规模电商推荐数据分析-基于矩阵分解的召回
  10. Asp.Net无刷新分页( jquery.pagination.js)
  11. asp.net中实现登陆的时候用SSL
  12. 精度、召回率、准确率、F-score
  13. C++ 对txt文档进行编辑
  14. html框架自动居中,html 宽度固定并布局居中模板框架
  15. window版ELK搭建
  16. 【考研经验】2019双非应届生清华大学计算机专业课和复试心得
  17. 用java做出长方体的表面积_编写java程序,输入一个长方体的长、宽、高,求长方体的表面积和体积,并将结果显示,一个长方体的...
  18. AI-K210 开发家庭万用宝模组(1)
  19. yum install时提示This system is not registered with an entitlement server
  20. win11右键菜单怎么回到win10旧版

热门文章

  1. 用mysql制作一个登录_连接数据库制作一个简单的登入页面1
  2. 单刹车信号不合理故障_航班盘旋数十圈返航 天津航空:刹车温度传感器等故障...
  3. python求数组的所有组合_使用numpy构建两个数组的所有组合的数组
  4. 主题目录检索是什么举例子_网络信息资源检索与利用第二次作业
  5. 敏捷开发之产品级经验分享
  6. DSP之通信之MMC控制器
  7. 2012.4.16总结(三)
  8. 蓝牙学习笔记(四)——AC692x程序烧录
  9. C语言:编写一个函数,计算二维数组中的最大元素,数组以指针的方式传递
  10. Spark自定义分区器