/*********************************************************************

  Filename:  clock.cpp
  Compiler:  Borland C++     Version 5.1      Summer 1997
  Ira Pohl  Copyright May 1997
  
     This code improves on the existing example.
     That example used different techniques such as member function
     overloading versus friend function overloading for didactic
     purposes. This version is more uniform.

   See pp 221-225 Sections7.4 and 7.5 of Object-Oriented Programming Using C++, Edition 2   
   See pp 79-83  Sections 11.4 nd 11.5 of C++ Distilled
   See pp 224-228 Sections 6.6 and 6.7 of C++ for Pascal Programmers, Edition 2
   See pp 208-212 Sections 6.5 and 6.6 of C++ for Fortran Programmers

*********************************************************************/

//Clock to show overloading   --Modified
//  Title: clock

#include < iostream.h>
//Change to allow negative times.
class clock {
   long int  tot_secs, secs, mins, hours, days;
public:
   clock(long i);        //constructor and conversion
   void print() const;         //formatted printout
   void tick();                //add one second
   clock operator++() { this -> tick(); return(*this); }
   //change to have consistent interface for postfix++
   clock operator++(int) { clock temp = *this; tick(); return temp;}
   void reset (const clock& c);
   //make all binary operators friend
   friend clock operator+(clock c1, clock c2);
   friend clock operator-(clock c1, clock c2);
   friend clock operator*(long m, clock c);
   friend clock operator*(clock c, long m);
   friend ostream& operator<<(ostream& out, const clock& c);
};

inline clock::clock(long i):tot_secs(i)
{
   secs = tot_secs % 60;
   mins = (tot_secs / 60) % 60;
   hours = (tot_secs / 3600) % 24;
   days = tot_secs / 86400;
}

//redone to be concise - use conversion constructor and default =
void clock::tick()
{
   *this = ++tot_secs;
}
//redone to use default operator =
void clock::reset(const clock& c)
{
   *this = c;
}

clock operator+(clock c1, clock c2)
{
   return (c1.tot_secs + c2.tot_secs);
}

clock operator-(clock c1, clock c2)
{
   return (c1.tot_secs - c2.tot_secs);
}

clock operator*(long m, clock c)
{
   return (m * c.tot_secs);
}

clock operator*(clock c, long m)
{
   return (m * c);  //keep tied to above
}
//overload operator<<
ostream& operator<< (ostream& out, const clock& c)
{
   out << c.days << " days, " << c.hours << " hours, "
	   << c.mins << " minutes, " << c.secs << "seconds\n";
   return out;
}

int
main()
{
   clock t1(59), t2(172799);  //min - 1 sec and 2 days - 1 sec
   clock t3(0);
   clock c1(900), c2(400);

   cout << "initial times are\n" << t1 << t2;
   ++t1;  ++t2;          //t1++; t2++ are also possible
   cout << "after one second times are\n" << t1 << t2 ;
   //More tests
   t3 = t1 + t2;
   cout << t3;
   t3 = t1 * 5;
   cout << t3;
   t3 = 6 * t3;
   cout << t3;
   t3 = t3 - t1;
   cout << t3;
   c1.reset(c2);
   c2.reset(100);
   cout << "\nc1 and c2\n" << c1 << c2;
}