Time class

Hi, one of our assignments in the class is to create a program that uses objects to display hours, minutes and seconds. With these numbers we now have to overload various operators that will increase the seconds/minutes by 1, and decrease them using ++ and --. Im having some trouble with the -- operator as its not working as expected, if I put in 0 minutes, and it decreases the minutes it returns values like 128 minutes. As Im starting out in this I would really appreciate some help.

And then using other operators (> < >= <= == !=) to compare 2 different hours, minutes and seconds and return a bool value if one is more than the other (i.e. h:1 m:5 s:0 vs h:0 m:5 s:0 will return 'true'). I havent gotten to the second part and would appreciate some pointers as Im trying to wrap my head around this whole idea.

Main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include "Time.h"

using namespace std;

int main() {

    int hour1, minute1, second1, hour2, minute2, second2;

    cout << "Enter time A (hh, mm, ss): ";
    cin >> hour1;
    cin >> minute1;
    cin >> second1;
    cout <<endl;

    /*cout << "Enter time B(hh, mm, ss): ";
    cin >> hour2;
    cin >> minute2;
    cin >> second;
    cout <<endl;*/

   Time T1(hour1, minute1, second1);

   ++T1;                    //Increases seconds by 1
   T1.displayTime();

   T1++;                    //Increases minutes by 1
   T1.displayTime();

   --T1;                    //Decreases seconds by 1
   T1.displayTime();

   T1--;                    //Decreases minutes by 1
   T1.displayTime();


   return 0;
}


Time.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#ifndef TIME_H
#define TIME_H

class Time
{
    public:
        Time();
        Time (int h, int m, int s);

        void displayTime();

        Time operator++();
        Time operator++(int);

        Time operator--();
        Time operator--(int);
        /*Time operator>();
        Time operator<();
        Time operator>=();
        Time operator<=();
        Time operator==();
        Time operator!=();*/

    private:
        int hours;
        int minutes;
        int seconds;
};

#endif // TIME_H


Time.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include "Time.h"

using namespace std;

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

Time::Time(int h, int m, int s){
    hours = h;
    minutes = m;
    seconds = s;
}

void Time::displayTime(){
    cout << "Hours: " << hours <<" Minutes: " << minutes << " Seconds: " <<seconds <<endl;
}

Time Time::operator++(){ //Prefix plus seconds
    ++seconds;
    if (minutes >= 60){
        ++hours;
        minutes -= 60;
    }
    if (seconds >= 60){
        ++minutes;
        seconds -= 60;
    }
    return Time(hours, minutes, seconds);
}

Time Time::operator++(int){ //Postfix plus minutes
    Time T(hours, minutes, seconds);

    ++minutes;
    if(minutes >=60){
        ++hours;
        minutes -= 60;
    }
    if (seconds >= 60){
        ++minutes;
        seconds -= 60;
    }
    return T;
}

Time Time::operator--(){ //PREFIX minus seconds

    --seconds;

    if (seconds >= 0){
        --minutes;
        seconds += 59;
    }

    if (minutes >= 0){
        --hours;
        minutes += 59;
    }

    return Time(hours, minutes, seconds);
}

Time Time::operator--(int){ //POSTFIX MINUS minutes
    Time T(hours, minutes, seconds);
    --minutes;

    if (minutes >= 0){
        --hours;
        minutes += 59;
    }
    if (seconds >= 0){
        --minutes;
        seconds += 59;
    }
    return T;
}


/*Time Time::operator>(){

}

Time Time::operator<(){

}

Time Time::operator>=(){

}

Time Time::operator<=(){

}

Time Time::operator==(){

}

Time Time::operator!=(){

}
*/



If you spot any other mistakes, please do let me know.
And thank you so much everyone, this forum has helped me a lot in the past with other issues ive had.
Last edited on
Time is now 1:59:59.

What happens, if you add one second?
1. You increment the second; 1:59:60
2. Minutes is still under 60
3. Seconds==60 => 1:60:00

What did we expect? 2:00:00
What goes wrong?
You check the minutes before you (might) modify the minutes. Too early.


Check the logic for each case.


PS. Current time is 00:00:00. What was it one second ago?
I would suggest that you remove the if on line 24. Instead when seconds exceeds 60 call the minute operator:
1
2
3
4
5
6
7
8
Time &Time::operator++(){ //Prefix plus seconds
    ++seconds;
    if (seconds >= 60){
        seconds -= 60;
        return operator++(0);
    }
    return *this;
}
When you return something (for cascading this operator) you should return a reference to this not a temporary variable.

Further more: I wouldn't expect that seconds is manipulated in the minutes operator.
Thanks guys, I actually made a bit of progress on this.
Heres what I have so far, and it seems that the ++ and -- are working.

Ive moved onto the comparison stages i.e. >> << etc. But Im stuck. How do I use the values that have been created in the different objects in order to compare them and return a bool value? I wrote some basic code of what it would look like, by no means is it correct, was hoping on some pointers in that regard.

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include "Time.h"

using namespace std;

int main() {

    int hour1, minute 1, second1, hour2, minute2, second2;

    cout << "Enter time A(hh, mm, ss): ";
    cin >> hour1;
    cin >> minute1;
    cin >> second1;
    cout <<endl;

    cout << "Enter time B (hh, mm, ss): ";
    cin >> hour2;
    cin >> minute2;
    cin >> second2;
    cout <<endl;

   Time T1(hour1, minute1, second1);

   ++T1;                  
   T1.displayTime();

   T1++;                    
   T1.displayTime();

   --T1;                    
   T1.displayTime();

   T1--;                    
   T1.displayTime();

   //psuedo CODE NOT FINISHED THIS IS THE PART THAT IM HAVING TROUBLE WITH
   Time T2(hour2, minute2, second2);
   T1>>T2;
   Time.compare();

   return 0;
}


Time.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef TIME_H
#define TIME_H

class Time
{
    public:
        Time();
        Time (int h, int m, int s);

        void displayTime();
        int compare(); //THIS IS FOR THE FUNCTiON TAT WILL DO THE ACTUAL COMPARISON

        Time operator++();
        Time operator++(int);

        Time operator--();
        Time operator--(int);

        Time operator>>(); //THIS IS THE OPERATOR FOR COMPARISONS

    private:
        int hours;
        int minutes;
        int seconds;
};

#endif // TIME_H 


and Time.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include "Time.h"

using namespace std;

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

Time::Time(int h, int m, int s){
    hours = h;
    minutes = m;
    seconds = s;
}

void Time::displayTime(){
    cout << "Hours: " << hours <<" Minutes: " << minutes << " Seconds: " <<seconds <<endl;
}

Time Time::operator++(){ //Prefix plus seconds
    ++seconds;
    if (seconds == 60){
        ++minutes;
        seconds = 0;
    }

    if (minutes == 60){
        minutes = 0;
        ++hours;
    }

    return *this;
}

Time Time::operator++(int){ //Postfix plus minutes
    Time T(hours, minutes, seconds);

    ++minutes;

    if(minutes ==60){
        ++hours;
        minutes = 0;
    }
    return T;
}

Time Time::operator--(){ //PREFIX MINUSS seconds
    --seconds;

    if (seconds == 0 || seconds == -1){
        --minutes;
        seconds = 59;
    }

    if (minutes == 0 || minutes == -1){
        --hours;
        minutes = 59;
    }

    return Time(hours, minutes, seconds);
}

Time Time::operator--(int){ //POSTFIX MINUSS minutes
    Time T(hours, minutes, seconds);
    --minutes;

    if (minutes == 0 || minutes == -1){
        --hours;
        minutes = 59;
    }
    if (seconds == 0 || seconds == -1){
        --minutes;
        seconds = 59;
    }
    return T;
}

//psuedo code THIS NEEDS A LOT OF EDITING. 
//How do I get the T1 object hours and T2 object hours and compare them here??

int Time::compare(int){
    int result = T1.hours - T2.hours;
    if (result == 0){
        result = T1.minutes - T2.minutes;
        if(result == 0){
            result = T1.seconds - T2.seconds;
        }
    }
    return result;
}


bool Time Time::operator>(Time const& other) const{
    return compare(other) > 0;
}


I apologise its a bit lengthy, but I thought including the whole code would be beneficial to understand whats been done.

Any help is appreciated
1
2
3
4
    if (seconds == 0 || seconds == -1){
        --minutes;
        seconds = 59;
    }
I don't know. I can't imagine two different input times resulting in the same output time here. Something is definitely wrong.

It would require a full rewrite, but it would be far easier for the class to maintain the total number of elapsed seconds only. Then ++, -- and comparisons are trivial while display() becomes a little more complicated because you have to compute the number of hours, minutes, and seconds.
Topic archived. No new replies allowed.