欢迎来到麦多课文档分享! | 帮助中心 海量文档,免费浏览,给你所需,享你所想!
麦多课文档分享
全部分类
  • 标准规范>
  • 教学课件>
  • 考试资料>
  • 办公文档>
  • 学术论文>
  • 行业资料>
  • 易语言源码>
  • ImageVerifierCode 换一换
    首页 麦多课文档分享 > 资源分类 > PPT文档下载
    分享到微信 分享到微博 分享到QQ空间

    Introduction to C++ Templates and Exceptions.ppt

    • 资源ID:376634       资源大小:120KB        全文页数:23页
    • 资源格式: PPT        下载积分:2000积分
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    二维码
    微信扫一扫登录
    下载资源需要2000积分(如需开发票,请勿充值!)
    邮箱/手机:
    温馨提示:
    如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如需开发票,请勿充值!如填写123,账号就是123,密码也是123。
    支付方式: 支付宝扫码支付    微信扫码支付   
    验证码:   换一换

    加入VIP,交流精品资源
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    Introduction to C++ Templates and Exceptions.ppt

    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

    10、me variableIdentifier,Example of a Class Template,template class GList public:bool IsEmpty() const;bool IsFull() const;int Length() const;void Insert( /* in */ ItemType item );void Delete( /* in */ ItemType item );bool IsPresent( /* in */ ItemType item ) const;void SelSort();void Print() const;GList

    11、(); / Constructorprivate:int length;ItemType dataMAX_LENGTH; ;,Template parameter,Instantiating a Class Template,Class template arguments must be explicit. The compiler generates distinct class types called template classes or generated classes. When instantiating a template, a compiler substitutes

    12、the template argument for the template parameter throughout the class template.,Instantiating a Class Template,/ Client codeGList list1; GList list2; GList list3;list1.Insert(356); list2.Insert(84.375); list3.Insert(“Muffler bolt“);,To create lists of different data types,GList_int list1; GList_floa

    13、t list2; GList_string list3;,template argument,Compiler generates 3 distinct class types,Substitution Example,class GList_int public:void Insert( /* in */ ItemType item );void Delete( /* in */ ItemType item );bool IsPresent( /* in */ ItemType item ) const;private:int length;ItemType dataMAX_LENGTH;

    14、;,int,int,int,int,Function Definitions for Members of a Template Class,template void GList:Insert( /* in */ ItemType item ) datalength = item;length+; ,/after substitution of float void GList:Insert( /* in */ float item ) datalength = item;length+; ,Another Template Example: passing two parameters,t

    15、emplate class Stack .T bufsize;Stack mystack;,non-type parameter,Standard Template Library,In the late 70s Alexander Stepanov first observed that some algorithms do not depend on some particular implementation of a data structure but only on a few fundamental semantic properties of the structure Dev

    16、eloped by Stepanov and Lee at HP labs in 1992 Become part of the C+ Standard in 1994,Whats in STL?,Container classes: vector, list, deque, set, map, and etc A large collection of algorithms, such as reverse, swap, heap, and etc.,Vector,A sequence that supports random access to elements Elements can

    17、be inserted and removed at the beginning, the end and the middle Constant time random access Commonly used operations begin(), end(), size(), , push_back(), pop_back(), insert(), empty(),Example of vectors,/ Instantiate a vector vector V;/ Insert elements V.push_back(2); / v0 = 2 V.insert(V.begin(),

    18、 3); / V0 = 3, V1 = 2/ Random access V0 = 5; / V0 = 5/ Test the size int size = V.size(); / size = 2,Take Home Message,Templates are mechanisms for generating functions and classes on type parameters. We can design a single class or function that operates on data of many typesfunction templates class templates,


    注意事项

    本文(Introduction to C++ Templates and Exceptions.ppt)为本站会员(Iclinic170)主动上传,麦多课文档分享仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文档分享(点击联系客服),我们立即给予删除!




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
    备案/许可证编号:苏ICP备17064731号-1 

    收起
    展开