《《C++教程英文版》PPT课件》由会员分享,可在线阅读,更多相关《《C++教程英文版》PPT课件(86页珍藏版)》请在金锄头文库上搜索。
1、1,10,Classes: ADeeper Look, Part 2,2,10.1 Introduction,const objects and const member functions Prevent modifications of objects Enforce the principle of least privilege Composition Classes having objects of other classes as members Friendship Enables class designer to specify that certain non-membe
2、r functions can access the classs non-public members,3,10.1 Introduction (Cont.),this pointer Dynamic memory management new and delete operators static class members Proxy classes Hide implementation details of a class from clients Pointer-base strings Used in C legacy code from the last two decades
3、,4,10.2 const (Constant) Objects and const Member Functions,Principle of least privilege One of the most fundamental principles of good software engineering Applies to objects, too const objects Keyword const Specifies that an object is not modifiable Attempts to modify the object will result in com
4、pilation errors,5,10.2 const (Constant) Objects and const Member Functions (Cont.),const member functions Only const member function can be called for const objects Member functions declared const are not allowed to modify the object A function is specified as const both in its prototype and in its
5、definition const declarations 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
6、. If the object is const, the 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
7、 member function cannot modify the 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 (Const
8、ant) Objects and const Member Functions (Cont.),Member initializer Required for initializing const data members Data members that are references Can be used for any data member Member initializer list Appears between a constructors parameter list and the left brace that begins the constructors body
9、Separated from the parameter list with a colon (:) Each member initializer consists of the data member name followed by parentheses containing the members initial value Multiple member initializers are separated by commas Executes before the body of the constructor executes,16,Outline,Increment.h (1
10、 of 1),const data member that must be initialized using 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),
11、19,Outline,Increment.h (1 of 1),Member function declared const 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
12、,Outline,fig10_09.cpp (1 of 2),22,Outline,fig10_09.cpp (2 of 2),23,10.3 Composition: Objects as Members of Classes,Composition Sometimes referred to as a has-a relationship A class can have objects of other classes as members Example AlarmClock object with a Time object as a member,24,10.3 Compositi
13、on: Objects as Members of Classes (Cont.),Initializing member objects Member initializers pass arguments from the objects constructor to member-object constructors Member objects are constructed in the order in which they are declared in the class definition Not in the order they are listed in the c
14、onstructors member initializer list Before the enclosing class object (host object) is constructed If a member initializer is not provided The member objects default constructor will be called implicitly,25,Software Engineering Observation 10.6,Member objects are constructed in the order in which th
15、ey are declared in the class definition (not in the order they are listed in the 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,Out
16、line,Date.cpp (3 of 3),30,Outline,Employee.h (1 of 1),Parameters to be passed via member 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 constructor,34,Outline,fig10_14.cpp (2 of 2),35,10.4 friend Functions and friend Classes,frie