Public paste
clockType.h
By: jazy | Date: Oct 2 2007 19:24 | Format: C++ | Expires: never | Size: 3.67 KB | Hits: 1453

  1. #include <iostream>
  2. #include <string>
  3. #include <cctype>
  4.  
  5.  
  6. using namespace std;
  7.  
  8.  
  9. class clockType
  10. {
  11. public:
  12.     void setTime(int hours, int minutes, int seconds);
  13.     void getTime(int& hours, int& minutes, int& seconds);
  14.     void printTime() const;
  15.     void incrementSeconds();
  16.     void incrementMinutes();
  17.     void incrementHours();
  18.     bool equalTime(const clockType& otherClock) const;
  19.     clockType(int hours, int minutes, int seconds);
  20.     clockType();
  21.     int returnHr() {
  22.       return hr;
  23.     }
  24.     int returnMin() {
  25.       return min;
  26.     }
  27.     int returnSec() {
  28.       return sec;
  29.     }
  30.        
  31. private:
  32.     int hr;  
  33.     int min;
  34.     int sec;
  35. };
  36.  
  37. void clockType::setTime(int hours, int minutes, int s)
  38. {
  39.  
  40.         if(0 <= hours && hours < 24)
  41.                 hr = hours;
  42.         else {
  43.           cout<<"Error with Hour... setting to 0n";
  44.                 hr = 00;
  45.         }
  46.  
  47.         if(0 <= minutes && minutes < 60)
  48.                 min = minutes;
  49.         else
  50.                 min = 00;
  51.  
  52.         if(0 <= s && s < 60) {
  53.                 sec = s;
  54.         }
  55.         else
  56.                 sec = 00;
  57. }
  58.  
  59. void clockType::getTime(int& hours, int& minutes, int& seconds)
  60. {
  61.         hours = hr;
  62.         minutes = min;
  63.         seconds = sec;
  64. }
  65.  
  66. void clockType::printTime() const
  67. {
  68.         if(hr < 10)
  69.            cout<<"0";
  70.         cout<<hr<<":";
  71.  
  72.         if(min < 10)
  73.            cout<<"0";
  74.         cout<<min<<":";
  75.  
  76.         if(sec < 10)
  77.            cout<<"0";
  78.         cout<<sec;
  79. }
  80.  
  81. void clockType::incrementHours()
  82. {
  83.         hr++;
  84.         if(hr > 23)
  85.           hr = 0;
  86. }
  87.  
  88. void clockType::incrementMinutes()
  89. {
  90.         min++;
  91.         if(min > 59)
  92.         {
  93.            min = 0;
  94.            incrementHours();
  95.         }
  96. }
  97.  
  98. void clockType::incrementSeconds()
  99. {
  100.     sec++;
  101.         if(sec > 59)
  102.         {
  103.            sec = 0;
  104.            incrementMinutes();
  105.         }
  106. }
  107.  
  108. bool clockType::equalTime(const clockType& otherClock) const
  109. {
  110.    return(hr == otherClock.hr
  111.             && min == otherClock.min
  112.           && sec == otherClock.sec);
  113. }
  114.  
  115. clockType::clockType(int hours, int minutes, int seconds)
  116. {
  117.         setTime(hours, minutes, seconds);
  118. }
  119.  
  120. clockType::clockType()  
  121. {
  122.   hr = 0;
  123.   min = 0;
  124.   sec = 0;
  125. }
  126.  
  127.  
  128.  
  129. class extClockType: public clockType  {
  130.         string timeZone;
  131.  
  132.  public:
  133.         extClockType();
  134.         void printZone();
  135.         void setZone(char *s);
  136.         friend ostream& operator << (ostream& os, extClockType& s) {
  137.           int h,m,sec;
  138.           s.getTime(h,m,sec);
  139.             return  os<<h<<":"<<m<<":"<<sec<<" "<<s.timeZone<<endl;
  140.          
  141.         }
  142.         friend void operator + (extClockType a, extClockType b) {
  143.           if (a.timeZone != b.timeZone)
  144.             cout<<"Time Zones don't match!!";
  145.          
  146.           cout<<a.returnHr() + b.returnHr();
  147.           cout<<":"<<a.returnMin() + b.returnMin();
  148.           cout<<":"<<a.returnSec() + a.returnSec()<<endl;
  149.         }
  150.         friend bool operator == (extClockType a, extClockType b) {
  151.           if (a.returnHr() == b.returnHr())
  152.             if (a.returnMin() == b.returnMin() )
  153.               if (a.returnSec() == b.returnSec() )
  154.                 if (a.timeZone == b.timeZone)
  155.                   return true;
  156.           return false;
  157.         }
  158.         friend void operator ++ (extClockType &a) {
  159.           a.incrementSeconds();
  160.           cout<<a;
  161.         }
  162.         friend void operator ++ (extClockType &a,int) {
  163.           cout<<a;
  164.           a.incrementSeconds();
  165.         }
  166.         friend istream& operator>>(istream& in, extClockType& a) {
  167.           int m,h,s;
  168.           cout<<"Enter Hour: ";
  169.           in >> h;
  170.           cout<<"Enter Minute: ";
  171.           in >> m;
  172.           cout<<"Enter Seconds: ";
  173.           in >> s;
  174.           a.setTime(h,m,s);
  175.         }
  176.  
  177.  
  178.  
  179. };
  180.  
  181.  
  182. extClockType::extClockType() {
  183.   timeZone = "CT";
  184. }
  185.  
  186.  
  187. void extClockType::printZone()
  188. {
  189.  
  190.         cout<<" "<<timeZone<<endl;
  191. }
  192.  
  193. void extClockType::setZone(char s[2]) {
  194.   // puts(s);
  195.  
  196.   char  *p;
  197.  
  198.   for (p = s; *p != ''; p++)
  199.     *p = (char) toupper(*p);
  200.   //puts(s);
  201.  
  202.   if (s[0] != 'P' || s[0] != 'C' || s[0] != 'M' || s[0] != 'E')
  203.   timeZone = "CT";
  204.   if (s[1] != 'T') {
  205.     timeZone = "CT";
  206.       }
  207.   else {
  208.     timeZone = "";
  209.     timeZone += s[0];
  210.     timeZone += s[1];
  211.   }
  212.  
  213. }
  214.  
  215.