c++primer函数模板.ppt_第1页
c++primer函数模板.ppt_第2页
c++primer函数模板.ppt_第3页
c++primer函数模板.ppt_第4页
c++primer函数模板.ppt_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1 第六章模板 6 1模板的概念6 2函数模板与模板函数6 3类模板和模板类 2 模板是面向对象多态性的一种表现 1990年ANSIC 委员会将模板确定为组成部分 BorlandC 3 0以上和VisualC 都支持模板 模板包括函数模板和类模板 一 为什么要引入模板 模板的引入一个最重要的目的就是简化编程 由于C 是强类型语言 许多类似功能 函数或类 只要数据类型不同 就必须定义多份 不但使源程序增长 工作量也加大 3 例如 设计一个求两参数最大值的函数 不使用模板时 需要定义四个函数 intmax inta intb return a b a b floatmax floata floatb return a b a b doublemax doublea doubleb return a b a b charmax chara charb return a b a b 这些函数的功能甚至语句都类似 能否只写一套代码 使用与多种数据类型呢 6 1模板的概念 4 6 1模板的概念 解决的答案就是模板 通过类型参数化来实现代码重用 即把数据类型定义为参数 使用时才给出具体类型来实例化类型参数 模板分为函数模板 functiontemplate 和类模板 classtemplate 5 模板 模板函数 模板类 对象 实例化 实例化 图6 1模板 模板类 模板函数和对象之间的关系 实例化 6 1模板的概念 6 所谓函数模板 就是写一个函数模子 用这个模子套印出许多功能相同 参数类型和返回类型不同的函数 模板函数 函数模板实例化后的具体函数 6 2函数模板与模板函数 7 6 2 1函数模板的声明与模板函数的生成函数模板的声明格式如下 template返回类型函数名 模板形参表 函数体 type是模板形参 在使用函数模板时 必须将其实例化 即用实际的数据类型替代它 6 2函数模板与模板函数 8 函数max 若使用模板 则只定义一个函数 templatetypemax typea typeb return a b a b 函数模板定义不是一个实实在在的函数 编译系统不为其产生任何执行代码 该定义只是对函数的描述 表示它每次能单独处理在类型形式参数表中说明的数据类型 6 2函数模板与模板函数 9 例6 1 includetemplate max Tx Ty return x y x y main inti1 10 i2 56 floatf1 12 5 f2 24 5 doubled1 50 344 d2 4656 546 charc1 k c2 n 6 2函数模板与模板函数 10 cout themaxofi1 i2 max i1 i2 endl cout themaxoff1 f2 max f1 f2 endl cout themaxofd1 d2 max d1 d2 endl cout themaxofc1 c2 max c1 c2 endl return0 intmax intx inty return x y x y floatmax floatx floaty return x y x y doublemax doublex doubley return x y x y charmax charx chary return x y x y 结果 themaxofi1 i2 56themaxoff1 f2 24 5themaxofd1 d2 4656 546themaxofc1 c2 n 11 函数模板max Tx Ty 模板函数1max i1 i2 模板函数2max f1 f2 模板函数3max d1 d2 模板函数4max c1 c2 函数模板 一组函数的抽象模板函数 一个具体的函数代码重用 提高了程序设计的效率 12 例6 2 includetemplateTsum T array intsize 0 Ttotal 0 for inti 0 i size i total array i returntotal intint array 1 2 3 10 doubledouble array 1 1 2 2 9 9 10 10 voidmain intitotal sum int array 10 doubledtotal sum double array 10 cout Thesumofintarrayare itotal endl cout Thesumofdoublearrayare dtotal endl 结果 Thesumofintarrayare 55Thesumofdoublearrayare 59 6 13 说明 1 在函数模板中允许用多个类型参数 注意定义形式 每个模板形参前必须加关键字class 例 templatevoidmyfun T1x T2y coutmyfun int char myfun 0 12345 10L myfun double longint 2 在模板template语句和函数模板定义之间不能有别的语句 6 2函数模板与模板函数 14 6 2 2函数模板的异常处理 实例化T的各模板实参之间必须保持完全一致的类型 否则会发生错误 template max Tx Ty return x y x y voidfun inti charc max i i okmax c c okmax i c error无法匹配max c i error 模板没有隐含的类型转换功能 15 1 采用强制类型转换max i int c 2 重载函数模板template max Tx Ty return x y x y intmax int int 只声明原型voidfun inti charc max i i okcallmax int int max c c okcallmax char char max i c okcallmax int int max c i okcallmax int int 支持隐式类型转换 16 定义一个完整的非模板函数 include includetemplateTmax Tx Ty return x y x y char max char x char y return strcmp x y 0 x y voidmain cout Max Hello Gold is max Hello Gold endl 结果 Max Hello Gold isHello该例中 调用的是非模板函数char max char char 17 C 中函数调用的一般顺序为 1 寻找一个参数完全匹配的函数 若找到则调用之 否则 2 寻找一个函数模板 若找到则将其实例化为一个模板函数 然后调用之 否则 3 寻找重载函数 考察有无可通过类型转换产生参数匹配的函数 若有则调用之 先配外部 6 2 2函数模板的异常处理 18 类模板 也称为类属类或类生成类 是 允许用户为类定义一种模式 使得类中的某些数据成员 某些成员函数的参数或者返回值 能取任意数据类型 templateclass 类说明体 模板类 类模板实例化成一个具体的类 类名对象名 6 3类模板与模板类 19 例 堆栈类模板constintsize 10 templateclassStack Tstk size 堆栈元素类型可变inttop 记录元素个数 int型不变public Stack top 0 创建voidpush Tob 元素入栈Tpop 元素出栈 20 类体外成员函数定义格式 template返回类型类名 函数名 参数表 函数体 在类体外定义成员函数时 若其中用到模板形参 则需要在函数体前进行模板声明 函数名前的类名之后也要缀上 21 例 templatevoidStack push Tob if top size cout Stackfull return else stk top ob top templatevoidStack pop if top 0 cout Stackempty return top returnstk top 22 例 堆栈类模板使用intmain Stacks1 s2 Stacks1 s2 s1 push a s1 push b s1 push c s2 push x s2 push y s2 push z cout pops1 for inti 0 i 3 i cout s1 pop cout pops2 for inti 0 i 3 i cout s2 pop return0 结果 pops1 cbapops2 zyx 23 类模板Stack 模板类1Stack 模板类2Stack 模板类3Stack 24 使用类模板使用户可以为类定义一种模式 使得类中的某些数据成员 某些成员函数的参数 某些成员函数的返回值 能取任意类型 包括系统预定义的和用户自定义的 25 实例 数组排序 函数模板 includetemplatevoidsort T array intlen Ttemp for inti 0 i len 1 i for intj i 1 j len j if array i array j temp array i array i array j array j temp templatevoidshow T array intlen for inti 0 i len i cout array i cout endl 26 intmain inttest1 21 23 13 42 58 floattest2 102 3 45 6 763 29 show test1 5 sort test1 5 show test1 5 show test2 3 sort test2 3 show test2 3 return0 21231342585842232113102 345 6763 29763 29102 345 6 27 includetemplateclassarray Tcontent 100 intlen public array T int voidsort voidshow 实例 数组排序 类模板 templatearray array T t intl for inti 0 i l i content i t i len l 28 templatevoidarray sort Ttemp for inti 0 i len 1 i for intj i 1 j len j if content i content j temp content i content i content j content j temp templatevoidarray show for inti 0 i len i cout content i cout endl 29 intmain inttest1 21 23 13 42 58 floattest2 102 3 45 6 763 29 arraya1 test1 5 arraya2 test2 3 a1 show

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论