




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、2020年6月28日,1,第九章 Templates,2020年6月28日,2,一、模板函数的定义,模板函数即是利用模板的概念来编写函数,让函数能够以相同的代码程序处理不同的数据类型。 定义格式如下: templateType fun(Type, Type,) 该定义格式有两部分内容: template /template的定义部分 关键字template表示:所定义的函数为模板函数; 关键字class表示:模板函数接受的数据类型可以是任何数据类型,包括用户定义的数据类型(不是类的概念); Type:template的形式参数,表示模板函数的形式数据类型; class Type 为关键字tem
2、plate的参数行。,2020年6月28日,3,一、模板函数的定义, Type fun(Type, Type,) /函数定义部分 Type:为经模板定义后形式数据类型,真正的数据类型含义取决于调用模板函数实参的数据类型。 编译器决定Type的依据是当调用函数时所给出的实在参数而非函数的返回值类型。,2020年6月28日,4,Prog9_1.cpp,templateX max(X var1,X var2) return(var1var2)? var1:var2; void main() int m1=max(100,200); double m2=max(35.5,65.6); char *m3
3、=max(Micro, Soft); cout The maxium of 100 and 200 is: m1endl; cout The maxium of 35.5 and 65.6 is: m2endl; cout The maxium of Micro and Soft is: m3endl; getch(); ,运算结果: The maxium of 100 and 200 is:200 The maxium of 35.5 and 65.6 is:65.6 The maxium of Micro and Soft is:Soft,2020年6月28日,5,Prog8_2.cpp,
4、template T sum(T *array, int size=0) T total=0; for(int i=0; isize;i+) total+=arrayi; return total; int intarray=1,2,3,4,5,6,7,8,9,10; double douarray=1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.0; int itotal=sum(intarray,10); double dtotal=sum(douarray,10); void main() cout The summary of integer arry a
5、re: itotalendl; cout The summary of double floating array are: dtotalendl; ,运行结果: The summary of integer arry are:55 The summary of double floating array are:59.5,2020年6月28日,6,二、template函数的重载,利用template函数的参数表不同便可以实现模板函数的重载。Overloding template不仅可以解决参数数据类型不同的问题,也解决了参数个数不同的问题。 如: templateType add(Type
6、a, Type b) templateType add(Type a, int b) templateType *add(Type a, char *b),2020年6月28日,7,Prog9_3.cpp,templateT sum(T *array,int size=0) static T total; for(int i=0;iT sum(T *array, T *array2, int size=0) static T total; for(int i=0; isize;i+) total+=arrayi+array2i; return total; ,2020年6月28日,8,int
7、intarr1=1,2,3,4,5,6,7,8,9,10; int intarr2=2,4,6,8,10,12,14,16; double douarr=1.1,2.2,3.3,4.4,5.5,6.6,7.7,8.8,9.9,10.0; int main(int argc, char* argv) int itotal=sum(intarr1,10); double dtotal=sum(douarr,10); int iatotal=sum(intarr1,intarr2,8); coutitotal= itotalendl; coutdtotal= dtotalendl; coutiato
8、tal= iatotalvar2)? var1:var2; char *m3=max(Micro, Soft); cout The maxium of Micro and Soft is: m3var2)? var1:var2; char *max(char *str1, char * str2) return(strcmp(str1,str2)=0)? str1:str2; ,2020年6月28日,12,四、多重类型参数,函数定义格式: template Type1 fun(Type1 a, Type2 b) 说明: Class关键字不能省; 每一个已定义的类型参数都必须至少被使用一次。 如
9、 int m1=max(100,35.50); int m2=max(A,30);,2020年6月28日,13,五、Template类,定义格式: templateclass newclass 定义成员函数时,必须以关键字template开始,然后接类型参数列表。 定义在类体为时,必须以范围运算符指明其所属范围。 必须加上类型参数以分辩成员函数将来是属于此template类的那一个类型。 定义模板类对象的格式: Newclass newclass_object; Type 为用户指定的数据类型。,2020年6月28日,14,如:,template class A protected: T y;
10、 public: A(T x)y=x; void show() cout“y= “y endl; ,void main() A aa(10); aa.show(); char cc=a; A bb(cc); bb.show(); ,2020年6月28日,15,Prog9_5.cpp,const int AS=20; templateclass Array private: Type *element; int size; public: Array(const int size); Array(); Type ,templateinline Array:Array() delete eleme
11、nt; template Type ,2020年6月28日,16,Prog9_5.cpp,void main() Array iobj(AS); Array dobj(AS); Array cobj(AS); cout.flags(ios:showpoint); for(int i=0;iAS;i+) iobji=i; dobji=i; cobj=c; for(i=0;iAS;i+) coutsetw(3)iobji setw(15)dobji cobji endl; ,2020年6月28日,17,运行结果: 0 0.000000 c 1 1.00000 c 2 2.00000 c 3 3.0
12、0000 c 4 4.00000 c 5 5.00000 c 6 6.00000 c 7 7.00000 c 8 8.00000 c 9 9.00000 c 10 10.0000 c 11 11.0000 c 12 12.0000 c 13 13.0000 c 14 14.0000 c 15 15.0000 c 16 16.0000 c 17 17.0000 c 18 18.0000 c 19 19.0000 c,2020年6月28日,18,六、template类的friend函数,template类的friend函数有三种: 1. 一般friend函数 如 friend void displ
13、ay(const int size); 2. 第一种template friend函数 Templateclass Array friend void display(const Array ,2020年6月28日,19,六、template类的friend函数,3. 第二种template friend函数 templateclass Array template friend void display(Array ,2020年6月28日,20,Prog9_7.cpp,templateclass Array friend ostream ,2020年6月28日,21,Prog9_7.cpp,
14、template Array:Array(const int s) size=s; element=new Tsize; for(int i=0;i Array:Array() delete eelement; template T ,template void Array:operator =(T temp) for(int i=0;i ostream ,2020年6月28日,22,const int AS=5; void main() Array iobj(AS); Array dobj(AS); Array cobj(AS); cout.flags(ios:showpoint); iob
15、j=100; dobj=3.14159; cobj=C; cout Integer: iobjendl Double: dobjendl Character: cobjendl; ,2020年6月28日,23,运行结果:,Integer: 100 100 100 100 100 Double: 3.14159 3.14159 3.14159 3.14159 3.14159 Character: C C C C C,2020年6月28日,24,七、模板继承,模板与继承相结合,将使C的功能变的很强大。 如定义模板类为:,template void A:show() cout“y= “y endl; ,template class A public: T y
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 3884.4-2025铜精矿化学分析方法第4部分:铅、锌、镉、镍和氧化镁含量的测定火焰原子吸收光谱法
- 2025江苏连云港市灌云县招聘社区工作者20人备考考试题库附答案解析
- 2025年彭州市教育系统“蓉漂人才荟”赴外公招(32人)笔试备考题库及答案解析
- 2025福建海峡人力资源股份有限公司平潭分公司第六批招聘1人备考考试题库附答案解析
- 2025四川成都市第八人民医院下半年编外招聘52人备考考试题库附答案解析
- 2025年宣城绩溪县开源建设投资发展有限公司公开招聘7名备考考试题库附答案解析
- 2025重庆武隆区事业单位考核招聘21人备考考试题库附答案解析
- 2025广东工业大学管理学院国际合作与认证中心招聘1人备考考试题库附答案解析
- 掌握游戏行业新角色
- 静脉血栓风险评估
- 2025年东风校招测评题库及答案
- 怎样合理减肥健康教育
- 音乐核心素养培训课件
- 企业营销管理培训课件
- 蘑菇中毒中医处理
- 重庆机电职业技术大学《高等数学Ⅱ》2025-2026学年期末试卷(A卷)
- 有奖竞猜题目及答案有趣
- 骨科引流管护理
- 脑梗死的中医护理查房
- 绿色SoC设计的架构探索与优化-洞察阐释
- 客运包车牌管理制度
评论
0/150
提交评论