1.16k likes | 1.17k Views
Learn about const objects and const member functions, composition, friend functions and classes, dynamic memory management, static class members, container classes and iterators, and proxy classes.
E N D
21 • Classes: A Deeper Look, Part 2
But what, to serve our private ends, Forbids the cheating of our friends? Charles Churchill • Instead of this absurd division into sexes they ought to class people as static and dynamic. • Evelyn Waugh • Have no friends not equal to yourself. • Confucius
OBJECTIVES In this chapter you will learn: • To specify const (constant) objects and const member functions. • To create objects composed of other objects. • To use friend functions and friend classes. • To use the this pointer. • To create and destroy objects dynamically with operators new and delete, respectively. • To use static data members and member functions. • The concept of a container class. • The notion of iterator classes that walk through the elements of container classes. • To use proxy classes to hide implementation details from a class's clients.
21.1 Introduction • 21.2 const (Constant) Objects and const Member Functions • 21.3 Composition: Objects as Members of Classes • 21.4friend Functions and friend Classes • 21.5 Using the this Pointer • 21.6 Dynamic Memory Management with Operators new and delete • 21.7 static Class Members • 21.8 Data Abstraction and Information Hiding • 21.8.1 Example: Array Abstract Data Type • 21.8.2 Example: String Abstract Data Type • 21.8.3 Example: Queue Abstract Data Type • 21.9 Container Classes and Iterators • 21.10 Proxy Classes • 21.11 Wrap-Up
21.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-member functions can access the class’s non-public members
21.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
21.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 compilation errors
Software Engineering Observation 21.1 • Declaring an object as const helps enforce the principle of least privilege. Attempts to modify the object are caught at compile time rather than causing execution-time errors. Using const properly is crucial to proper class design, program design and coding.
Performance Tip 21.1 • Declaring variables and objects const can improve performance—today’s sophisticated optimizing compilers can perform certain optimizations on constants that cannot be performed on variables.
21.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 definition • const declarations are not allowed for constructors and destructors
Common Programming Error 21.1 • Defining as const a member function that modifies a data member of an object is a compilation error.
Common Programming Error 21.2 • Defining as const a member function that calls a non-const member function of the class on the same instance of the class is a compilation error.
Common Programming Error 21.3 • Invoking a non-const member function on a const object is a compilation error.
Software Engineering Observation 21.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 compiler uses the const version. If the object is not const, the compiler uses the non-const version.
Common Programming Error 21.4 • Attempting to declare a constructor or destructor const is a compilation error.
Outline Time.h (1 of 2) const keyword to indicate that member function cannot modify the object
Outline Time.h (2 of 2)
Outline Time.cpp (1 of 3)
const keyword in function definition, as well as in function prototype Outline Time.cpp (2 of 3)
Outline Time.cpp (3 of 3)
Outline fig21_03.cpp (1 of 2) Cannot invoke non-const member functions on a constobject
Outline fig21_03.cpp (2 of 2)
21.2 const (Constant) 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 constructor’s parameter list and the left brace that begins the constructor’s body • Separated from the parameter list with a colon (:) • Each member initializer consists of the data member name followed by parentheses containing the member’s initial value • Multiple member initializers are separated by commas • Executes before the body of the constructor executes
Outline Increment.h (1 of 1) const data member that must be initialized using a member initializer
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
Outline fig21_06.cpp (1 of 1)
Software Engineering Observation 21.3 • A const object cannot be modified by assignment, so it must be initialized. When a data member of a class is declared const, a member initializer must be used to provide the constructor with the initial value of the data member for an object of the class. The same is true for references.
Common Programming Error 21.5 • Not providing a member initializer for a const data member is a compilation error.
Software Engineering Observation 21.4 • Constant data members (const objects and const variables) and data members declared as references must be initialized with member initializer syntax; assignments for these types of data in the constructor body are not allowed.
Error-Prevention Tip 21.1 • Declare as const all of a class’s member functions that do not modify the object in which they operate. Occasionally this may seem inappropriate, because you will have no intention of creating const objects of that class or accessing objects of that class through const references or pointers to const. Declaring such member functions const does offer a benefit, though. If the member function is inadvertently written to modify the object, the compiler will issue an error message.
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
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
Outline fig21_09.cpp (1 of 2)
Outline fig21_09.cpp (2 of 2)
21.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
21.3 Composition: Objects as Members of Classes (Cont.) • Initializing member objects • Member initializers pass arguments from the object’s 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 constructor’s member initializer list • Before the enclosing class object (host object) is constructed • If a member initializer is not provided • The member object’s default constructor will be called implicitly
Software Engineering Observation 21.5 • A common form of software reusability is composition, in which a class has objects of other classes as members.
Outline Date.h (1 of 1)
Outline Date.cpp (1 of 3)
Outline Date.cpp (2 of 3)
Outline Date.cpp (3 of 3)
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
Outline Employee.cpp (1 of 2) Member initializers that pass arguments to Date’simplicit default copy constructor
Outline Employee.cpp (2 of 2)
Outline fig21_14.cpp (1 of 2) Passing objects to a host object constructor
Outline fig21_14.cpp (2 of 2)
Common Programming Error 21.6 • A compilation error occurs if a member object is not initialized with a member initializer and the member object’s class does not provide a default constructor (i.e., the member object’s class defines one or more constructors, but none is a default constructor).
Performance Tip 21.2 • Initialize member objects explicitly through member initializers. This eliminates the overhead of “doubly initializing” member objects—once when the member object’s default constructor is called and again when set functions are called in the constructor body (or later) to initialize the member object.
Software Engineering Observation 21.6 • If a class member is an object of another class, making that member object public does not violate the encapsulation and hiding of that member object’s private members. However, it does violate the encapsulation and hiding of the containing class’s implementation, so member objects of class types should still be private, like all other data members.
21.4 friend Functions and friend Classes • friend function of a class • Defined outside that class’s scope • Not a member function of that class • Yet has the right to access the non-public (and public) members of that class • Standalone functions or entire classes may be declared to be friends of a class • Can enhance performance • Often appropriate when a member function cannot be used for certain operations