Advertisement
Guest User

Untitled

a guest
Oct 30th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. #include "Time.h"
  6.  
  7. ostream& operator<< (ostream &output, const Time &a)
  8. {
  9. output << ((a.hour == 0 || a.hour == 12) ? 12 : a.hour % 12) << ":"
  10. << setfill ('0') << setw(2) << a.minute << (a.hour < 12 ? "AM" : "PM" );
  11.  
  12. return output;
  13. }
  14.  
  15. istream& operator>>(istream &input, Time &a)
  16. {
  17. input >> a.hour;
  18. cin.ignore(1);
  19. input >> a.minute;
  20.  
  21. return input;
  22. }
  23.  
  24. //Constructor function to initialize private data
  25. //remember a constructor is called whenever a new object of
  26. //a class data type is instantiated, if a constructor is not defined the C++ do
  27. //nothing constructor is run, it is better to ALWAYS have your own contructor
  28. //once you have your own constructor the default is no longer available
  29. //Constructors can and should be overloaded
  30. //Constructors cannot be const since they always manipulate private data
  31. Time::Time(int hour, int minute)
  32. {
  33. setTime(hour, minute);
  34. }
  35.  
  36. /*SET FUNCTIONS: Never const since they need to modify private member data*/
  37.  
  38. //setTime function now is set up to enable cascading
  39. Time& Time::setTime(int hour, int minute)
  40. {
  41. setHour(hour);
  42. setMinute(minute);
  43. return *this;
  44. }
  45.  
  46. //setHour function is now set up to enable cascading
  47. Time& Time::setHour(int h)
  48. {
  49. hour = (h >= 0 && h < 24) ? h : 0; //validates hour, if valid set to h, else set to 0
  50. return *this;
  51. }
  52.  
  53. //setMinute function is now set up to enable cascading
  54. Time& Time::setMinute(int m)
  55. {
  56. minute = (m >= 0 && m < 60) ? m : 0; //validates minute, if valid set to m, else set to 0
  57. return *this;
  58. }
  59.  
  60. /*GET FUNCTIONS: Do not modify private member data normally always const*/
  61.  
  62. //get Hour
  63. int Time::getHour() const //must be const since prototype is const
  64. {
  65. return hour;
  66. }
  67.  
  68. //get Minute
  69. int Time::getMinute() const //must be const since prototype is const
  70. {
  71. return minute;
  72. }
  73.  
  74. /*PRINT FUNCTIONS: Normally do not modify private member data so should be const*/
  75.  
  76. void Time::printUniversal()const //must be const since prototype is const
  77. {
  78. cout << setfill('0') << setw(2) << hour << ":"
  79. << setw(2) << minute << endl;
  80. }
  81.  
  82. void Time::printStandard()const //must be const since prototype is const
  83. {
  84. cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":"
  85. << setfill ('0') << setw(2) << minute << (hour < 12 ? "AM" : "PM" )<< endl;
  86. }
  87.  
  88. //void Time::operator= (const Time &a)
  89. //{
  90. // hour = a.hour;
  91. // minute = a.minute;
  92. //}
  93.  
  94. bool Time::operator== (const Time &a)
  95. {
  96. if ( hour == a.getHour() )
  97. {
  98. if( minute == a.getMinute() )
  99. {
  100. return true;
  101. }
  102. }
  103.  
  104. return false;
  105. }
  106.  
  107. bool Time::operator!= (const Time &a)
  108. {
  109. if ( getHour() == a.getHour() )
  110. {
  111. if( getMinute() == a.getMinute() )
  112. {
  113. return false;
  114. }
  115. }
  116.  
  117. return true;
  118. }
  119.  
  120. bool Time::operator<= (const Time &a)
  121. {
  122. if ( getHour() < a.getHour() )
  123. {
  124. return true;
  125. }
  126.  
  127. if( getHour() == a.getHour() )
  128. {
  129. if( getMinute() <= a.getMinute() )
  130. {
  131. return true;
  132. }
  133. }
  134.  
  135. return false;
  136. }
  137.  
  138. bool Time::operator< (const Time &a)
  139. {
  140. if ( getHour() < a.getHour() )
  141. {
  142. return true;
  143. }
  144.  
  145. if( getHour() == a.getHour() )
  146. {
  147. if( getMinute() < a.getMinute() )
  148. {
  149. return true;
  150. }
  151. }
  152.  
  153. return false;
  154. }
  155.  
  156. bool Time::operator>= (const Time &a)
  157. {
  158.  
  159. if ( getHour() > a.getHour() )
  160. {
  161. return true;
  162. }
  163.  
  164. if( getHour() == a.getHour() )
  165. {
  166. if( getMinute() >= a.getMinute() )
  167. {
  168. return true;
  169. }
  170. }
  171.  
  172. return false;
  173.  
  174. }
  175.  
  176. bool Time::operator> (const Time &a)
  177. {
  178. if ( getHour() > a.getHour() )
  179. {
  180. return true;
  181. }
  182.  
  183. if( getHour() == a.getHour() )
  184. {
  185. if( getMinute() > a.getMinute() )
  186. {
  187. return true;
  188. }
  189. }
  190.  
  191. return false;
  192.  
  193. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement