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

本文深入探讨了C++中函数重载和模板的使用方法,包括默认参数、函数重载实例和函数模板的应用场景。通过具体代码示例展示了如何实现和调用这些高级特性,为读者提供了在实际编程中解决问题的实用技巧。
默认参数
  设置方法:函数原型

  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
  只有在函数很短时才能采用内联方式
  引用变量是一种伪指针,它允许为变量创建别名(另一个名称)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值