< previous page page_851 next page >

Page 851
class's implementor that the user cannot directly access any private code or data and compromise the correct functioning of the implementation.
You have been introduced to encapsulation and information hiding before. In Chapter 7, we discussed the possibility of hiding a function's implementation in a separate file. In this chapter, you'll see how to hide the implementations of class member functions by placing them in files separate from the client code.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Abstraction Barrier The invisible wall around a class object that encapsulates implementation details. The wall can be breached only through the public interface.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Black Box An electrical or mechanical device whose inner workings are hidden from view.
3e26ecb1b6ac508ae10a0e39d2fb98b2.gif 3e26ecb1b6ac508ae10a0e39d2fb98b2.gif
Information Hiding The encapsulation and hiding of implementation details to keep the user of an abstraction from depending on or incorrectly manipulating these details.
The creator of a C++ class is free to choose which members are private and which are public. However, making data members public allows the client to inspect and modify them directly. Because information hiding is so fundamental to data abstraction, most classes exhibit a typical pattern: the private part contains data, and the public part contains the functions that manipulate the data.
The TimeType class exemplifies this organization. The data members hrs, mins, and secs are private, so the compiler prohibits a client from accessing these members directly. The following client statement therefore results in a compile-time error:
checkInTime.hrs = 9;    // Prohibited
Because only the class's member functions can access the private data, the creator of the class can offer a reliable product, knowing that external access to the private data is impossible. If it is acceptable to let the client inspect (but not modify) private data members, a class might provide observer functions. The TimeType class has three such functions: Write, Equal, and LessThan. Because these observer functions are not intended to modify the private data, they are declared with the word const following the parameter list:
void    Write() const;
Boolean Equal( TimeType ) const;
Boolean LessThan( TimeType ) const;

 
< previous page page_851 next page >