类模板(3种)
来源:互联网 发布:淘宝上美的官方旗舰店 编辑:程序博客网 时间:2024/06/17 23:36
很多初学Cpp的人不懂类模板,在此,我举了3个简单的例子,帮助大家了解语法和基本流程
类模板1:
#include<iostream>using namespace std;template<class T>//相当于template<typename/class numtype(类型)>class Compare{ public: Compare(T a, T b); T max(); T min(); private: T x, y;};template<class T>Compare<T> :: Compare(T a,T b) { x = a; y = b; } template<class T> T Compare<T> :: max() { return (x > y) ? x : y; } template<class T> T Compare<T> :: min() { return (x < y) ? x : y; }int main(){ Compare<int>cmp1(3, 7);//这里的Compare不是类名了,而是类的模板名,注意调用的形式,中间加上虚拟类型,跟以往的不一样 cout<<cmp1.max()<<" is the maxnum between a and b"<<endl; cout<<cmp1.min()<<" is the minnum between a and b"<<endl; Compare<float>cmp2(6.4, 8.5); cout<<cmp2.max()<<" is the maxnum between a and b"<<endl; cout<<cmp2.min()<<" is the minnum between a and b"<<endl; system("pause"); return 0;}类模板2:
#include<iostream>#include<cstdlib>using namespace std;template<class T>class Array{ private: T*set; int n; public: Array(T *data, int i){set = data; n = i;} void sort(); T sum(); void display();};template<class T>void Array<T> :: sort(){ int i,j; T temp; for(i = 0;i < n - 1; i++) for(j = 0;j < n - 1 - i; j++) if(set[j] > set[j+1])//指针名[i]即元素,如p[i]等同于*(p+i)以及*(a+i),不需加星号了 { temp = set[j]; set[j] = set[j+1]; set[j+1] = temp; }}template<class T>T Array<T> :: sum(){ int i;T s = 0;for(i = 0;i < n;i++) s + =set[i];return s;}template<class T>void Array<T> :: display(){ int i; for(i = 0;i < n;i++) cout<<"数组的第"<<i+1<<"个元素为:"<<set[i]<<endl;}int main(){ int a[10] = {9,8,7,1,2,3,4,6,5,10}; float b[9] = {1.2,2.3,4.6,3.2,2.1,9.8,2.4,9.1,10.1}; Array<int>a1(a,10); Array<float>b1(b,9); cout<<"a数组的原始序列为:"<<endl; a1.display(); a1.sort(); cout<<"a数组的排序后序列为:"<<endl; a1.display(); cout<<"a数组元素之和为:"<<a1.sum()<<endl; cout<<"---------------------------"<<endl; cout<<"b数组的原始序列为:"<<endl; b1.display(); b1.sort(); cout<<"b数组的排序后序列为:"<<endl; b1.display(); cout<<"b数组元素之和为:"<<b1.sum()<<endl; system("pause"); return 0;}类模板3:
#include<iostream>#include<cstdlib>using namespace std;template<class T,int N>//T为类型,N为数值,类模板里面包含多个参数 class A{ private: T a[N]; public: A();//无参构造函数对应无参对象 void show(); void sh();};template<class T,int N>A<T,N> :: A(){ for(int i = 0;i < N; i++) { cout<<"input a["<<i<<"]:"; cin>>a[i]; }}template<class T, int N>void A<T, N> :: show(){ cout<<"N = "<<N<<endl;}template<class T, int N>void A<T, N> :: sh(){for(int i = 0;i < N;i++) cout<<"a["<<i<<"] = "<<a[i]<<endl;}int main(){ A<float, 5>a;//直接定义对象,对应构造函数,不需要再为蛇添足,使用对象调用构造函数 a.show(); a.sh(); system("pause"); return 0;}
- 类模板(3种)
- C++模板(类模板、函数模板)
- 模板之类模板(数组类模板)
- c++模板 (函数模板和类模板)
- C++中的模板(函数模板,类模板)
- Linux C/C++ 模板:类模板(主模板)
- C++模板(函数模板和类模板)
- 类模板与模板类(C++)
- c++模板(二)---------类模板
- gauss 3种模板
- 模板类3
- (十八)类模板
- 类模板(转)
- 模板 (类、函数)
- 类模板(一)
- 二叉排序树(模板类)
- 类模板(二)
- 模板(函数模板和类模板基本类似,类模板的定义需要注意)
- 数据结构的排序算法(直接插入)
- MongoDB在windows下的安装配置
- 面试题之interleave字符串分析
- Android源码编译整理总结
- 在 Sandboxed Mac App 中嵌入第三方可执行文件
- 类模板(3种)
- LK60xWin.dll LK-607 读卡器
- java读取txt文档到string中
- 基于Ant+Selenium+TestNG的测试框架配置
- Weblogic下的应用程序,客户端打印时提示权限不够
- Linux安装程序Anaconda分析
- 互联网公司笔试面试准备以及注意事项
- java基础--映射表Map
- SQL SERVER 取本月上月日期