《C++ Primer Plus(第六版)》(11)(第八章 函数探幽 复习题答案)
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; }
//原来的代码
#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; }
人生如戏,还是戏如人生?微信公众号:传说之路
csdn博客 http://blog.csdn.net/u012175089/article/list/2