Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class time {
  6. private:
  7. int hour, minute, sec, millisec;
  8. static int count;
  9. mutable int mutID;
  10. public:
  11. time();
  12. time(int h, int m, int s, int ms);
  13. ~time();
  14. void SetHour(int h);
  15. void SetMinute(int m);
  16. void SetSec(int s);
  17. void SetMillisec(int ms);
  18. void SetMutID(int x) const;
  19. int GetHour() const;
  20. int GetMinute() const;
  21. int GetSec() const;
  22. int GetMillisec() const;
  23. static int GetCount();
  24. int GetMutID() const;
  25. void Print() const;
  26. time(const time& other);
  27. time operator -(time& other);
  28. time operator +(time& other);
  29. time& operator = (const time& other) {
  30. cout << "Вызвался оператор =\n" << this << "\n";
  31. this->hour = other.hour;
  32. this->minute = other.minute;
  33. this->sec = other.sec;
  34. this->millisec = other.millisec;
  35. return *this;
  36. }
  37. };
  38.  
  39. int time::count = 0;
  40.  
  41. time::time() {
  42. cout << "Вызвался конструктор по умолчанию\n";
  43. cout << this << "\n\n";
  44. hour = 0;
  45. minute = 0;
  46. sec = 0;
  47. millisec = 0;
  48. count++;
  49. mutID =count;
  50. }
  51. time::time(int h, int m, int s, int ms) {
  52. cout << "Вызвался конструктор с параматром\n";
  53. cout << this << "\n\n";
  54. hour = h;
  55. minute = m;
  56. sec = s;
  57. millisec = ms;
  58. count++;
  59. mutID = count;
  60. }
  61. time::~time() {
  62. cout << "Вызвался деструктор " << this << "\n";
  63. count--;
  64. }
  65. time::time(const time& other) {
  66. cout << "Вызвался конструктор копирования\n";
  67. cout << this << "\n";
  68. this->hour = other.hour;
  69. this->minute = other.minute;
  70. this->sec = other.sec;
  71. this->millisec = other.millisec;
  72. count++;
  73. mutID = count;
  74. }
  75. time time:: operator -(time& other) {
  76. cout << "Вызвался оператор -\n";
  77. time temp;
  78. int sM1 = 0, sM2 = 0, sM3 = 0;
  79. sM1 = this->hour * 3600000 + this->minute * 60000 + this->sec * 1000 + this->millisec;
  80. sM2 = other.hour * 3600000 + other.minute * 60000 + other.sec * 1000 + other.millisec;
  81. sM3 = abs(sM1 - sM2);
  82. temp.millisec = sM3 % 1000;
  83. sM3 /= 1000;
  84. temp.hour = sM3 / 3600;
  85. while (temp.hour > 23) temp.hour -= 24;
  86. sM3 %= 3600;
  87. temp.minute = sM3 / 60;
  88. while (temp.minute > 59) temp.minute -= 60;
  89. sM3 %= 60;
  90. temp.sec = sM3;
  91. while (temp.sec > 59) temp.sec -= 60;
  92.  
  93. return temp;
  94. }
  95. time time:: operator +(time& other) {
  96. cout << "Вызвался оператор +\n";
  97. time temp;
  98. int sM1 = 0, sM2 = 0, sM3 = 0;
  99. sM1 = this->hour * 3600000 + this->minute * 60000 + this->sec * 1000 + this->millisec;
  100. sM2 = other.hour * 3600000 + other.minute * 60000 + other.sec * 1000 + other.millisec;
  101. sM3 = sM1 + sM2;
  102. temp.millisec = sM3 % 1000;
  103. sM3 /= 1000;
  104. temp.hour = sM3 / 3600;
  105. while (temp.hour > 23) temp.hour -= 24;
  106. sM3 %= 3600;
  107. temp.minute = sM3 / 60;
  108. while (temp.minute > 59) temp.minute -= 60;
  109. sM3 %= 60;
  110. temp.sec = sM3;
  111. while (temp.sec > 59) temp.sec -= 60;
  112. return temp;
  113. }
  114. void time::SetHour(int h) {
  115. hour = h;
  116. }
  117. void time::SetMinute(int m) {
  118. minute = m;
  119. }
  120. void time::SetSec(int s) {
  121. sec = s;
  122. }
  123. void time::SetMillisec(int ms) {
  124. millisec = ms;
  125. }
  126. void time::SetMutID(int x)const{
  127. mutID = x;
  128. }
  129. int time::GetHour() const {
  130. return hour;
  131. }
  132. int time::GetMinute() const {
  133. return minute;
  134. }
  135. int time::GetSec() const {
  136. return sec;
  137. }
  138. int time::GetMillisec() const {
  139. return millisec;
  140. }
  141. int time::GetCount() {
  142. return count;
  143. }
  144. int time::GetMutID() const {
  145. return mutID;
  146. }
  147. void time::Print() const {
  148. cout << "Время: " << GetHour() << ":" << GetMinute() << ":" << GetSec() << ":" << GetMillisec() << "\n\n";
  149. }
  150. int main()
  151. {
  152. setlocale(LC_ALL, "Russian");
  153. int h, m, s, ms;
  154.  
  155. cout << "Введите часы, минуты, секунды и миллисекунды 1 оъекта\n";
  156. cin >> h >> m >> s >> ms;
  157. time to1;
  158. to1.SetHour(h);
  159. to1.SetMinute(m);
  160. to1.SetSec(s);
  161. to1.SetMillisec(ms);
  162.  
  163. cout << "Введите часы, минуты, секунды и миллисекунды 2 оъекта\n";
  164. cin >> h >> m >> s >> ms;
  165. time to2(h, m, s, ms);
  166.  
  167. time to3, to4, to5;
  168. to3 = to1 + to2;
  169. to4 = to1 - to2;
  170. to5 = to1;
  171. const time constto(15, 20, 25, 300);
  172.  
  173. constto.SetMutID(15);
  174. cout<<constto.GetMutID()<<"\n";
  175.  
  176. to1.Print();
  177. to2.Print();
  178. to3.Print();
  179. to4.Print();
  180. to5.Print();
  181. constto.Print();
  182.  
  183. cout << to1.GetMutID() << "\n";
  184. cout << to2.GetMutID() << "\n";
  185. cout << to3.GetMutID() << "\n";
  186. cout << to4.GetMutID() << "\n";
  187. cout << to5.GetMutID() << "\n";
  188. cout << constto.GetMutID() << "\n";
  189. return 0;
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement