Classes A Deeper Look Part Chapter 910 Lecture
- Slides: 21
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 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 AND DESTRUCTORS ARE CALLED 04 ACCESS FUNCTIONS AND UTILITY FUNCTIONS
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. cpp (1 of 3)
Sales. Person. cpp (2 of 3)
Calling a private utility function Sales. Person. cpp (3 of 3) Definition of a private utility function
fig 09_07. cpp (1 of 1)
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. cpp (1 of 2)
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 reference
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 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 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 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 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) 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 in the client code—no mention of the existence of a separate class called Implementation
- Look left right
- Into the heart of jesus deeper and deeper i go
- Nia 401
- Dm 910 del 2017
- Mmc 910
- Instalações eletricas
- Colchonero vertical
- 910 dewey decimal
- 910 dewey decimal
- Tom tom go 910
- Mmc 910
- Jamal goodrich
- Digitrace 910 controller
- Classe e subclasse de e
- Pre ap classes vs regular classes
- 01:640:244 lecture notes - lecture 15: plat, idah, farad
- 1098 t rio hondo
- Activity 1 picture analysis with answers
- Picture analysis activity 1
- Look at activity 1 and answer
- Go deeper in christ
- Deeper life bible church messages