模板类 例子
2015-09-02 18:13
357 查看
TemplateDemo.h
TemplateDemo.cpp
.h文件中类名前的 template< class Type>几乎是固定的,
.cpp文件中也是,不过在类名后又多了< Type>.仔细看看。
#ifndef TEMPLATE_DEMO_HXX #define TEMPLATE_DEMO_HXX template<class Type> class Math{ public: Math(); Type Add(Type a, Type b); }; #endif
TemplateDemo.cpp
#include "TemplateDemo.h" template<class Type> Math<Type>::Math(){}; template<class Type>Type Math<Type>::Add(Type a, Type b) { return a + b; } int main() { Math<int> demo1; cout << demo1.Add(5, 5) << endl; retrun 0; }
.h文件中类名前的 template< class Type>几乎是固定的,
.cpp文件中也是,不过在类名后又多了< Type>.仔细看看。
相关文章推荐
- POJ3067 Japan(树状数组)
- iOS高级-QuartzCore框架-CALayer图层
- AVL树的实现
- OC中Blocks
- 欢迎使用CSDN-markdown编辑器
- Android.support.v7,v4,app还有design几个库的意义
- How to deploy the Cloudera Evaluation Cluster in Azure
- 方法的参数传递
- 适用于IOS的几种必须会用的工具
- scrollLeft,scrollTop,滚动代码的总结
- redis和mongodb工具类
- linux 设置 tmpfs 大小
- PE 439 Sum of sum of divisors | 51nod 1220 约数之和
- 【java基础】重载与重写
- Log日志+Toast+try/catch
- PackageInstaller 原理简述
- 程序员月入过万已经不再是神话
- 【java基础】重载与重写
- PAT 1078. Hashing (25)
- java 数组中的数据排序方法详解