您的位置:首页 > 编程语言 > C语言/C++

C++ Primer Plus学习:第八章 函数探幽(2)

2011-09-06 15:26 519 查看
默认参数

设置方法:函数原型

eg

#include <iostream>
using namespace std;

const int ArSize = 80;
char * left(const char * str,int n = 1);

int main()
{
char sample[ArSize];
cout<<"Enter a string:\n";
cin.get(sample,ArSize);
char *ps = left(sample,4);
cout<<ps<<endl;
delete []ps;
ps = left(sample);
cout<<ps<<endl;
delete []ps;

system("pause");
return 0;
}

char * left(const char * str,int n)
{
if(n<0)
n = 0;
char *p = new char[n+1];
int i;
for(i=0;i<n&&str[i];i++)
p[i] = str[i];
while(i<=n)
p[i++] = '\n';
return p;
}


函数重载

可以有多个同名的函数

eg

#include <iostream>
using namespace std;

unsigned long left(unsigned long num,unsigned ct);
char *left(const char *str,int n = 1);

int main()
{
char *trip = "Hawaii!!";
unsigned long n = 12345678;
int i;
char *temp;

for(i = 1;i<10;i++)
{
cout<<left(n,i)<<endl;
temp = left(trip,i);
cout<<temp<<endl;
delete[] temp;
}

system("pause");
return 0;
}

unsigned long left(unsigned long num,unsigned ct)
{
unsigned digits = 1;
unsigned long n = num;

if(ct == 0||num == 0)
return 0;
while(n/=10)
digits++;
if(digits>ct)
{
ct = digits-ct;
while(ct--)
num/=10;
return num;
}
else
return num;
}

char *left(const char *str,int n)
{
if(n<0)
n = 0;
char *p = new char[n+1];
int i;
for(i = 0;i<n&&str[i];i++)
p[i]=str[i];
while(i<=n)
p[i++]='\n';
return p;
}


函数模板

eg

#include <iostream>
using namespace std;

template <class Any>
void Swap(Any &a,Any &b);

int main()
{
int i = 10;
int j = 20;
cout<<"i = "<<i<<",j = "<<j<<endl;
cout<<"Using compiler-generated int swapper:\n";
Swap(i,j);   //generates void Swap(int &,int &)
cout<<"Now i = "<<i<<",j = "<<j<<endl;

double x = 24.5;
double y = 71.9;
cout<<"x = "<<x<<",y = "<<y<<endl;
cout<<"Using compiler-generated double swapper:\n";
Swap(x,y);   //generates void Swap(double &,double &)
cout<<"Now x = "<<x<<",y = "<<y<<endl;

system("pause");
}

//function template definition
template <class Any>
void Swap(Any &a,Any &b)
{
Any temp;
temp = a;
a = b;
b = temp;
}


重载

eg1

#include <iostream>
using namespace std;

template <class Any>
void Swap(Any &a,Any &b);

template <class Any>
void Swap(Any *a,Any *b,int n);

void Show(int a[]);
const int Lim = 8;

int main()
{
int i = 10,j = 20;
cout<<"i = "<<i<<", j = "<<j<<endl;
cout<<"Using compile-generated int swapper:\n";
Swap(i,j);
cout<<"Now i = "<<i<<",j = "<<j<<endl;

int d1[Lim] = {0,7,0,4,1,7,7,6};
int d2[Lim] = {0,6,2,0,1,9,6,9};
cout<<"Original arrays:\n";
Show(d1);
Show(d2);
Swap(d1,d2,Lim);
cout<<"Swapped arrays:\n";
Show(d1);
Show(d2);

system("pause");
return 0;
}

template <class Any>
void Swap(Any &a,Any &b)
{
Any temp;
temp = a;
a = b;
b = temp;
}

template <class Any>
void Swap(Any a[],Any b[],int n)
{
Any temp;
for(int i = 0;i<n;i++)
{
temp = a[i];
a[i] = b[i];
b[i] = temp;
}
}

void Show(int a[])
{
cout<<a[0]<<a[1]<<"/";
cout<<a[2]<<a[3]<<"/";
for(int i = 4;i<Lim;i++)
{
cout<<a[i];
}
cout<<endl;
}


eg2

#include <iostream>
using namespace std;

template <class Any>
void Swap(Any &a,Any &b);

struct job
{
char name[40];
double salary;
int floor;
};

template <> void Swap<job>(job &j1,job &j2);
void Show(job &j);

int main()
{
cout.precision(2);
cout.setf(ios::fixed,ios::floatfield);
int i = 10,j = 20;
cout<<"i = "<<i<<",j = "<<j<<endl;
cout<<"Using compiler-generated int swapper:\n";
Swap(i,j);
cout<<"Now i = "<<i<<",j = "<<j<<endl;

job sue = {"Susan Yaffee",7300.60,7};
job sidney = {"Sidney Taffee",78060.72,9};
cout<<"Before job swapping:\n";
Show(sue);
Show(sidney);
Swap(sue,sidney);
cout<<"After job swapping:\n";
Show(sue);
Show(sidney);

system("pause");
return 0;

}

template <class Any>
void Swap(Any &a,Any &b)
{
Any temp;
temp = a;
a = b;
b = temp;
}

template <> void Swap<job>(job &j1,job &j2)
{
double t1;
int t2;
t1=j1.salary;
j1.salary = j2.salary;
j2.salary = t1;

t2 = j1.floor;
j1.floor = j2.floor;
j2.floor = t2;
}

void Show(job &j)
{
cout<<j.name<<":{1}quot;<<j.salary<<" on floor "<<j.floor<<endl;
}


重载解析

创建候选函数列表

使用候选函数列表创建可行函数列表

确定是否有最佳的可行函数

匹配:最佳到最差

Note

只有在函数很短时才能采用内联方式

引用变量是一种伪指针,它允许为变量创建别名(另一个名称)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: