泛型编程 Generic Programming(PPT讲稿)Templates

Generic Programming Templates
Generic Programming: Templates

Reusability and genericity Major theme in development of programming languages Reuse code to avoid repeatedly reinventing the wheel Trend contributing to this Use of generic code Can be used with different types of data Function and class templates Enable programmers to specify an entire range of related functions and related classes Generic programming
Reusability and Genericity ▪ Major theme in development of programming languages • Reuse code to avoid repeatedly reinventing the wheel ▪ Trend contributing to this • Use of generic code • Can be used with different types of data ▪ Function and class templates • Enable programmers to specify an entire range of related functions and related classes • → Generic programming 2

Function Templates (parameterized functions)
Function Templates (parameterized functions)

Motivation Initially code was reusable by encapsulating it within functions Example Write void swap (int& first, int& second) int temp= firsti first second second =temp; Then call swap(x,y);
Motivation ▪ Initially code was reusable by encapsulating it within functions ▪ Example: • Write • Then call swap(x,y); void swap (int& first, int& second) { int temp = first; first = second; second = temp; }

To swap variables of different types, write another function Overloading allows functions to have same name Signature(types and numbers of parameters) keep them unique to the compiler This could lead to a library of swap functions One function for each standard/primitive type Compiler chooses which to use from signature void swap (int& first, int& second) int temp firsti first second: second temp void swap (double& first, double& second double temp first first second void swap (char& first, char& second second temp first second second temp; But . what about swapping user defined types such as an object? We cannot cover the swap function for ALL possible class objects
▪ To swap variables of different types, write another function • Overloading allows functions to have same name • Signature (types and numbers of parameters) keep them unique to the compiler ▪ This could lead to a library of swap functions • One function for each standard/primitive type • Compiler chooses which to use from signature ▪ But … what about swapping user defined types such as an object? • We cannot cover the swap function for ALL possible class objects void swap (int& first, int& second) { int temp = first; first = second; second = temp; } void swap (double& first, double& second) { double temp = first; first = second; second = temp; } void swap (char& first, char& second) { char temp = first; first = second; second = temp; }

Passing Types (Instead of Fixed Types) void swap (T& first, T& second) T temp- first first second second temp i
void swap (T& first, T& second) { T temp = first; first = second; second = temp; } Passing Types (Instead of Fixed Types)

Using function overloading, note how similar each of the swap functions would be The three places where the type is specified What if we passed the type somehow? Templates make this possible Declare functions that receive both data and types via parameter Thus code becomes more generic Easier to reuse and extend to other types
▪ Using function overloading, note how similar each of the swap functions would be • The three places where the type is specified ▪ What if we passed the type somehow? ▪ Templates make this possible • Declare functions that receive both data and types via parameter ▪ Thus code becomes more generic • Easier to reuse and extend to other types

Function Templates Produce overloaded functions that perform identical operations/algorithms on different types of data Programmer writes a single function-template definition Compiler generates separate object-code functions(function template specializations) based on argument types in calls to the function template Generate and compile declaration or definition? compiler does more things
Function Templates Produce overloaded functions that perform identical operations/algorithms on different types of data • Programmer writes a single function-template definition • Compiler generates separate object-code functions (functiontemplate specializations) based on argument types in calls to the function template Generate and compile, declaration or definition? compiler does more things …

General Form of Template template Function Definition with the parameter type T T is a type-parameter(placeholder)naming the"generic"type of value(s)on which the function operates Function Definition is the definition of the function, using type T Can use 'class'or 'typename, so it is a type name, so 'typename'is better than the old'class
General Form of Template template Function Definition with the parameter type T … Can use ‘class’ or ‘typename’, so it is a type name, so ‘typename’ is better than the old ‘class’! ▪ T is a type-parameter (placeholder) naming the "generic" type of value(s) on which the function operates ▪ Function Definition is the definition of the function, using type T

template(a, b) swap(x, y)i// swap(x, y) swap(m, n)i// swap(m, n)i
template void swap (T& first, T& second) { T temp = first; first = second; second = temp; } main() { int a,b; double x,y; char m,n; swap(a,b); // swap(a,b); swap(x,y); // swap(x,y); swap(m,n); // swap(m,n); }
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 北京大学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
- 《汇编语言》课程PPT教学课件:第三章 80x86寻址方式和指令系统.ppt
- 清华大学:高校信息门户建设(PPT讲稿).ppt
- 《计算机辅助设计 Computer Aided Design》课程PPT教学课件:第一篇 CAD技术 第一章 几何造型方法介绍和分类.ppt
- 西安电子科技大学:《操作系统 Operating Systems》课程教学资源(PPT课件讲稿)Chapter 02 进程和线程 Processes and Threads.ppt
- 《数字图像处理 Digital Image Processing》课程教学资源(PPT课件讲稿)第2章 图像的基本知识及运算.ppt
- 江苏海洋大学(淮海工学院):《Java面向对象程序设计》课程教学资源(PPT课件讲稿)第3章 Java 面向对象编程 3.1 面向对象软件开发概述.pptx
- 利用NetRiver实验系统实现IP协议交互和TCP协议交互.ppt
- 西安电子科技大学:《Mobile Programming》课程PPT教学课件(Android Programming)Lecture 9 Service and Broadcast Receiver.pptx
- 计算机问题求解(PPT讲稿)算法在计算机科学中的地位(算法的效率).pptx
- 《计算机组装与维修》课程教学资源(PPT讲稿)第7章 显示器.ppt
- 《Java语言程序设计》课程教学资源(PPT课件讲稿)第四章 Applet及其应用.ppt
- 《编译原理实践》课程教学资源(PPT讲稿)词法分析程序的自动生成器LEX.ppt
- 华中科技大学:《面向对象程序设计》课程PPT教学课件(Visual C++ 编程)第2讲 Visual C++ 6.0开发环境.ppt
- 东南大学:《泛型编程 Generic Programming》课程教学资源(PPT课件讲稿)Chapter 14 Templates.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