Advertisement
Guest User

Untitled

a guest
May 6th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip> //for setw()
  3. using namespace std;
  4. ////////////////////////////////////////////////////////////////
  5. class Time
  6. {
  7. private:
  8. int hrs, mins, secs;
  9. public:
  10. Time() //no-arg constructor
  11. {
  12. hrs = mins = secs = 0;
  13. }
  14. //--------------------------------------------------------------
  15. Time(int h, int m, int s) //3-arg constructor
  16. {
  17. hrs = h; mins = m; secs = s;
  18. }
  19. //--------------------------------------------------------------
  20. void display() //format 11:59:59
  21. {
  22. cout.fill('0'); //with leading zeros
  23. cout << hrs
  24. << ':' << setw(2) << mins
  25. << ':' << setw(2) << secs;
  26. }
  27.  
  28. Time operator +(const Time &s){
  29. int newh, newm, news;
  30. news = secs + s.secs;
  31. newm = mins + s.mins;
  32. newh = hrs + s.hrs;
  33. while (news >= 60){
  34. newm++;
  35. news -= 60;
  36. }
  37. while (newm >= 60){
  38. newh++;
  39. newm -= 60;
  40. }
  41. if (newh >= 24){
  42. newh = newh % 24;
  43. }
  44. return Time(newh, newm, news);
  45. }
  46.  
  47. Time& operator --(){
  48. secs--;
  49. if (secs < 0){
  50. secs += 60;
  51. mins--;
  52. }
  53. if (mins < 0){
  54. mins += 60;
  55. hrs--;
  56. }
  57. if (hrs < 0){
  58. hrs += 24;
  59. }
  60. return *this;
  61. }
  62.  
  63. Time& operator ++(){
  64. secs++;
  65. if (secs >= 60){
  66. secs -= 60;
  67. mins++;
  68. }
  69. if (mins >= 60){
  70. mins -= 60;
  71. hrs++;
  72. }
  73. if (hrs >= 24){
  74. hrs -= 24;
  75. }
  76. return *this;
  77. }
  78.  
  79. Time operator --(int){
  80. Time Pom = *this;
  81. secs--;
  82. if (secs < 0){
  83. secs += 60;
  84. mins--;
  85. }
  86. if (mins < 0){
  87. mins += 60;
  88. hrs--;
  89. }
  90. if (hrs < 0){
  91. hrs += 24;
  92. }
  93. return Pom;
  94. }
  95.  
  96. Time operator ++(int){
  97. Time Pom = *this;
  98. secs++;
  99. if (secs >= 60){
  100. secs -= 60;
  101. mins++;
  102. }
  103. if (mins >= 60){
  104. mins -= 60;
  105. hrs++;
  106. }
  107. if (hrs >= 24){
  108. hrs -= 24;
  109. }
  110. return Pom;
  111. }
  112.  
  113. }; //end class time
  114. ////////////////////////////////////////////////////////////////
  115. int main()
  116. {
  117.  
  118. Time time1(5, 59, 59); //creates and initialze
  119. Time time2(4, 0, 1); // two times
  120. Time time3,time4; //create another time
  121. time3 = time1 + time2; //add two times
  122. cout << "\nSum = "; time3.display(); //display result
  123. time4 = --time3;
  124. cout << "\nPrefix decr time4 = "; time4.display();
  125. cout << "\nPrefix decr time3 = "; time3.display();
  126. time4 = ++time3;
  127. cout << "\nPrefix incr time4 = "; time4.display();
  128. cout << "\nPrefix incr time3 = "; time3.display();
  129. cout << "\nNo change = "; time3.display();
  130. time4 = time3--;
  131. cout << "\nPostfix decr time4 = "; time4.display();
  132. cout << "\nPostfix decr time3 = "; time3.display();
  133. time4 = time3++;
  134. cout << "\nPostfix incr time4 = "; time4.display();
  135. cout << "\nPostfix incr time3 = "; time3.display();
  136. cout << "\nNo change = "; time3.display();
  137. cout << endl;
  138. return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement