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.
#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
#include <iostream>
#include "Time.h"
usingnamespace 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.
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.
#include <iostream>
#include "Time.h"
usingnamespace 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;
}
#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
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.