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

加号运算符重载

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

#include 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;
    }


    Time operator+(Time& p)
    {
        Time temp;
        temp.hours = this->hours + p.hours;
        temp.minutes = this->minutes + p.minutes;
        if(temp.minutes >= 60)
        {
            temp.hours++;
            temp.minutes -= 60;
        }
        return temp;
    }

    // write your code here......
   

};

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;

}

全部评论

相关推荐

吴offer选手:学到了,下次面试也放张纸在电脑上,不然老是忘记要说哪几个点
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务