C++教程英文版.ppt
《C++教程英文版.ppt》由会员分享,可在线阅读,更多相关《C++教程英文版.ppt(85页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、 2008 Pearson Education,Inc.All rights reserved.110Classes:ADeeperLook,Part2 2008 Pearson Education,Inc.All rights reserved.210.1Introductionconstconst objects and constconst member functionsPrevent modifications of objects Enforce the principle of least privilegeCompositionClasses having objects of
2、 other classes as membersFriendshipEnables class designer to specify that certain non-member functions can access the classs non-publicpublic members 2008 Pearson Education,Inc.All rights reserved.310.1Introduction(Cont.)thisthis pointerDynamic memory managementnewnew and deletedelete operatorsstati
3、cstatic class membersProxy classes Hide implementation details of a class from clientsPointer-base stringsUsed in C legacy code from the last two decades 2008 Pearson Education,Inc.All rights reserved.410.2constconst(Constant)ObjectsandconstconstMemberFunctionsPrinciple of least privilege One of the
4、 most fundamental principles of good software engineeringApplies to objects,tooconstconst objectsKeyword constconstSpecifies that an object is not modifiable Attempts to modify the object will result in compilation errors 2008 Pearson Education,Inc.All rights reserved.510.2constconst(Constant)Object
5、sandconstconstMemberFunctions(Cont.)constconst member functionsOnly constconst member function can be called for constconst objectsMember functions declared constconst are not allowed to modify the object A function is specified as constconst both in its prototype and in its definitionconstconst dec
6、larations are not allowed for constructors and destructors 2008 Pearson Education,Inc.All rights reserved.6SoftwareEngineeringObservation10.2A constconst member function can be overloaded with a non-constconst version.The compiler chooses which overloaded member function to use based on the object o
7、n which the function is invoked.If the object is constconst,the compiler uses the constconst version.If the object is not constconst,the compiler uses the non-constconst version.2008 Pearson Education,Inc.All rights reserved.7CommonProgrammingError10.4Attempting to declare a constructor or destructo
8、r constconst is a compilation error.2008 Pearson Education,Inc.All rights reserved.8OutlineTime.hTime.h (1 of 2)const keyword to indicate that member function cannot modify the object 2008 Pearson Education,Inc.All rights reserved.9OutlineTime.hTime.h (2 of 2)2008 Pearson Education,Inc.All rights re
9、served.10OutlineTime.cppTime.cpp (1 of 3)2008 Pearson Education,Inc.All rights reserved.11OutlineTime.cppTime.cpp (2 of 3)const keyword in function definition,as well as in function prototype 2008 Pearson Education,Inc.All rights reserved.12OutlineTime.cppTime.cpp (3 of 3)2008 Pearson Education,Inc.
10、All rights reserved.13Outlinefig10_03.cppfig10_03.cpp (1 of 2)Cannot invoke non-const member functions on a const object 2008 Pearson Education,Inc.All rights reserved.14Outlinefig10_03.cppfig10_03.cpp (2 of 2)2008 Pearson Education,Inc.All rights reserved.1510.2constconst(Constant)Objectsandconstco
11、nstMemberFunctions(Cont.)Member initializer Required for initializingconstconst data membersData members that are referencesCan be used for any data memberMember initializer listAppears between a constructors parameter list and the left brace that begins the constructors bodySeparated from the param
12、eter list with a colon(:)Each member initializer consists of the data member name followed by parentheses containing the members initial valueMultiple member initializers are separated by commasExecutes before the body of the constructor executes 2008 Pearson Education,Inc.All rights reserved.16Outl
13、ineIncrement.hIncrement.h (1 of 1)const data member that must be initialized using a member initializer 2008 Pearson Education,Inc.All rights reserved.17OutlineIncrement.cppIncrement.cpp(1 of 1)Colon(:)marks the start of a member initializer listMember initializer for non-const member countRequired
14、member initializer for const member increment 2008 Pearson Education,Inc.All rights reserved.18Outlinefig10_06.cppfig10_06.cpp (1 of 1)2008 Pearson Education,Inc.All rights reserved.19OutlineIncrement.hIncrement.h (1 of 1)Member function declared const to prevent errors in situations where an Increm
15、ent object is treated as a const object 2008 Pearson Education,Inc.All rights reserved.20OutlineIncrement.cppIncrement.cpp (1 of 1)It is an error to modify a const data member;data member increment must be initialized with a member initializer 2008 Pearson Education,Inc.All rights reserved.21Outline
16、fig10_09.cppfig10_09.cpp (1 of 2)2008 Pearson Education,Inc.All rights reserved.22Outlinefig10_09.cppfig10_09.cpp (2 of 2)2008 Pearson Education,Inc.All rights reserved.2310.3Composition:ObjectsasMembersofClassesCompositionSometimes referred to as a has-a relationshipA class can have objects of othe
17、r classes as membersExampleAlarmClockAlarmClock object with a TimeTime object as a member 2008 Pearson Education,Inc.All rights reserved.2410.3Composition:ObjectsasMembersofClasses(Cont.)Initializing member objectsMember initializers pass arguments from the objects constructor to member-object const
18、ructorsMember objects are constructed in the order in which they are declared in the class definitionNot in the order they are listed in the constructors member initializer listBefore the enclosing class object(host object)is constructedIf a member initializer is not providedThe member objects defau
19、lt constructor will be called implicitly 2008 Pearson Education,Inc.All rights reserved.25SoftwareEngineeringObservation10.6Member objects are constructed in the order in which they are declared in the class definition(not in the order they are listed in the constructors member initializer list)and
20、before their enclosing class objects(sometimes called host objects)are constructed.2008 Pearson Education,Inc.All rights reserved.26OutlineDate.hDate.h (1 of 1)2008 Pearson Education,Inc.All rights reserved.27OutlineDate.cppDate.cpp (1 of 3)2008 Pearson Education,Inc.All rights reserved.28OutlineDat
21、e.cppDate.cpp (2 of 3)2008 Pearson Education,Inc.All rights reserved.29OutlineDate.cppDate.cpp (3 of 3)2008 Pearson Education,Inc.All rights reserved.30OutlineEmployee.hEmployee.h (1 of 1)Parameters to be passed via member initializers to the constructor for class Dateconst objects of class Date as
22、members 2008 Pearson Education,Inc.All rights reserved.31OutlineEmployee.cppEmployee.cpp(1 of 2)Member initializers that pass arguments to Dates implicit default copy constructor 2008 Pearson Education,Inc.All rights reserved.32OutlineEmployee.cppEmployee.cpp(2 of 2)2008 Pearson Education,Inc.All ri
23、ghts reserved.33Outlinefig10_14.cppfig10_14.cpp(1 of 2)Passing objects to a host object constructor 2008 Pearson Education,Inc.All rights reserved.34Outlinefig10_14.cppfig10_14.cpp(2 of 2)2008 Pearson Education,Inc.All rights reserved.3510.4friendfriendFunctionsandfriendfriendClassesfriendfriend fun
24、ction of a class Defined outside that classs scopeNot a member function of that classYet has the right to access the non-publicpublic(and publicpublic)members of that classStandalone functions or entire classes may be declared to be friends of a class Can enhance performanceOften appropriate when a
25、member function cannot be used for certain operations 2008 Pearson Education,Inc.All rights reserved.3610.4friendfriendFunctionsandfriendfriendClasses(Cont.)To declare a function as a friendfriend of a class:Provide the function prototype in the class definition preceded by keyword friendfriendTo de
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 教程 英文
限制150内