当前位置:高等教育资讯网  >  中国高校课件下载中心  >  大学文库  >  浏览文档

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

资源类别:文库,文档格式:PPT,文档页数:95,文件大小:527.5KB,团购合买
点击下载完整版文档(PPT)

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 (function￾template 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();

点击下载完整版文档(PPT)VIP每日下载上限内不扣除下载券和下载次数;
按次数下载不扣除下载券;
24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
共95页,可试读20页,点击继续阅读 ↓↓
相关文档

关于我们|帮助中心|下载说明|相关软件|意见反馈|联系我们

Copyright © 2008-现在 cucdc.com 高等教育资讯网 版权所有