class airtime
{
private:
int hours;
int minutes;
public:
airtime() : hours(0), minutes(0){}
airtime(int h, int m) : hours(h), minutes(m){}
void display() const
{
cout << hours << ':' << minutes;
}
void get()
{
char dummy;
cout << "\nВведите время (формат 12:59): ";
cin >> hours >> dummy >> minutes;
}
airtime operator+(const airtime right) const
{
int temph = hours + right.hours;
int tempm = minutes + right.minutes;
if (tempm >= 60)
{
temph++;
tempm -= 60;
}
return airtime(temph, tempm);
}
bool operator==(const airtime& at2) const
{
return (hours == at2.hours) && (minutes == at2.minutes);
}
bool operator<(const airtime& at2) const
{
return (hours < at2.hours) || (hours == at2.hours && minutes < at2.minutes);
}
bool operator!=(const airtime& at2) const
{
return !(*this == at2);
}
bool operator>(const airtime& at2) const
{
return !(*this < at2) && !(*this == at2);
}
};
int main()
{
setlocale(LC_ALL, "Russian");
char answ;
airtime* temp = new airtime;
airtime* sum = new airtime;
list<airtime> airlist;
do
{
(*temp).get();
airlist.push_back(*temp);
cout << "Продолжить (y/n)? ";
cin >> answ;
} while (answ != 'n');
*sum = accumulate(airlist.begin(), airlist.end(), airtime(0, 0), plus <airtime>());
cout << "\nСумма = ";
(*sum).display();
cout << endl;
delete temp;
delete sum;
_getch();
return 0;
}
operator<
для того, чтобы отсортировать значения.<template class T>
T sum3(T t1, T t2, T3) {
return t1 + t2 + t3;
}
sum3(MyClass(), MyClass(), MyClass());
этот код скомпилируется только если вы определите MyClass::operator+.