对于时间类的重载

一:重载大于 小于 等于用bool类型

1:重载大于号(以月和天做对象)

bool operator>(const Time&t)
{
    if(month>t.month)
    return true;
    if(month==t.month&&day>t.day)
    return true;
    return false;
}

2:重载小于号(以月和天做对象)

bool operator<(const Time&t)
{
    if(month<t.month)
    return true;
    if(month==t.month&&day<t.day)
    return true;
    return false;
}

3:重载等于号(月份和天做对象)

bool operator=(const Time&t)
{
    if (day==t.day&&month==t.month)
    return true;
    return false;
}

二:重载减号 用int 类型

以小时和分钟为对象,容易计算

int operator-(const Time&t)
{
    int a=0;
    if(hour>t.hour)
    a=a+(hour-t.hour)*60+minuth-t.minute;
    if(hour==t.hour&&minute>t.minute)
    a=a+minute-t.minute;
    return a;
}

三:重载输入输出

输出:

friend ostream&operator<<(ostream&os,const Time&t)
{
    os<<t.day<<endl;
    os<<t.month<endl;
    return os;
}

输入:

friend istream&operator>>(istream&is,Time&t)
{
    is>>t.day;
    is>>t.month;
    return is;
}


猜你喜欢

转载自blog.csdn.net/sdau_20171819/article/details/80942467