// calling.cpp -- defining, prototyping, and calling a function#include<iostream>voidsimple();// function prototypeintmain(){
using namespace std;
cout <<"main() will call the simple() function:\n";simple();// function call
cout <<"main() is finished with the simple() function.\n";// cin.get();return0;}// function definitionvoidsimple(){//< 在子函数中也要使用一条using编译指令,说明using的作用域也是在指定的地方开始,在当前代码块中结束0
using namespace std;
cout <<"I'm but a simple function.\n";}
返回引用的函数可以作为左值使用
//strc_ref.cpp -- using structure references#include<iostream>#include<string>struct free_throws
{
std::string name;int made;int attempts;float percent;};voiddisplay(const free_throws & ft);voidset_pc(free_throws & ft);
free_throws &accumulate(free_throws &target,const free_throws &source);intmain(){
free_throws one ={"Ifelsa Branch",13,14};
free_throws two ={"Andor Knott",10,16};
free_throws three ={"Minnie Max",7,9};
free_throws four ={"Whily Looper",5,9};
free_throws five ={"Long Long",6,14};
free_throws team ={"Throwgoods",0,0};
free_throws dup;set_pc(one);display(one);accumulate(team, one);display(team);// use return value as argumentdisplay(accumulate(team, two));accumulate(accumulate(team, three), four);display(team);// use return value in assignment//< 返回的是引用,直接将team的值复制给dup ,要是返回的不是应用就会先将值放入到一个特定的位置(函数返回值临时存放的地方)//< 然后在从临时存放的地方将临时值复制给sup而使用返回引用的方式就节省了中间的步骤
dup =accumulate(team,five);
std::cout <<"Displaying team:\n";display(team);
std::cout <<"Displaying dup after assignment:\n";display(dup);set_pc(four);// ill-advised assignment//< accumulate(dup,five)能作为左值,因为该函数返回的就是 dup自身accumulate(dup,five)= four;
std::cout <<"Displaying dup after ill-advised assignment:\n";display(dup);// std::cin.get();return0;}voiddisplay(const free_throws & ft){
using std::cout;
cout <<"Name: "<< ft.name <<'\n';
cout <<" Made: "<< ft.made <<'\t';
cout <<"Attempts: "<< ft.attempts <<'\t';
cout <<"Percent: "<< ft.percent <<'\n';}voidset_pc(free_throws & ft){if(ft.attempts !=0)
ft.percent =100.0f*float(ft.made)/float(ft.attempts);else
ft.percent =0;}//< 返回的是函数本身也就是该函数其实是可以作为左值使用的//< free_throws & target == 传入的参数自身,因此该函数返回的也是传入参数自身
free_throws &accumulate(free_throws & target,const free_throws & source){
target.attempts += source.attempts;
target.made += source.made;set_pc(target);return target;}
函数模板
// funtemp.cpp -- using a function template#include<iostream>// function template prototype
template <typename T>// or class TvoidSwap(T &a, T &b);intmain(){
using namespace std;int i =10;int j =20;
cout <<"i, j = "<< i <<", "<< j <<".\n";
cout <<"Using compiler-generated int swapper:\n";Swap(i,j);// generates void Swap(int &, int &)
cout <<"Now i, j = "<< i <<", "<< j <<".\n";double x =24.5;double y =81.7;
cout <<"x, y = "<< x <<", "<< y <<".\n";
cout <<"Using compiler-generated double swapper:\n";//< 使用函数模板是在编译阶段,编译器根据传入不同的参数将函数生成不同的函数用来调用Swap(x,y);// generates void Swap(double &, double &)
cout <<"Now x, y = "<< x <<", "<< y <<".\n";// cin.get();return0;}//< 程序员看不到这些,但是在编译器确实根据传入参数的不同生成了相应的函数// function template definition
template <typename T>// or class TvoidSwap(T &a, T &b){
T temp;// temp a variable of type T
temp = a;
a = b;
b = temp;}