8.1 内联函数

使用内联函数
inline double square(double x) { return x * x; }
cout << "a = " << a << endl;
8.2 引用变量

将引用用作函数参数(使用const)
double cube(const double &ra);
cout << cube(x) << " = cube of " << x << endl;
cout << cube(5) << " = cube of " << "5" << endl;
cout << cube(x+5) << " = cube of " << x+5 << endl;
double cube(const double &ra) {
将引用用于结构
void set_pc(free_throws &ft);
void display(const free_throws &ft);
free_throws &accumulate(free_throws &target, const free_throws &source);
free_throws one = { "Rick", 13, 14 };
free_throws two = { "Jack", 10, 16 };
free_throws team = { "All", 0, 0 };
display(accumulate(team, one));
void set_pc(free_throws &ft) {
ft.percent = 100.0 * float(ft.made) / float(ft.attempts);
void display(const free_throws &ft) {
cout << "Name: " << ft.name << endl;
cout << "Made: " << ft.made << '\t';
cout << "Attempts: " << ft.attempts << '\t';
cout << "Percent: " << ft.percent << endl;
free_throws &accumulate(free_throws &target, const free_throws &source) {
target.attempts += source.attempts;
target.made += source.made;
将引用用于类的对象
string version1(const string &s1, const string &s2);
const string &version2(string &s1, const string &s2);
cout << "Enter a string: ";
cout << "You string: " << input << endl;
result = version1(input, "***");
cout << "Your string enhanced: " << result << endl;
cout << "Your input: " << input << endl;
cout << "-------------------------------------" << endl;
result = version2(input, "###");
cout << "Your string enhanced: " << result << endl;
cout << "Your input: " << input << endl;
string version1(const string &s1, const string &s2) {
const string &version2(string &s1, const string &s2) {
对象、继承和引用
void file_it(ostream &os, double fo, const double fe[], int n);
const char *fn = "ep-data.txt";
cout << "Can't open " << fn << "." << endl;
cout << "Enter the focal length of telescope objective in mm: ";
for (int i = 0; i < LIMIT; i++) {
cout << "Eyepieces #" << i + 1 << ": ";
file_it(cout, objective, eps, LIMIT);
file_it(fout, objective, eps, LIMIT);
void file_it(ostream &os, double fo, const double fe[], int n) {
os << "Focal length of objective: " << fo << endl;
os << "f.l. eyepieces" << " magnification" << endl;
for (int i = 0; i < n; i++) {
os << " " << fe[i] << " " << int(fo / fe[i] + 0.5) << endl;
8.3 默认参数

默认参数的用法(取出字符串的前n个值)
char *left(const char *str, int n = 1);
cout << "Enter a string: " << endl;
char *ps = left(sample, 4);
char *left(const char *str, int n) {
while (m < n && str[m] != '\0') m++;
char *p = new char[m + 1];
for (i = 0; i < m; i++) {
8.4 函数重载

函数重载示例(取出字符串/数字的前n个值)
char *left(const char *str, int n = 1);
unsigned long left(unsigned long num, unsigned int ct);
const char *trip = "Hawaii";
unsigned long n = 12345678;
for (i = 0; i < 10; i++) {
cout << left(n, i) << endl;
char *left(const char *str, int n) {
while (m < n && str[m] != '\0') m++;
char *p = new char[m + 1];
for (i = 0; i < m; i++) {
unsigned long left(unsigned long num, unsigned int ct) {
if (num == 0 || ct == 0) return 0;
while (n /= 10) digits++;
8.5 函数模板

函数模板示例(交换两个数的值)
cout << "i, j = " << i << ", " << j << "." << endl;
cout << "Afer swap, i, j = " << i << ", " << j << "." << endl;
cout << "x, y = " << x << ", " << y << "." << endl;
cout << "Afer swap, x, y = " << x << ", " << y << "." << endl;
重载的模板示例(交换两个数或两个数组)
void Swap(T a[], T b[], int n);
void show(int arr[], int n);
cout << "i, j = " << i << ", " << j << "." << endl;
cout << "Afer swap, i, j = " << i << ", " << j << "." << endl;
int d1[LIMIT] = { 0,7,0,4,1,7,7,6 };
int d2[LIMIT] = { 0,7,2,0,1,9,6,9 };
cout << "Original arrays: " << endl;
cout << "After swap: " << endl;
void Swap(T a[], T b[], int n) {
for (int i = 0; i < n; i++) {
void show(int arr[], int n) {
for (int i = 0; i < n; i++) {
调用函数时的最佳匹配(打印数组内容)
void ShowArray(T arr[], int n);
void ShowArray(T *arr[], int n);
int things[6] = { 13,31,103,301,310,130 };
for (int i = 0; i < 3; i++) {
void ShowArray(T arr[], int n) {
cout << "template A:" << endl;
for (int i = 0; i < n; i++)
void ShowArray(T *arr[], int n) {
cout << "template B:" << endl;
for (int i = 0; i < n; i++)
引导编译器使用指定函数(打印较小的值)
int lesser(int a, int b) {
double x = 15.5, y = -25.9;
cout << lesser(m, n) << endl;
cout << lesser(x, y) << endl;
cout << lesser<>(m, n) << endl;
cout << lesser<int>(x, y) << endl;