设计一个Time类.cpp 916 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include <iostream>
  2. using namespace std;
  3. class Time {
  4. private:
  5. int hours;
  6. int minutes;
  7. int seconds;
  8. public:
  9. // 构造函数
  10. Time(int h, int m, int s) {
  11. hours = h;
  12. minutes = m;
  13. seconds = s;
  14. }
  15. // 将两个Time对象相加
  16. void addTime(Time t1, Time t2) {
  17. seconds = t1.seconds + t2.seconds;
  18. minutes = t1.minutes + t2.minutes + seconds / 60;
  19. seconds %= 60;
  20. hours = t1.hours + t2.hours + minutes / 60;
  21. minutes %= 60;
  22. }
  23. // 输出时间
  24. void printTime() {
  25. cout << "the result is:" << hours << ":" << minutes << ":" << seconds << endl;
  26. }
  27. };
  28. int main() {
  29. int h1, m1, s1, h2, m2, s2;
  30. cin >> h1 >> m1 >> s1;
  31. cin >> h2 >> m2 >> s2;
  32. Time time1(h1, m1, s1);
  33. Time time2(h2, m2, s2);
  34. Time result(0, 0, 0);
  35. result.addTime(time1, time2);
  36. result.printTime();
  37. return 0;
  38. }