Timeout with ++ operator

Asked 1 weeks ago, Updated 1 weeks ago, 1 views

#include<iostream>
using namespace std;

class Time {
private:
    int hours;
    int minutes;
public:
    Time() : minutes(0) {};
    ~Time() {};
    Time() :hours(0), minutes(0) {}
    Time(int h, int m) : hours{ h }, minutes{ m } {}

    void displayTime() {
        cout << hours << ": " << minutes << "\n";
    }
    Time& operator++() {
        ++minutes;
        return *this;
    }
};
int main() {
    Time t(10, 59);
    cout << t.displayTime() << "\n";
    ++t;
    cout << t.displayTime() << "\n";
    return 0;
}

Why is the cout display time not coming out at the end here?

09:20

09:21

I'm trying to print it out like this

c++

2022-09-20 14:22

1 Answers

If you really don't know what the problem is after looking at the code above, don't study the chapter you're studying, read the textbook from the beginning, type all the examples, and solve all the practice questions in each store and have enough practice.

Please refer to the code below.

#include<iostream>
using namespace std;

class Time {
public:
    Time() :hours{ 0 }, minutes{ 0 } {}
    Time(int h, int m) : hours{ h }, minutes{ m } {}

    void displayTime() const { cout << hours << ':' << minutes << "\n"; }
    Time& operator++() { ++minutes; return *this; }
private:
    int hours;
    int minutes;
};

int main()
{
    Time t(10, 59);
    t.displayTime();
    ++t;
    t.displayTime();
}


2022-09-20 14:22

If you have any answers or tips


© 2022 pinfo. All rights reserved.