/********************************************************************************************
ex1.cpp
Developing the military time class
********************************************************************************************/
#include <iostream>
#include <fstream>
using namespace std;
//class definition
class miltime {
public:
int hour; // 0-23
int minute; // 0-59
int second; // 0-59
};
void setup(miltime& t);
void print(const miltime& t, ostream& out = cout);
void main()
{
miltime mtTest;
setup(mtTest);
print(mtTest); //to the screen
//show printing to another stream besides cout
ofstream fout("c:\\temp\\testout.txt");
print(mtTest, fout);
//Add 30 seconds to it and reprint it
mtTest.second += 30;
print(mtTest);
print(mtTest, fout);
//Add 30 minutes to it and print it
mtTest.minute += 30;
print(mtTest);
print(mtTest, fout);
//Add 30 hours to it and print it
mtTest.hour += 30; //Oops! goes over 23 hours!
print(mtTest);
print(mtTest, fout);
}
//------- Initialize the object
void setup(miltime& t) {
t.hour = 0;
t.minute = 0;
t.second = 0;
}
//------- Print the object in "pretty" format -------
void print(const miltime& t, ostream& out) {
if (t.hour < 10)
out << '0';
out << t.hour << ':';
if (t.minute < 10)
out << '0';
out << t.minute << ':';
if (t.second < 10)
out << '0';
out << t.second << endl;
};