东南大学:《泛型编程 Generic Programming》课程教学资源(PPT课件讲稿)Chapter 14 Templates

Chapter 14 Templates 0 2018, SEU. All rights reserved. 1
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 1 Templates Chapter 14

OBJECTIVES o To use function templates to conveniently create a group of related (overloaded) functions o To distinguish between function templates and function-template specializations o To use class templates to create a group of related types. To distinguish between class templates and class-template specializations. o To overload function templates To understand the relationships among templates, friends, inheritance and static members 0 2018, SEU. All rights reserved. 2
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 2 OBJECTIVES To use function templates to conveniently create a group of related (overloaded) functions. To distinguish between function templates and function-template specializations. To use class templates to create a group of related types. To distinguish between class templates and class-template specializations. To overload function templates. To understand the relationships among templates, friends, inheritance and static members

Topics o 14.1 Introduction o 14.2 Function Templates o 14.3 Overloading Function Templates o 14.4 Class Templates o 14.5 Nontype Parameters and Default Types for Class Templates o 14.6 Notes on Templates and Friends o 14.7 Notes on Templates and static Members 0 2018, SEU. All rights reserved. 3
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 3 Topics 14.1 Introduction 14.2 Function Templates 14.3 Overloading Function Templates 14.4 Class Templates 14.5 Nontype Parameters and Default Types for Class Templates 14.6 Notes on Templates and Friends 14.7 Notes on Templates and static Members

14.1 Introduction o Motivation 实现支持不同数据类型的取最大值函数 (例:支持int、 double、 Hugelnt等) 实现支持不同数据类型的Aray类(例: 支持int、 double、 NugeNt等) 0 2018, SEU. All rights reserved. 4
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 4 14.1 Introduction Motivation 实现支持不同数据类型的取最大值函数 (例:支持int、double、HugeInt等) 实现支持不同数据类型的Array类(例: 支持int、double、HugeInt等)

14.1 Introduction 模板( template):利用一种完全通用的方法来设 计函数或类而不必预先说明将被使用的每个对象 的类型,利用模板功能可以构造相关的函数或类 的系列,因此模板也可称为参数化的类型 泛型编程( Generic Programming °在C++语言中,模板可分为类模板( class template)和函数模板( (function template) 0 2018, SEU. All rights reserved. 5
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 5 14.1 Introduction 模板(template): 利用一种完全通用的方法来设 计函数或类而不必预先说明将被使用的每个对象 的类型,利用模板功能可以构造相关的函数或类 的系列,因此模板也可称为参数化的类型。—— 泛型编程(Generic Programming) 在 C++ 语 言 中 , 模 板 可 分 为 类 模 板 (class template)和函数模板(function template)

Topics o 14.1 Introduction o 14.2 Function Templates o 14.3 Overloading Function Templates o 14.4 Class Templates o 14.5 Nontype Parameters and Default Types for Class Templates o 14.6 Notes on Templates and Friends o 14.7 Notes on Templates and static Members 0 2018, SEU. All rights reserved. 6
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 6 Topics 14.1 Introduction 14.2 Function Templates 14.3 Overloading Function Templates 14.4 Class Templates 14.5 Nontype Parameters and Default Types for Class Templates 14.6 Notes on Templates and Friends 14.7 Notes on Templates and static Members

14.2 Function Templates review °定义模板: template关键字+尖括号<>括起来的 template parameter list(模板参数列表) °模板参数列表: 参数称为 I formal type parameter(形式类型参数)。 在函数调用时替换为基本数据类型或用户自定义数据 类型。 每个参数都必须以关键词 typename(或cass)起 头,参数和参数之间必须以逗号分隔,如 template typename T, typename V> 0 2018, SEU. All rights reserved. 7
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 7 14.2 Function Templates -- review 定义模板: template关键字+尖括号<>括起来的template parameter list(模板参数列表) 模板参数列表: 参数称为formal type parameter(形式类型参数)。 在函数调用时替换为基本数据类型或用户自定义数据 类型。 每个参数都必须以关键词typename (或class )起 头,参数和参数之间必须以逗号分隔,如 template

14.2 Function Templates revlew template typename T>//or template T maximum(t value 1, T value 2, T value3) T maximumValue= value 1: / assume value l is maximum n/ determine whether value2 is greater than maximumvalue if value2> maximum value) maximumValue value2 // determine whether value3 is greater than maximumvalue if ( value3>maximumValue maximumValue value3 return maximumvalue )//end function template maximum 0 2018, SEU. All rights reserved. 8
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 8 14.2 Function Templates -- review template // or template T maximum( T value1, T value2, T value3 ) { T maximumValue = value1; // assume value1 is maximum // determine whether value2 is greater than maximumValue if ( value2 > maximumValue ) maximumValue = value2; // determine whether value3 is greater than maximumValue if ( value3 > maximumValue ) maximumValue = value3; return maximumValue; } // end function template maximum

14.2 Function Templates revlew template∥ or template∥模板声明 T maximun( T value1, T value2, T value3)∥/函数模板定义 T maximumValue= value 1: / assume value l is maximum T指定函数返回值、形参以及局部变量的类型 determIne Whether value2 Is greater than maxImumvalue if value2> maximum value) maximumValue value2 // determine whether value3 is greater than maximumvalue if ( value3>maximumValue maximumValue value3 函数调用形式: return maximumvalue maximum(1, 2, 3); )//end function template maximum maximum(intl, int2, int3); 0 2018, SEU. All rights reserved. 9
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 9 14.2 Function Templates -- review template // or template //模板声明 T maximum( T value1, T value2, T value3 ) //函数模板定义 { T maximumValue = value1; // assume value1 is maximum // determine whether value2 is greater than maximumValue if ( value2 > maximumValue ) maximumValue = value2; // determine whether value3 is greater than maximumValue if ( value3 > maximumValue ) maximumValue = value3; return maximumValue; } // end function template maximum T指定函数返回值、形参以及局部变量的类型 函数调用形式: maximum (1, 2, 3); maximum (int1, int2, int3);

14.2 Function Templates 定义 o template o template< class ElementType r o template< typename BorderType, typename FileType cas; typename(关键字):.函数模板类型参数,任何内 部类型或用户自定义类型。 0 2018, SEU. All rights reserved. 10
© 2009, SEU. All rights reserved. © 2018, SEU. All rights reserved. 10 14.2 Function Templates -- 定义 template or template or template class,typename(关键字):函数模板类型参数,任何内 部类型或用户自定义类型
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 华中科技大学:《面向对象程序设计》课程PPT教学课件(Visual C++ 编程)第2讲 Visual C++ 6.0开发环境.ppt
- 《编译原理实践》课程教学资源(PPT讲稿)词法分析程序的自动生成器LEX.ppt
- 《Java语言程序设计》课程教学资源(PPT课件讲稿)第四章 Applet及其应用.ppt
- 《计算机组装与维修》课程教学资源(PPT讲稿)第7章 显示器.ppt
- 计算机问题求解(PPT讲稿)算法在计算机科学中的地位(算法的效率).pptx
- 西安电子科技大学:《Mobile Programming》课程PPT教学课件(Android Programming)Lecture 9 Service and Broadcast Receiver.pptx
- 泛型编程 Generic Programming(PPT讲稿)Templates.ppt
- 北京大学SAS俱乐部:SAS软件会员培训(PPT讲稿)SAS编程语言入门.ppt
- 中国科学技术大学:《数据结构及其算法》课程电子教案(PPT课件讲稿)第三章 栈和队列.pps
- 白城师范学院:《数据库系统概论 An Introduction to Database System》课程教学资源(PPT课件讲稿)第六章 关系数据理论.pptx
- 安徽广播影视职业技术学院:《ASP动态网页设计实用教程》课程教学资源(PPT讲稿)第1章 ASP基础(贾海陶).ppt
- 《MATLAB应用基础》课程教学资源(PPT课件讲稿)第4章 MATLAB的数值计算.ppt
- 《编译原理》课程教学资源(PPT课件讲稿)第七章 中间代码生成.ppt
- 上海交通大学:Basic Raster Graphics Algorithms for Drawing 2D Primitives.ppt
- Transport Layer Identification of P2P Traffic.ppt
- 复旦大学:《数据库基础与应用》课程PPT教学课件(Access案例教程)第1章 数据库基础知识.pptx
- 香港科技大学:Advanced Topics in NextGeneration Wireless Networks.ppt
- 《编译原理》课程教学资源(PPT课件讲稿)第五章 语法分析——自下而上分析.ppt
- 香港城市大学:Introduction to Real-Time Systems(Design and Analysis of Algorithms).pptx
- 《网站设计与建设 Website design and developments》课程教学资源(PPT课件讲稿)第一部分 Web基础知识 第3章 图形与Web设计.ppt
- Coded Caching under Arbitrary Popularity Distributions.pptx
- Distributed Systems and Networking Programmin(SOAP – Introduction).ppt
- 北京师范大学现代远程教育:《计算机应用基础》课程教学资源(PPT课件讲稿)第5章 Microsoft Excel 2010.pptx
- 图形处理及多媒体应用(PPT课件讲稿).pps
- Vitebi 译码.ppt
- 香港城市大学:Rank Aggregation in MetaSearch.ppt
- 《计算机网络技术》课程教学资源(PPT课件讲稿)第5章 广域网.ppt
- 中国科学技术大学:《现代密码学理论与实践》课程教学资源(PPT课件讲稿)第二部分 公钥密码和散列函数 第8章 数论入门(苗付友).pptx
- 中国科学技术大学:《计算机体系结构》课程教学资源(PPT课件讲稿)向量体系结构.pptx
- 南京大学:《面向对象技术 OOT》课程教学资源(PPT课件讲稿)面向对象的分析与设计简介 OOA & OOD:An introduction.ppt
- 北京师范大学现代远程教育:《计算机应用基础》课程教学资源(PPT课件讲稿)第1章 计算机常识(主讲:马秀麟).pptx
- 《并发控制技术》课程教学资源(PPT课件讲稿)第7章 事务管理 transaction management.ppt
- 山东大学软件学院:非线性规划(PPT讲稿)一维搜索方法.ppt
- 合肥工业大学:《计算机网络技术》课程教学资源(PPT课件讲稿)第4章 交换网的运行.ppt
- 长春工业大学:《网页设计与制作》课程教学资源(PPT课件)第5章 Div+CSS布局技术.ppt
- 《网络搜索和挖掘关键技术 Web Search and Mining》课程教学资源(PPT讲稿)Lecture 09 Evaluation.ppt
- 上海交通大学:《计算机图形学 Computer Graphics》课程教学资源(PPT讲稿)CHAPTER 4 THE VISUALIZATION PIPELINE.pptx
- 香港中文大学:XML for Interoperable Digital Video Library.ppt
- 中国医科大学计算机中心:《虚拟现实与增强现实技术概论》课程教学资源(PPT课件讲稿)第3章 虚拟现实系统的输出设备.pptx
- 同济大学:《大数据分析与数据挖掘 Big Data Analysis and Mining》课程教学资源(PPT课件讲稿)K-means & EM.pptx