Classes A Deeper Look Part Chapter 910 Lecture

  • Slides: 21
Download presentation
Classes: A Deeper Look, Part Chapter 9、10 Lecture 8 -6

Classes: A Deeper Look, Part Chapter 9、10 Lecture 8 -6

OBJECTIVES In this chapter you’ll learn: • How to use a preprocessor wrapper to

OBJECTIVES In this chapter you’ll learn: • How to use a preprocessor wrapper to prevent multiple definition errors caused by including more than one copy of a header file in a source-code file. • To understand class scope and accessing class members via the name of an object, a reference to an object or a pointer to an object. • To define constructors with default arguments. • How destructors are used to perform "termination housekeeping" on an object before it is destroyed. • When constructors and destructors are called and the order in which they are called. • The logic errors that may occur when a public member function of a class returns a reference to private data. • To assign the data members of one object to those of another object by default memberwise assignment.

Lecture 8 -6 Contents 01 Time CLASS CASE STUDY 02 DESTRUCTORS 03 WHEN CONSTRUCTORS

Lecture 8 -6 Contents 01 Time CLASS CASE STUDY 02 DESTRUCTORS 03 WHEN CONSTRUCTORS AND DESTRUCTORS ARE CALLED 04 ACCESS FUNCTIONS AND UTILITY FUNCTIONS

01 Access Functions and Utility FUNCTIONS • Access functions • Can read or display

01 Access Functions and Utility FUNCTIONS • Access functions • Can read or display data • Can test the truth or falsity of conditions • Such functions are often called predicate functions • For example, is. Empty function for a class capable of holding many objects • Utility functions (also called helper functions) • private member functions that support the operation of the class’s public member functions • Not part of a class’s public interface • Not intended to be used by clients of a class 4

Sales. Person. h (1 of 1) Prototype for a private utility function

Sales. Person. h (1 of 1) Prototype for a private utility function

Sales. Person. cpp (1 of 3)

Sales. Person. cpp (1 of 3)

Sales. Person. cpp (2 of 3)

Sales. Person. cpp (2 of 3)

Calling a private utility function Sales. Person. cpp (3 of 3) Definition of a

Calling a private utility function Sales. Person. cpp (3 of 3) Definition of a private utility function

fig 09_07. cpp (1 of 1)

fig 09_07. cpp (1 of 1)

02 Time Class Case Study: A Subtle Trap—Returning a Reference to a private Data

02 Time Class Case Study: A Subtle Trap—Returning a Reference to a private Data Member • Returning a reference to an object • Alias for the name of an object • An acceptable lvalue that can receive a value • May be used on the left side of an assignment statement • If a function returns a const reference • That reference cannot be used as a modifiable lvalue • One (dangerous) way to use this capability • A public member function of a class returns a reference to a private data member of that class • Client code could alter private data • Same problem would occur if a pointer to private data were returned 10

Time. h (1 of 1) Prototype for function that returns a reference

Time. h (1 of 1) Prototype for function that returns a reference

Time. cpp (1 of 2)

Time. cpp (1 of 2)

Time. cpp (2 of 2) Returning a reference to a private data member =

Time. cpp (2 of 2) Returning a reference to a private data member = DANGEROUS!

Fig 09_16. cpp (1 of 2) Modifying a private data member through a returned

Fig 09_16. cpp (1 of 2) Modifying a private data member through a returned reference

Modifying private data by using a function call as an lvalue Fig 09_16. cpp

Modifying private data by using a function call as an lvalue Fig 09_16. cpp (2 of 2)

03 Proxy Classes • Header files contain some portion of a class’s implementation and

03 Proxy Classes • Header files contain some portion of a class’s implementation and hints about others • For example, a class’s private members are listed in the class definition in a header file • Potentially exposes proprietary information to clients of the class

03 Proxy Classes (Cont. ) • Proxy class • Hides even the private data

03 Proxy Classes (Cont. ) • Proxy class • Hides even the private data of a class from clients • Knows only the public interface of your class • Enables the clients to use your class’s services without giving the client access to your class’s implementation details

Class definition for the class that contains the proprietary implementation we would like to

Class definition for the class that contains the proprietary implementation we would like to hide The data we would like to hide from the client Implementation. h (1 of 1)

Interface. h Declares Implementation as a data type without including the class’s complete header

Interface. h Declares Implementation as a data type without including the class’s complete header file public interface between client and hidden class Using a pointer allows us to hide implementation details of class Implementation (1 of 1)

Only location where Implementation. h is included with #include Interface. cpp (1 of 1)

Only location where Implementation. h is included with #include Interface. cpp (1 of 1) Setting the value of the hidden data via a pointer Getting the value of the hidden data via a pointer

fig 10_27. cpp (1 of 1) Only the header file for Interface is included

fig 10_27. cpp (1 of 1) Only the header file for Interface is included in the client code—no mention of the existence of a separate class called Implementation