Sams Teach Yourself C in 21 Days

(singke) #1
54: hours += 1;
55: while (hours >= 24 )
56: {
57: hours -= 24;
58: }
59: }
60:
61: // Add 1 to the number of minutes
62: //--------------------------------
63: void time::add_minute(void)
64: {
65: minutes += 1;
66: while (minutes >= 60)
67: {
68: add_hour();
69: minutes -= 60;
70: }
71: }
72:
73: // Add 1 to the number of seconds
74: //--------------------------------
75: void time::add_second(void)
76: {
77: seconds += 1;
78: while (seconds >= 60)
79: {
80: add_minute();
81: seconds -= 60;
82: }
83: }

Start time: 7:10:15
End time: 11:20:30
Start time: 8:11:16
New end time: 12:21:31
Although this listing is different from the previous listing, it accomplishes the
same end results. This listing is, however, more object oriented. Looking at lines
5–16, you see that the timestructure has been defined. In this example, in lines 12–15 of
thetimestructure, you can see that the add...functions and the print_time()function
have been declared. Note that these are inside of the structure. It is this internal declara-
tion that makes these functions members of the timestructure, just as the data members
in lines 7–9 are members of the timestructure.

672 Bonus Day 3

LISTINGB3.3 continued

OUTPUT

ANALYSIS

38 448201x-Bonus3 8/13/02 11:19 AM Page 672

Free download pdf