Introduction to C++ Templates and Exceptions.ppt
《Introduction to C++ Templates and Exceptions.ppt》由会员分享,可在线阅读,更多相关《Introduction to C++ Templates and Exceptions.ppt(23页珍藏版)》请在麦多课文档分享上搜索。
1、Introduction to C+ Templates and Exceptions,C+ Function Templates C+ Class Templates,C+ Function Templates,Approaches for functions that implement identical tasks for different data types Nave Approach Function Overloading Function Template Instantiating a Function Templates,Approach 1: Nave Approac
2、h,create unique functions with unique names for each combination of data types difficult to keeping track of multiple function names lead to programming errors,Example,void PrintInt( int n ) cout “*Debug“ endl;cout “Value is “ n endl; void PrintChar( char ch ) cout “*Debug“ endl;cout “Value is “ ch
3、endl; void PrintFloat( float x ) void PrintDouble( double d ) ,PrintInt(sum);PrintChar(initial);PrintFloat(angle);,To output the traced values, we insert:,Approach 2:Function Overloading (Review),The use of the same name for different C+ functions, distinguished from each other by their parameter li
4、sts,Eliminates need to come up with many different names for identical tasks.Reduces the chance of unexpected results caused by using the wrong function name.,Example of Function Overloading,void Print( int n ) cout “*Debug“ endl;cout “Value is “ n endl; void Print( char ch ) cout “*Debug“ endl;cout
5、 “Value is “ ch endl; void Print( float x ) ,Print(someInt); Print(someChar); Print(someFloat);,To output the traced values, we insert:,Approach 3: Function Template,A C+ language construct that allows the compiler to generate multiple versions of a function by allowing parameterized data types.,Tem
6、plate FunctionDefinition,FunctionTemplate,TemplateParamDeclaration: placeholder,class typeIdentifier typename variableIdentifier,Example of a Function Template,templatevoid Print( SomeType val ) cout “*Debug“ endl;cout “Value is “ val endl; ,Print(sum); Print(initial); Print(angle);,To output the tr
7、aced values, we insert:,Template parameter (class, user defined type, built-in types),Template argument,Instantiating a Function Template,When the compiler instantiates a template, it substitutes the template argument for the template parameter throughout the function template.,Function (FunctionArg
8、List),TemplateFunction Call,A more complex example,template void sort(vector ,Summary of Three Approaches,Nave Approach Different Function Definitions Different Function Names,Function Overloading Different Function Definitions Same Function Name,Template Functions One Function Definition (a functio
9、n template) Compiler Generates Individual Functions,Class Template,A C+ language construct that allows the compiler to generate multiple versions of a class by allowing parameterized data types.,Template ClassDefinition,Class Template,TemplateParamDeclaration: placeholder,class typeIdentifier typena
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- INTRODUCTIONTOCTEMPLATESANDEXCEPTIONSPPT
