C++教程英文版.ppt
《C++教程英文版.ppt》由会员分享,可在线阅读,更多相关《C++教程英文版.ppt(85页珍藏版)》请在三一办公上搜索。
1、1,10,Classes:ADeeper Look,Part 2,2,10.1 Introduction,const objects and const member functionsPrevent modifications of objects Enforce the principle of least privilegeCompositionClasses having objects of other classes as membersFriendshipEnables class designer to specify that certain non-member funct
2、ions can access the classs non-public members,3,10.1 Introduction(Cont.),this pointerDynamic memory managementnew and delete operatorsstatic class membersProxy classes Hide implementation details of a class from clientsPointer-base stringsUsed in C legacy code from the last two decades,4,10.2 const(
3、Constant)Objects and const Member Functions,Principle of least privilege One of the most fundamental principles of good software engineeringApplies to objects,tooconst objectsKeyword constSpecifies that an object is not modifiable Attempts to modify the object will result in compilation errors,5,10.
4、2 const(Constant)Objects and const Member Functions(Cont.),const member functionsOnly const member function can be called for const objectsMember functions declared const are not allowed to modify the object A function is specified as const both in its prototype and in its definitionconst declaratio
5、ns are not allowed for constructors and destructors,6,Software Engineering Observation 10.2,A const member function can be overloaded with a non-const version.The compiler chooses which overloaded member function to use based on the object on which the function is invoked.If the object is const,the
6、compiler uses the const version.If the object is not const,the compiler uses the non-const version.,7,Common Programming Error 10.4,Attempting to declare a constructor or destructor const is a compilation error.,8,Outline,Time.h(1 of 2),const keyword to indicate that member function cannot modify th
7、e object,9,Outline,Time.h(2 of 2),10,Outline,Time.cpp(1 of 3),11,Outline,Time.cpp(2 of 3),12,Outline,Time.cpp(3 of 3),13,Outline,fig10_03.cpp(1 of 2),Cannot invoke non-const member functions on a const object,14,Outline,fig10_03.cpp(2 of 2),15,10.2 const(Constant)Objects and const Member Functions(C
8、ont.),Member initializer Required for initializingconst 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 parameter list with a colon(:
9、)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,16,Outline,Increment.h(1 of 1),const data member that must be initialized usi
10、ng a member initializer,17,Outline,Increment.cpp(1 of 1),Colon(:)marks the start of a member initializer list,Member initializer for non-const member count,Required member initializer for const member increment,18,Outline,fig10_06.cpp(1 of 1),19,Outline,Increment.h(1 of 1),Member function declared c
11、onst to prevent errors in situations where an Increment object is treated as a const object,20,Outline,Increment.cpp(1 of 1),It is an error to modify a const data member;data member increment must be initialized with a member initializer,21,Outline,fig10_09.cpp(1 of 2),22,Outline,fig10_09.cpp(2 of 2
12、),23,10.3 Composition:Objects as Members of Classes,CompositionSometimes referred to as a has-a relationshipA class can have objects of other classes as membersExampleAlarmClock object with a Time object as a member,24,10.3 Composition:Objects as Members of Classes(Cont.),Initializing member objects
13、Member initializers pass arguments from the objects constructor to member-object constructorsMember 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(hos
14、t object)is constructedIf a member initializer is not providedThe member objects default constructor will be called implicitly,25,Software Engineering Observation 10.6,Member objects are constructed in the order in which they are declared in the class definition(not in the order they are listed in t
15、he constructors member initializer list)and before their enclosing class objects(sometimes called host objects)are constructed.,26,Outline,Date.h(1 of 1),27,Outline,Date.cpp(1 of 3),28,Outline,Date.cpp(2 of 3),29,Outline,Date.cpp(3 of 3),30,Outline,Employee.h(1 of 1),Parameters to be passed via memb
16、er initializers to the constructor for class Date,const objects of class Date as members,31,Outline,Employee.cpp(1 of 2),Member initializers that pass arguments to Dates implicit default copy constructor,32,Outline,Employee.cpp(2 of 2),33,Outline,fig10_14.cpp(1 of 2),Passing objects to a host object
17、 constructor,34,Outline,fig10_14.cpp(2 of 2),35,10.4 friend Functions and friend Classes,friend function of a class Defined outside that classs scopeNot a member function of that classYet has the right to access the non-public(and public)members of that classStandalone functions or entire classes ma
18、y be declared to be friends of a class Can enhance performanceOften appropriate when a member function cannot be used for certain operations,36,10.4 friend Functions and friend Classes(Cont.),To declare a function as a friend of a class:Provide the function prototype in the class definition preceded
19、 by keyword friendTo declare a class as a friend of a class:Place a declaration of the form friend class ClassTwo;in the definition of class ClassOneAll member functions of class ClassTwo are friends of class ClassOne,37,10.4 friend Functions and friend Classes(Cont.),Friendship is granted,not taken
20、For class B to be a friend of class A,class A must explicitly declare that class B is its friendFriendship relation is neither symmetric nor transitiveIf class A is a friend of class B,and class B is a friend of class C,you cannot infer that class B is a friend of class A,that class C is a friend of
21、 class B,or that class A is a friend of class C It is possible to specify overloaded functions as friends of a classEach overloaded function intended to be a friend must be explicitly declared as a friend of the class,38,Software Engineering Observation 10.10,Some people in the OOP community feel th
22、at“friendship”corrupts information hiding and weakens the value of the object-oriented design approach.In this text,we identify several examples of the responsible use of friendship.,39,Outline,fig10_15.cpp(1 of 2),friend function declaration(can appear anywhere in the class),40,Outline,fig10_15.cpp
23、(2 of 2),friend function can modify Counts private data,Calling a friend function;note that we pass the Count object to the function,41,Outline,fig10_16.cpp(1 of 3),42,Outline,fig10_16.cpp(2 of 3),Non-friend function cannot access the classs private data,43,Outline,fig10_16.cpp(3 of 3),Classes Part
24、II,friend Class Example,/ClassTwo Interface class ClassTwo friend class ClassOne;public:/constructor ClassTwo():x(0),y(0),z(0)/no body void print()const cout x=x endl;cout y=y endl;cout z=z endl;private:int x,y,z;/data members void setX(int val)x=val;void setY(int val)y=val;void setZ(int val)z=val;,
25、/ClassOne Interface#include“classtwo.h”class ClassOne public:void setX(int val)c.x=val;void setY(int val)c.y=val;void setZ(int val)c.z=val;void print()const cout x=c.x endl;cout y=c.y endl;cout z=c.z endl;private:ClassTwo c;,Classes Part II,CIS 554/45,friend Class Exampletest driver and output,#incl
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C+ 教程 英文
链接地址:https://www.31ppt.com/p-6153960.html