C++| time2=time1.fun2(); 值的问题

#include<iostream>
using namespace std;

class Time {
public:
    Time() { minute = 0; sec = 0; }
    Time(int m, int s) :minute(m), sec(s) {};
    Time fun1(); Time fun2();
    void display() { cout << minute << ":" << sec<<endl; }
private:
    int minute;
    int sec;
};

Time Time::fun1() {
    if (++sec >= 60) {
        sec -= 60;
        ++minute;
    }
    return *this;
}
Time Time::fun2() {
    Time temp(*this);
    sec++;
    if (sec >= 60) {
        sec -= 60;
        ++minute;
    }
    return temp;
}

int main() {
    Time time1(34, 59), time2;
    cout << "time1:   ";time1.display();
    time1.fun1();
    cout << "++time1:"; time1.display();
    time2 = time1.fun2(); cout << "time1++:   "; time1.display();
    cout << "time2:  "; time2.display();

    system("pause");
    return 0;
}

//输出
//      time1:   34 : 59
//    ++time1 : 35 : 0
//    time1++ : 35 : 1
//    time2 : 35 : 0

为什么time2.display(); 输出 35:0

#笔试题目#
全部评论
Time temp(*this);后面的语句都不影响返回的temp,只影响对象time1的成员。
点赞 回复 分享
发布于 2018-12-11 23:41

相关推荐

尊尼获获:闺蜜在哪?
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务