题解 | #加号运算符重载#

加号运算符重载

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

#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......
        Time operator+(Time &s)
        {
            Time a(this->hours+s.hours,this->minutes+s.minutes);
            if(a.minutes>59)
            {
                a.hours++;
                a.minutes-=60;
            }
            return a;
        }

};

int main() {

    int h, m;
    cin >> h;
    cin >> m;

    Time t1(h, m);
    Time t2(2, 20);

    Time t3 = t1 + t2;
    t3.show();
    
    return 0;
}

全部评论

相关推荐

10-13 17:47
门头沟学院 Java
wulala.god:图一那个善我面过,老板网上找的题库面的
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务