Sams Teach Yourself C in 21 Days

(singke) #1
Working with C++ Classes and Objects 669

BD3


43: //----------------------------------------
44: void print_time(struct time tm)
45: {
46: cout << tm.hours << “:” << tm.minutes << “:” << tm.seconds;
47: }
48:
49: // Add 1 to the number of hours
50: //------------------------------
51: void add_hour(struct time *tm)
52: {
53: tm->hours += 1;
54: while (tm->hours >= 24 )
55: {
56: tm->hours -= 24;
57: }
58: }
59:
60: // Add 1 to the number of minutes
61: //------------------------------
62: void add_minute(struct time *tm)
63: {
64: tm->minutes += 1;
65: while (tm->minutes >= 60)
66: {
67: add_hour(tm);
68: tm->minutes -= 60;
69: }
70: }
71:
72: // Add 1 to the number of seconds
73: //------------------------------
74: void add_second(struct time *tm)
75: {
76: tm->seconds += 1;
77: while (tm->seconds >= 60)
78: {
79: add_minute(tm);
80: tm->seconds -= 60;
81: }
82: }

Start time: 7:10:15
End time: 11:20:30

Start time: 7:10:15
New end time: 12:21:31
As you can see, Listing B3.2 differs from Listing B3.1 in a couple of ways. The
first is that the three add...functions have been added to the listing. The

LISTINGB3.2 continued

OUTPUT

ANALYSIS

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

Free download pdf