1: // Filenme: TESTIME4.CPP
2: // Purpose: Tests the Time class in TIME4.H and TIME4.CPP, which
3: // has a four-in-one constructor, accessor functions,
4: // friend functions, and overloaded operators.
6: #include <iostream>
7: #include <fstream>
8: #include <iomanip>
9: using namespace std;
11: #include "TIME4.H"
12: #include "PAUSE.H"
15: int main()
16: {
17: cout << endl
18: << "This program tests the Time class with "
19: << "a four-in-one constructor, " << endl
20: << "accessor functions, friend functions, "
21: << "and overloaded operators. " << endl
22: << "You should study the source code "
23: << "at the same time as the output. " << endl << endl;
25: Time t0; // Default constructor (no parameters) invoked
26: Time t1(12); // One-parameter constructor invoked
27: Time t2(11, 45); // Two-parameter constructor invoked
28: Time t3(11, 44, 55); // Three-parameter constructor invoked
29: cout << t0; cout << endl; // Overloaded << operator used with cout
30: cout << t1; cout << endl;
31: cout << t2 << setw(12) << t3; cout << endl; // How does setw work?
32: Pause(0);
34: while (!(t3 == t2)) // Overloaded == operator
35: {
36: cout << t3; cout << endl;
37: t3.Increment();
38: }
39: if (t3 == t2)
40: cout << "\nt2: " << t2 << "\nt3: " << t3 << endl;
41: Pause(0);
43: Time t4 = t1 + t2; // Overloaded + operator used in initialization
44: Time t5;
45: t5 = t1 + t3; // Overloaded + operator used in assignment
46: cout << t4; cout << endl;
47: cout << t5; cout << endl;
48: Pause(0);
50: ofstream outFile("times.tmp"); // Initialize outFile object with specific file
51: outFile << t1 << endl // Overloaded << operator used with outFile
52: << t2 << endl // Cascading of output values, i.e.,
53: << t3 << endl // ... << value1 << value2 << value3 << ...
54: << t4 << endl
55: << t5 << endl;
56: outFile.close();
58: ifstream inFile("times.tmp");
59: Time t;
60:
61: inFile >> t; // Overloaded >> operator used with inFile
62: while (inFile)
63: {
64: cout << t << endl;
65: inFile >> t;
66: }
67: inFile.close();
68: Pause(0);
70: inFile.open("times.tmp");
71: // Cascading of input values (... >> value1 >> value2 >> value3 ...):
72: inFile >> t1 >> t2 >> t3 >> t4 >> t5;
73: inFile.close();
74: cout << t1 << " "
75: << t2 << " "
76: << t3 << " "
77: << t4 << " "
78: << t5 << endl;
80: return 0;
81: }