Sams Teach Yourself C in 21 Days

(singke) #1
26: start_time.init(7, 15, 20);
27: end_time.init(10, 20, 30);
28:
29: // Print initial times
30: cout << “\nStart time: “;
31: start_time.print_time();
32: cout << “\nEnd time: “;
33: end_time.print_time();
34:
35: // Add 1 hour, 1 minute, and 1 second to end time
36: end_time.add_hour();
37: end_time.add_minute();
38: end_time.add_second();
39:
40: // Print final times
41: cout << “\n\nStart time: “;
42: start_time.print_time();
43: cout << “\nNew end time: “;
44: end_time.print_time();
45:
46: return 0;
47: }
48:
49: // Print a time structure in h:m:s format
50: //----------------------------------------
51: void time::print_time(void)
52: {
53: cout << hours << “:” << minutes << “:” << seconds;
54: }
55:
56: // Add 1 to the number of hours
57: //------------------------------
58: void time::add_hour(void)
59: {
60: hours += 1;
61: while (hours >= 24 )
62: {
63: hours -= 24;
64: }
65: }
66:
67: // Add 1 to the number of minutes
68: //--------------------------------
69: void time::add_minute(void)
70: {
71: minutes += 1;
72: while (minutes >= 60)
73: {
74: add_hour();

678 Bonus Day 3

LISTINGB3.4 continued

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

Free download pdf