如何使课程持续时间存储时间长度
How can I make a class duration to store time length?
我正在尝试编写一个带有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;
}
相关文章:
- 指向(数据)成员的指针作为非类型模板参数,例如具有自动存储持续时间/无链接
- 为什么具有静态存储持续时间的同一内联变量在包含在 VS2017 编译的两个翻译单元中时会构造和销毁两次
- 图问题:找出两个节点是否在每个节点的O(1)时间和O(2)存储中共享同一分支
- C++-需要读取存储在文件(.txt)中的时间戳并正确检索
- 具有静态存储持续时间的常量初始化变量的初始化顺序
- 如何将 time-uuid(存储在 boost uuid 中)转换为时间戳/自纪元以来的时间?
- 使用时间,时间应该如何存储为变量?
- 静态存储持续时间初始化
- 添加两个时间值存储为数据库中的字符串
- 在不同翻译单元中具有静态存储持续时间的依赖非局部常量浮点变量的常量初始化
- 访问和存储/解析性std :: Chrono ::持续时间:: milliseconds(cpprest)时使用什么类型
- 将时间戳存储在纳秒C 中
- 如何在双精度内存储时间
- C++ - 函数中的局部指针变量具有什么类型的存储持续时间以及它们存储在哪里?
- 变量在临时存储中存在多长时间
- 什么是静态存储持续时间的临时对象
- 如何使课程持续时间存储时间长度
- 将当前时间 simTime.dbl() 与以前存储的双精度值进行比较并不总是给出正确的答案......为什么?
- C++ 时间存储time_point表达式必须是可修改的左值
- 尝试使用chrono将时间存储到数组中