如何使课程持续时间存储时间长度

How can I make a class duration to store time length?

本文关键字:时间 存储 持续时间 何使课      更新时间:2023-10-16

我正在尝试编写一个带有3个属性和某些构造函数的类,以及以下方法: set (h, m, s)Double getHousrs () operator correctTime()。更改例如1:76:84至2:13:13

当前代码

#include <iostream>
using namespace std;
class duration {
 public:
  duration(int h, int m, int s)
  :hour (h), minutes (m), seconds (s);
  {}
  void printDate()
  {
   cout << hour<< ":" << minutes << ":" << seconds << endl;
  }
  double getHours() {
        return hours;
    }
    double getSeconds() {
        return seconds;
    }
 private:
  int hour;
  int minutes;
  int seconds;
  duration operator+(duration &obj)
  { }
};
int main()
{
    duration obj;
    return 0;
}

解决问题的解决方案是将值添加在一起,这些值可以像这样最有效地完成,我还修复了您类中您遇到的所有其他错误:

#include <iostream>
using namespace std;
class duration {
 public:
  duration(int h, int m, int s)
  :hour (h), minute (m), second (s)
  {}
  void printDate()
  {
   cout << hour<< ":" << minute << ":" << second << endl;
  }
  double getHours() {
        return hour;
    }
    double getSeconds() {
        return second;
    }
  duration operator + (const duration& other)
    {
    duration temp(0, 0, 0);
    temp.second = (other.second+second)%60;
    temp.minute = ((other.second + second)/60 + other.minute + minute)%60;
    temp.hour = ((other.minute+minute)/60 + other.hour + hour)%60;
    return temp;
    }   
 private:
  int hour;
  int minute;
  int second;
};
int main()
{
    duration obj(3, 5, 10);
    duration obj2(4, 55, 40);
    duration temp = obj + obj2;
    temp.printDate();
    return 0;
}