如何制作 class duration 来存储时间长度?

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

我正在尝试编写一个具有 3 个属性和一些构造函数以及以下方法的 class:set (h, m, s)Double getHousrs () 运算符 + 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;
}

您的问题的解决方案是将这些值相加,这样可以最有效地完成我还修复了您在 class:

中遇到的所有其他错误
#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;
}