C++自学笔记(day5)

四、模板

1.函数模板

funcTemplate.cpp
#include <iostream>
using namespace std;
/*
* 模板: 支持参数多态化的工具,让函数或者类具备通用性。
* 目的: 让程序员编写与类型无关的代码,是泛型编程的基础
* 分类:
*   1、函数模板
*       仅针对函数参数类型、返回值类型不同, 功能相同的函数
*   2、类模板
*       类模板针对仅数据成员和成员函数类型不同的类
*
*   template <class T, typename B .....>
*       template: 关键字,说明是一个模板
*       class T : 定义类型模板形参, class 等价于 typename
*       T       :类型模板形参,用来替换数据类型
*   注意:
*       T 为类型模板形参,那么就可以有 默认模板参数,但是32系统编译时必须 : -std=c++0x
* */
#if 0
template <class T> T add(T x, T y)         //声明定义一个函数模板
{
    return x + y;
}
#else 
template <class T = int> T add(T x, T y);         //声明一个函数模板
#endif

int main(int argc, char *argv[])
{
    int a, b;
    cin >> a >> b;
    cout << add(a, b) << endl;                      //函数模板的隐式调用,自动匹配数据类型
    cout << add(3.1, 2.1) << endl;    

    cout << add< >(12, 34) << endl;                 //函数模板的显式调用,自动匹配数据类型,也就是 T 代表了 int 类型 
    cout << add< float >(1.1, 2.1) << endl;         //函数模板的显式调用,手动传递数据类型,将float 传递赋值给了 T 
}
#if 0

#else
template <class B> B add(B x, B y)          //定义一个函数模板
{
    return x + y;
}
#endif

funcTemplate.cpp
#include <iostream>
using namespace std;

template <class T, class B> B add(T x, B y){    //可以有默认参数,但没有意义
    return x+y;
}

int main()
{
    cout << add(1, 2) << endl;  //隐式调用,自动匹配类型 T代表int B代表int
    cout << add(1, 1.2) << endl; //隐式调用,自动匹配类型 T代表int B代表double
    cout << add< >(2, 3) << endl;
    cout << add< >(2.1, 4) << endl;
    cout << add<int, double>(1, 3.1) << endl;
    cout << add<int>(2, 3.1) << endl;   //与第一个实参 2进行匹配,若类型不同强转实参的数据类型,后一个形参B自动匹配
    cout << add<double>(2, 3.1) << endl;    //double与2数据类型不同,将2强转为double类型,B自动匹配
    cout << add<double, int>(2, 3.1) << endl;
    return 0;
}

2.非类型函数模板

noTypeTemplate.cpp
#include <iostream>
using namespace std;
#define MAX 12
/*
* 使用非类型模板形参 目的: 让模板中的常数具备灵活性
* */
template <class T, int max = MAX> bool getmem(T **p)
{
    char buf[max] = {0};
    cout << sizeof(buf) << endl;
    *p = new T[max];
    if (p)
        return true;
    return false;
}
int main(int argc, char *argv[])
{
    int *p = NULL;
    if (getmem<int, 32>(&p)){
        for(int i = 0; i < MAX; i++)
            p[i] = i;
        for(int i = 0; i < MAX; i++)
            cout << p[i] << " ";
        cout << endl;
    }
}

3.类模板

classTemplate.cpp
#include <iostream>
#include <algorithm>
#include <string>
/*
类模板:
语法:
    template <class 形参名> class 类名{ 类体 };
注意事项:
    1.成员函数在类模板外定义的基本语法,必须单独使用template说明
        语法为template <class 形参名> 返回值 类名<形参名>::函数名(<参数列表>){ 函数体 }
    2.类模板定义对象不能隐式说明
      类模板定义对象不能自动匹配类型
      模板形参有默认值时,可以使用自动匹配类型的写法
*/
using namespace std;
template <class T=int>
class Demo{
public:
    Demo(T);
    T getValue();
    void setValue(T);
private:
    T x;
};

//成员函数在类模板外定义的基本语法,必须单独使用template说明
//注意:作用域访问符前必须是 类名<类型参数>::
template <class T>
Demo<T>::Demo(T x):x(x){   }

template <class T>
T Demo<T>:: getValue(){
    return x;
}

template <class T>
void Demo<T>::setValue(T x){
    this->x = x;
}

int main()
{
    //Demo obj(12);         //错误,类模板定义对象不能隐式说明
    //Demo <>obj(12);       //错误,类模板定义对象不能自动匹配类型
    Demo <int>obj(12);    
    cout << obj.getValue() << endl;

    Demo <>obj1(15);        //这种使用方法,要求模板形参必须有默认值
    return 0;
}

4.非类型类模板

ClassTemplate.cpp
#include <iostream>
using namespace std;
template <class T, int max>
class Demo{
public:
    Demo(T);
private:
    T buf[max];
};

template <class T, int max>
Demo<T, max>::Demo(T x){
    for(int i=0; i<max; i++){
        buf[i] = x+i;
    }
}
int main()
{
    Demo<int, 10>obj(3);
    return 0;
}

5.友元模板函数

friendClassTemplate.cpp
#include <iostream>
using namespace std;
template <class T>
class Base{
public:
    Base(T x):x(x){}
#if 0
    T operator+(Base<T> &obj){
        return x + obj.x;
    }
#elif 0
    T operator+(Base<T> &);
#elif 0
    friend T operator+(Base<T> &obj1, Base<T> &obj2){
        return obj1.x + obj2.x;
    }
#else 
    template <class B>
    friend B operator+(Base<B> &, Base<B> &);
#endif
private:
    T x;
};
#if 0
#elif 0
template <class B> B Base<B>::operator+(Base<B> &obj)
{
    return x + obj.x;
}
#else
template <class T>
T operator+(Base<T> &obj1, Base<T> &obj2)
{
    return obj1.x + obj2.x;
}
#endif
int main(int argc, char *argv[])
{
    Base<int> a(2), b(3);
    cout << a + b << endl;

    Base<double> c(2.1), d(3.1);
    cout << c + d << endl;
}

C++自学笔记(day6)
C++自学笔记(目录)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值