A class's private data members can be accessed only by member functions (and friends) of the class. Classes often provide public member functions to allow clients of the class to set (i.e., write) or get (,.e., read) the values of private data members. These functions need not be called set and get specifically, but they often are.
- Set functions
- Perform validity checks before modifying private data
- Notify if invalid values
- Indicate with return values
- Get functions
- "Query" functions
- Control format of data returned
The program of Figs. 14-18 enhances class Time to include set and get functions for the private data members hour, minute, and second.
Figure 14:
Time class definition with set and get functions.
|
Figure 15:
Time class member-function definitions,including set and get functions. (part 1 of 2)
|
Figure 16:
Time class member-function definitions,including set and get functions. (part 2 of 2)
|
Figure 17:
Set and get functions manipulating an object's private data. (part 1 of 2)
|
Figure 18:
Set and get functions manipulating an object's private data. (part 2 of 2)
|
2004-07-01