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 the second part is 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 on how to get this started as Im trying to wrap my head around this whole idea logically.
Main.cpp
#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
#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
#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 MINUSS seconds
--seconds;
if (seconds == 0){
--minutes;
seconds += 59;
}
if (minutes == 0){
--hours;
minutes += 59;
}
return Time(hours, minutes, seconds);
}
Time Time::operator--(int){ //POSTFIX MINUSS 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.
So with this the minutes are not subtracting correctly. It seems that it is just taking away from 0, but not adding the necessary seconds back to it (if that makes sense).
Thank you.