< previous page page_846 next page >

Page 846
(text box continued from previous page)
We use this style throughout the remainder of the book.
Regarding public versus private accessibility, we can now describe more fully the difference between C++ structs and classes. C++ defines a struct to be a class whose members are all, by default, public. In contrast, members of a class are, by default, private. Furthermore, it is most common to use only data, not functions, as members of a struct. Note that you can declare struct members to be private and you can include member functions in a struct, but then you might as well use a class!

Classes, Class Objects, and Class Members
It is important to restate that a class is a type, not a data object. Like any type, a class is a pattern from which you create (or instantiate) many objects of that type. Think of a type as a cookie cutter and objects of that type as the cookies.
The declarations
TimeType time1;
TimeType time2;
create two objects of the TimeType class: time1 and time2. Each object has its own copies of hrs, mins, and secs the private data members of the class. At a given moment during program execution, time1's copies of hrs, mins, and secs might contain the values 5, 30, and 10; and time2's copies might contain the values 17, 58, and 2. Figure 15-3 is a visual image of the class objects time1 and time2.
(In truth, the C++ compiler does not waste memory by placing duplicate copies of a member functionsay, Incrementinto both time1 and time2. The compiler generates just one physical copy of Increment, and any class object executes this one copy of the function. Nevertheless, the diagram in Figure 15-3 is a good mental picture of two different class objects.)
Be sure you are clear about the difference between the terms class object and class member. Figure 15-3 depicts two objects of the TimeType class, and each object has eight members.
Built-In Operations on Classes
In many ways, programmer-defined classes are like built-in types. You can declare as many objects of a class as you like. You can pass class objects as parameters to functions and return them as function values. You can declare arrays of class objects. Like any variable, a class object can be automatic (created each time control reaches its declaration and destroyed when con-

 
< previous page page_846 next page >