您的位置:首页 > 其它

函数模版与类模版简单实例--学习笔记

2011-04-26 16:07 375 查看
/******************************************************
 * 功能: 函数模版和类模版的使用
 * 时间:2011-04-26
 ******************************************************/
#include <iostream>

using namespace std;

#define _DEBUG_FUNCTION_TEMPLATE
#define _DEBUG_CLASS_TEMPLATE

#ifdef _DEBUG_FUNCTION_TEMPLATE
/*函数模版*/
template<class T>
T Add(T a, T b){
    T c;
    c = a+b;
    cout<<"函数模版内计算结果:"<<c<<endl;
    return c;
} 
#endif

#ifdef _DEBUG_CLASS_TEMPLATE
template<class T>
class TempClass{
     public:
        TempClass(){};//注意要有{}
        T Add(T a, T b){
            T c;
            c = a+b;
            cout<<"类模版内计算结果:"<<c<<endl;
            return c;
        };
       virtual  ~TempClass(){};
}; //注意要有;
#endif
int main()
{
#ifdef _DEBUG_FUNCTION_TEMPLATE
    cout<<"调用函数模版计算结果:"<<Add<int>(3,4)<<endl;
#endif

#ifdef _DEBUG_CLASS_TEMPLATE
    TempClass<int> templt;
    cout<<"调用类模版计算结果:"<<templt.Add(3,4)<<endl;
#endif
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: