Generic Programming(PPT课件讲稿)Templates and Overloading

Generic Programming Templates and Overloading
Generic Programming: Templates and Overloading

Evolution of reusability and G emeril 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
Evolution of 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 COMP152 2

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

Motivation Initially code was reusable by encapsulating it within functions Example Write void swap (int& firstr int& second) int temp= firsti first second second =temp; Then call swap(x,y); COMP 152
Motivation ▪ Initially code was reusable by encapsulating it within functions ▪ Example: • Write • Then call swap(x,y); COMP152 4 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 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 firsti first second void swap (double& first, double& second) secon d temp double temp firsti first second void swap (char& first, char& second) second temp; first second second temp; COMP 152
▪ 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 COMP152 5 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 COMP 152
COMP152 6 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 COMP 152
▪ 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 COMP152 7

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 COMP 152
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 COMP152 8

Function Templates More compact and convenient form of overloading Identical program logic and operations algorithms for each data type Function template definition Written by programmer once Essentially defines a whole family of overloaded functions Begins with the template keyword Contains template parameter list of formal type parameters for the function template enclosed in angle brackets(>) Formal type parameters a Preceded by keyword typename or keyword class u Placeholders for fundamental types or user-defined types COMP 152
Function Templates ▪ More compact and convenient form of overloading • Identical program logic and operations/algorithms for each data type ▪ Function template definition • Written by programmer once • Essentially defines a whole family of overloaded functions • Begins with the template keyword • Contains template parameter list of formal type parameters for the function template enclosed in angle brackets (<>) • Formal type parameters ❑Preceded by keyword typename or keyword class ❑Placeholders for fundamental types or user-defined types COMP152 9

Writing Template A function template is a pattern constructed based on given actual types type parameter said to be"bound"to the actual type passed to it Calling a function with template type inside the function template void fo i T f(); COMP 152
Writing Template ▪ A function template is a pattern • constructed based on given actual types • type parameter said to be "bound" to the actual type passed to it ▪ Calling a function with template type inside the function COMP152 10 template void f() { T a; … } f();
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 西安电子科技大学:《操作系统 Operating Systems》课程教学资源(PPT课件讲稿)Chapter 01 Introduction(主讲:高海昌).ppt
- 四川大学:《数据结构》课程教学资源(PPT课件讲稿)第七章 查找 Search.ppt
- 西安电子科技大学:《现代操作系统》课程PPT教学课件(讲稿)作业管理 Job Management.ppt
- 《多媒体技术》课程教学资源(PPT课件讲稿).ppt
- 南京航空航天大学:《数据结构》课程教学资源(PPT课件讲稿)第二章 线性表.ppt
- 《计算机文化基础》课程教学课件(PPT课件讲稿)第一章 信息技术与计算机文化.ppt
- 江苏大学:《面向对象建模技术》课程教学资源(PPT课件讲稿)第1章 UML与面向对象(主讲:林琳).ppt
- 《数据结构》课程教学资源(PPT课件讲稿)第五章 树及二叉树.ppt
- 《网站设计与建设》课程PPT教学课件(Website design and developments)第二部分 网站规划 第9章 软件平台规划.ppt
- 《数据库原理与应用》课程教学资源(PPT课件讲稿)第2章 关系数据库数学模型.ppt
- 《计算机网络》课程电子教案(PPT教学课件讲稿,共十章).ppt
- 西华大学:《电子商务概论》课程教学资源(PPT课件讲稿)第3章 电子商务的技术基础.ppt
- 中国科学技术大学:《高级操作系统 Advanced Operating System》课程教学资源(PPT课件讲稿)分布式系统的同步(3.3-3.5).ppt
- 西安交通大学:《微机原理与接口技术》课程教学资源(PPT课件讲稿)第1章 微机系统概论(2013).ppt
- 西安交通大学:《微机原理与接口技术》课程教学资源(PPT课件讲稿)复习纲要(主讲:桂小林).ppt
- 厦门大学:《大数据技术原理与应用》课程教学资源(PPT课件讲稿,2017)第6章 云数据库.ppt
- 《Java程序开发》课程教学资源(PPT课件讲稿)第4章 面向对象(基础篇).ppt
- 管理Windows 2000 Server服务器(PPT课件讲稿).ppt
- 《信息安全概论》课程教学资源(PPT课件讲稿)第8章 操作系统安全.ppt
- 电子科技大学:《计算机操作系统》课程教学资源(PPT课件讲稿)第二章 进程与调度(Processes and Scheduling)Section III.ppt
- 徐州师范大学:《电子商务 Electronic Business》课程教学资源(PPT课件讲稿)电子商务安全实验、数字证书应用.ppt
- 信息化技术中心:网络安全意识培训(PPT讲稿).pptx
- 《微型计算机原理及应用》课程教学资源(PPT课件讲稿)第6章 输入输出与中断.ppt
- 大庆职业学院:《计算机网络技术基础》课程电子教案(PPT教学课件)第3章 网络体系结构与协议.ppt
- 《数据结构 Data Structure》课程教学资源(PPT课件讲稿)第二章 线性表.ppt
- 北京大学:网络搜索引擎原理(PPT讲稿)Web Graph & Link Analysis.ppt
- 西安电子科技大学:《现代密码学》课程教学资源(PPT课件讲稿)第七章 密码协议.pptx
- 上海交通大学:Network Coding for Wireless Networks(PPT讲稿).pptx
- 并行算法 Parallel Algorithms(PPT讲稿)现状与展望 status and prospects.ppt
- 《高级程序语言》课程教学资源(PPT课件讲稿)第09章 平台无关语言.ppt
- Phase Change Memory Aware Data Management and Application.pptx
- 合肥工业大学:《数据库系统概论》课程教学资源(PPT课件)第四章 并发控制.ppt
- 中国科学技术大学:《Linux操作系统分析》课程教学资源(PPT课件讲稿)Linux的进程(1/3).ppt
- 《计算机网络》课程教学大纲 Computer Networks.pdf
- 南京大学:模型检测(PPT课件讲稿)Model Checking.pptx
- 电子科技大学:《计算机操作系统》课程教学资源(PPT课件讲稿)第四章 设备管理 Device Management and Disk Scheduling.ppt
- 湖南生物机电职业技术学院:《电子商务概论》课程教学资源(PPT课件)第八章 电子商务安全.ppt
- 《操作系统》课程PPT教学课件(英文)内存管理 Memory Management.ppt
- 上海交通大学:IT项目管理(PPT讲稿)讲座6 软件项目工作量估算.ppt
- 四川大学:《数据库技术》课程教学资源(PPT课件讲稿)第9章 数据库系统开发工具VB.ppt