《C++ Primer Plus(第六版)》(11)(第八章 函数探幽 复习题答案)
2016-12-14 20:12
615 查看
8.7 复习题
1.经常被调用,逻辑简单,代码少,一般要求没有递归,没有循环。
2.
void song(const char * name, int times);
3.
void iquote(int x)
{
cout << "\"" << x << "\""<<endl;
}
4.
结构:
struct box
{
char maker[40];
float height;
float width;
float length;
float volume;
};
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);
#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);
6.
//a
double mass(double density, double volume = 1.0);//或者重载
7.
template<typename T> T getMax(T a, T b)
{
return a > b ? a : b;
}
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;
};
9.对于变量加(),就是引用
int g(int x)
{
return x;
}
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;//float decltype(rm) v2 = m;//float& decltype((m)) v3 = m;//float& decltype(g(100)) v4 = g(12345);//int decltype(2.0 * m) v5 = 2.0 * m;//double cout << 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; }
相关文章推荐
- 《C++ Primer Plus(第六版)》(12)(第八章 函数探幽 编程题答案)
- 《C++ Primer Plus(第六版)》(10)(第八章 函数探幽 笔记)
- 《C++ Primer Plus(第六版)》(32)(第十五章 友元、异常和其他 复习题答案)
- 《C++ Primer Plus(第六版)》(23)(第十二章 类和动态内存分配 复习题和答案)
- C++PrimerPlus第八章学习笔记——函数探幽
- 【读书笔记:C++ primer plus 第六版 中文版】第8章 函数探幽
- 《C++ Primer Plus(第六版)》(35)(第十六章 string类和标准模板库 复习题答案)
- C++ Primer plus 学习笔记之第八章函数探幽(1)
- C++ Primer Plus学习:第八章 函数探幽(1)
- 《C++ Primer Plus(第六版)》(9)(第七章 函数 笔记和答案)
- 2012/1/21 《C++ Primer Plus》第八章:函数探幽 学习笔记
- 《C++ Primer Plus(第六版)》(29)(第十四章 C++中的代码重用 复习题答案)
- 《C++ Primer Plus(第六版)》(44)(第十八章 探讨C++新标准 复习题和答案)
- 《C++ Primer Plus(第六版)》(40)(第十七章 输入、输出和文件 复习题和答案)
- 2012/1/21 《C++ Primer Plus》第八章:函数探幽 学习笔记
- C++ Primer Plus学习:第八章 函数探幽(2)
- C++ Primer Plus第六版 第八章 编程练习答案
- 【C++ Primer plus】【第八章】函数探幽
- 《C++ Primer Plus(第六版)》(16)(第十章 对象和类 笔记和复习题答案)
- 《C++ Primer Plus(第六版)》(19)(第十一章 使用类 复习题答案)