1: // Filenme: TESTIME2.CPP
2: // Purpose: Illustrates a simple time class, with the class accessed from a
3: // separate header (.H) file and separately compiled object (.OBJ)
4: // file. This driver tests the version of the Time class containing
5: // two constructors, and two other (non-member) functions.
7: #include <iostream>
8: using namespace std;
11: #include "TIME2.H"
12: #include "PAUSE.H"
15: void GetTimeFromUser(Time&);
16: Time IncrementedTime(const Time&, int);
19: int main()
20: {
21: cout << endl
22: << "This program illustrates the Time class "
23: << "with constructors, plus two non-member " << endl
24: << "functions. You should study the source "
25: << "code at the same time as the output. " << endl << endl;
27: Time t1(11, 14, 57);
28: Time t2;
29: t1.Display(); cout << endl;
30: t2.Display(); cout << endl;
31: Pause(0);
33: Time t3 = t1;
34: Time t4;
35: for (int i = 1; i <= 3; i++)
36: t1.Increment();
37: t4 = t1;
38: t3.Display(); cout << endl;
39: t4.Display(); cout << endl;
40: Pause(0);
42: Time myTime;
43: GetTimeFromUser(myTime);
44: myTime.Display(); cout << endl;
45: Pause(0);
47: int numberOfSeconds;
48: cout << "Enter a number of seconds to increment the time: ";
49: cin >> numberOfSeconds; cin.ignore(80, '\n'); cout << endl;
50: Time yourTime = IncrementedTime(myTime, numberOfSeconds);
51: yourTime.Display(); cout << endl;
52: Pause(0);
54: IncrementedTime(yourTime, 5).Display(); cout << endl;
56: return 0;
57: }
60: void GetTimeFromUser(/* out */ Time& t)
61: // Pre: none
62: // Post: "t" contains a time value entered by the user from the keyboard.
63: {
64: int hours;
65: int minutes;
66: int seconds;
68: cout << endl;
69: cout << "Enter the hours, minutes and "
70: << "seconds for a time. " << endl
71: << "Enter the values in that order, "
72: << "separated by a blank space: ";
73: cin >> hours >> minutes >> seconds; cin.ignore(80, '\n'); cout << endl;
75: t.Set(hours, minutes, seconds);
76: }
79: Time IncrementedTime(/* in */ const Time& t,
80: /* in */ int numberOfSeconds)
81: // Pre: "t" contains a valid time value.
82: // Post: The return-value of the function is the time value obtained
83: // by incrementing "t" by numberOfSeconds seconds. The value
84: // of "t" itself is unchanged.
85: {
86: Time tempTime = t;
87: for (int i = 1; i <= numberOfSeconds; i++)
88: tempTime.Increment();
89: return tempTime;
90: }