|
|
|
|
|
|
|
TimeType startTime;
TimeType endTime; |
|
|
|
|
|
|
|
|
Any software that declares and manipulates TimeType objects is called a client of the class. |
|
|
|
 |
|
 |
|
|
Class A structured type in a programming language that is used to represent an abstract data type. |
|
|
|
 |
|
 |
|
|
Class Member A component of a class. Class members may be either data or functions. |
|
|
|
 |
|
 |
|
|
Class Object (Class Instance) A variable of a class type. |
|
|
|
 |
|
 |
|
|
Client Software that declares and manipulates objects of a particular class. |
|
|
|
|
|
|
|
|
As you look at the preceding declaration of the TimeType class, you can see the reserved words public and private (each followed by a colon). Data and/or functions declared between the words public and private constitute the public interface; clients can access these class members directly. Class members declared after the word private are considered private information and are inaccessible to clients. If client code attempts to access a private item, the compiler will signal an error. |
|
|
|
|
|
|
|
|
Private class members can be accessed only by the class's member functions. In the TimeType class, the private variables hrs, mins, and secs can be accessed only by the member functions Set, Increment, Write, Equal, and LessThan, not by client code. This separation of class members into private and public parts is a hallmark of ADT design. To preserve correctly the properties of an ADT, an instance of the ADT should be manipulated only through the operations that form the public interface. We have more to say about this issue later in the chapter. |
|
|
|
| |
|
|
|
|
Declaring Public and Private Class Members |
|
|
|
| |
|
|
|
|
C++ does not require you to declare public and private class members in a fixed order. Several variations are possible. |
|
|
|
| |
|
|
|
|
By default, class members are private; the word public must be used to open up any members for public access. Therefore, we could write the TimeType class declaration as follows: |
|
|
|
|
|
|
|
|
|
(text box continues on next page) |
|
|
|
|
|