如何设置课程持续时间以存储时间长度?

时间:2017-12-10 12:56:42

标签: c++ operators

我正在尝试编写一个具有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;
}

1 个答案:

答案 0 :(得分: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;
}