Помогите пожалуйста с кодом. home_task_7_overload_oper_wo_c_1592131369.pdf (72.2 КБ)
Не могу понять как реализовать корректировку времени нормально. Вот что я написал:
myTime.cpp
#include "myTime.h"
myTime::myTime(int h, int m, int s)
{
/*if (h < 24 && h >= 0)*/ hours = h;
/*if (m < 60 && m >= 0)*/ minutes = m;
/*if (s < 60 && s >= 0)*/ seconds = s;
if (seconds > 59)
{
seconds -= 60;
minutes++;
}
if (minutes > 59)
{
minutes -= 60;
hours++;
}
if (hours > 23)
{
hours -= 24;
}
}
myTime::myTime():myTime(0,0,0)
{
}
int myTime::getHours() const
{
return hours;
}
void myTime::setHours(int h)
{
myTime::hours = h;
}
int myTime::getMinutes() const
{
return minutes;
}
void myTime::setMinutes(int m)
{
myTime::minutes = m;
}
int myTime::getSeconds() const
{
return seconds;
}
void myTime::setSeconds(int s)
{
myTime::seconds = s;
}
bool myTime::operator==(const myTime& obj) const
{
return hours==obj.hours&&minutes==obj.minutes&&seconds==obj.seconds;
}
bool myTime::operator!=(const myTime& obj) const
{
return !(*this == obj);
}
bool myTime::operator>(const myTime& obj) const
{
return (hours * 3600 + minutes * 60 + seconds) >
(obj.hours * 3600 + obj.minutes * 60 + obj.seconds);
}
bool myTime::operator<(const myTime& obj) const
{
return (hours * 3600 + minutes * 60 + seconds) <
(obj.hours * 3600 + obj.minutes * 60 + obj.seconds);
}
bool myTime::operator>=(const myTime& obj) const
{
return (hours * 3600 + minutes * 60 + seconds) >=
(obj.hours * 3600 + obj.minutes * 60 + obj.seconds);
}
bool myTime::operator<=(const myTime& obj) const
{
return (hours * 3600 + minutes * 60 + seconds) <=
(obj.hours * 3600 + obj.minutes * 60 + obj.seconds);
}
myTime myTime::operator+(const myTime& obj) const
{
return myTime(hours+obj.hours,minutes+obj.minutes,seconds+obj.seconds);
}
myTime myTime::operator+(int val) const
{
return myTime(hours, minutes, seconds+val);
}
myTime myTime::operator-(const myTime& obj) const
{
return myTime(hours - obj.hours, minutes - obj.minutes, seconds - obj.seconds);
}
myTime myTime::operator-(int val) const
{
return myTime(hours, minutes, seconds - val);
}
myTime& myTime::operator++(int)
{
myTime tmp(*this);
this->seconds++;
return *this;
}
myTime& myTime::operator--(int)
{
myTime tmp(*this);
this->seconds--;
return *this;
}
void myTime::Show() const
{
if (hours > 9)
cout << hours;
else
cout << '0' << hours;
cout << ':';
if (minutes > 9)
cout << minutes;
else
cout << '0' << minutes;
cout << ':';
if (seconds > 9)
cout << seconds;
else
cout << '0' << seconds;
cout << endl;
}
main.cpp
#include"myTime.h"
int main()
{
myTime a(22, 9, 1);
a.Show();
myTime b(22, 59, 12);
b.Show();
cout << (a == b) << endl;
cout << (a != b) << endl;
cout << (a >= b) << endl;
myTime c = a + b;
c.Show();
myTime d = c + 120;
d.Show();
cout << (a > b) << endl;
a--;
a.Show();
}