Sams Teach Yourself C in 21 Days

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

BD3


5: struct time {
6: // Data Members:
7: int hours;
8: int minutes;
9: int seconds;
10:
11: // Member Functions:
12: void print_time(void);
13: void add_hour(void);
14: void add_minute(void);
15: void add_second(void);
16: };
17:
18: int main(int argc, char* argv[])
19: {
20: struct time start_time = {7, 15, 20};
21: struct time end_time = {10, 20, 30};
22:
23: // Print initial times
24: cout << “\nStart time: “;
25: start_time.print_time();
26: cout << “\nEnd time: “;
27: end_time.print_time();
28:
29: // Add 1 hour, 1 minute, and 1 second to end time
30: end_time.add_hour();
31: end_time.add_minute();
32: end_time.add_second();
33:
34: // Print final times
35: cout << “\n\nStart time: “;
36: start_time.print_time();
37: cout << “\nNew end time: “;
38: end_time.print_time();
39:
40: return 0;
41: }
42:
43: // Print a time structure in h:m:s format
44: //----------------------------------------
45: void time::print_time(void)
46: {
47: cout << hours << “:” << minutes << “:” << seconds;
48: }
49:
50: // Add 1 to the number of hours
51: //------------------------------
52: void time::add_hour(void)
53: {

LISTINGB3.3 continued

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

Free download pdf