题解 | #重载小于号#

重载小于号

https://www.nowcoder.com/practice/e717e94202304f34b7ed95b2d31fce6b

#include <iostream>
using namespace std;

class Time {

  public:
    int hours;      // 小时
    int minutes;    // 分钟

    Time() {
        hours = 0;
        minutes = 0;
    }

    Time(int h, int m) {
        this->hours = h;
        this->minutes = m;
    }

    void show() {
        cout << hours << " " << minutes << endl;
    }

    // write your code here......
    bool operator<(Time t) {
        if (hours < t.hours) {
            return true;
        } else if (hours > t.hours) {
            return false;
        } else {
            if (minutes < t.minutes) {
                return true;
            } else {
                return false;
            }
        }
    }

};

int main() {
    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(6, 6);

    if (t1 < t2) cout << "yes";
    else cout << "no";
    return 0;
}

全部评论

相关推荐

头像
04-09 14:29
Java
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务