Public paste
clock.h
By: timebs | Date: Oct 2 2007 02:19 | Format: C++ | Expires: never | Size: 2.06 KB | Hits: 1173

  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6.  
  7. class clockType
  8. {
  9. public:
  10.     void setTime(int hours, int minutes, int seconds);
  11.     void getTime(int& hours, int& minutes, int& seconds);
  12.     void printTime() const;
  13.     void incrementSeconds();
  14.     void incrementMinutes();
  15.     void incrementHours();
  16.     bool equalTime(const clockType& otherClock) const;
  17.     clockType(int hours, int minutes, int seconds);
  18.     clockType();
  19.        
  20. private:
  21.     int hr;  
  22.     int min;
  23.     int sec;
  24. };
  25.  
  26. void clockType::setTime(int hours, int minutes, int seconds)
  27. {
  28.         if(0 <= hours && hours < 24)
  29.                 hr = hours;
  30.         else
  31.                 hr = 0;
  32.  
  33.         if(0 <= minutes && minutes < 60)
  34.                 min = minutes;
  35.         else
  36.                 min = 0;
  37.  
  38.         if(0 <= seconds && seconds < 60)
  39.                 sec = seconds;
  40.         else
  41.                 sec = 0;
  42. }
  43.  
  44. void clockType::getTime(int& hours, int& minutes, int& seconds)
  45. {
  46.         hours = hr;
  47.         minutes = min;
  48.         seconds = sec;
  49. }
  50.  
  51. void clockType::printTime() const
  52. {
  53.         if(hr < 10)
  54.            cout<<"0";
  55.         cout<<hr<<":";
  56.  
  57.         if(min < 10)
  58.            cout<<"0";
  59.         cout<<min<<":";
  60.  
  61.         if(sec < 10)
  62.            cout<<"0";
  63.         cout<<sec;
  64. }
  65.  
  66. void clockType::incrementHours()
  67. {
  68.         hr++;
  69.         if(hr > 23)
  70.           hr = 0;
  71. }
  72.  
  73. void clockType::incrementMinutes()
  74. {
  75.         min++;
  76.         if(min > 59)
  77.         {
  78.            min = 0;
  79.            incrementHours();
  80.         }
  81. }
  82.  
  83. void clockType::incrementSeconds()
  84. {
  85.     sec++;
  86.         if(sec > 59)
  87.         {
  88.            sec = 0;
  89.            incrementMinutes();
  90.         }
  91. }
  92.  
  93. bool clockType::equalTime(const clockType& otherClock) const
  94. {
  95.    return(hr == otherClock.hr
  96.             && min == otherClock.min
  97.           && sec == otherClock.sec);
  98. }
  99.  
  100. clockType::clockType(int hours, int minutes, int seconds)
  101. {
  102.         setTime(hours, minutes, seconds);
  103. }
  104.  
  105. clockType::clockType()  
  106. {
  107.         setTime(0, 0, 0);
  108. }
  109.  
  110.  
  111.  
  112. class extClockType: public clockType
  113. {
  114.   public:
  115.         int getTimeZone();
  116.         void setTimeZone(char t);
  117.         void setTime(int hours, int minutes, int seconds, char t);
  118.         void getTime(int& hours, int& minutes, int& seconds, char& t);
  119.         void printTime();
  120.         extClockType();
  121.         extClockType(int hours, int minutes, int seconds, char t);
  122.   private:
  123.         char timeZone;
  124. };
  125.  
  126.  
  127.  
  128.  
  129.  
  130.