For one of my assignments we are given the following code...
Code:
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
////////////////////////////////////////////////////////////////
class Time
{
private:
const float secTime; // seconds to time
int hrs; //1 to 23
int mins; //0 to 59
int secs; //0 to 59
public: //no-arg constructor
Time() : hrs(0), mins(0), secs(0), secTime(60)
{ }
//3-arg constructor
Time(int h, int m, int s) : hrs(h), mins(m), secs(s), secTime(60)
{ }
void display() const //format: 11:59 p.m.
{
if (hrs < 10) cout << '0';
cout << hrs << ':';
if (mins < 10) cout <<'0';
cout << mins <<':' ;
if (secs <10) cout << '0';
cout <<secs;
}
Time operator ++ () // increment prefix
{ return Time (++hrs, ++ mins, ++ secs);}
Time operator ++ (int) // increment postfix
{ return Time (hrs++, mins++, secs++);}
Time operator -- () //prefix
{return Time (--hrs, --mins, --secs);}
Time operator -- (int) //postfix
{return Time (hrs--, mins--, secs--);}
Time ( float seconds ) : secTime(60) //convert from long to Time
{
float fltmin = seconds / secTime;
float flthrs = fltmin / secTime;
hrs = int(flthrs);
float min = secTime * (flthrs - hrs) ;
mins = int(min);
float fltsec = (min - mins) * 100;
secs = int(fltsec);
}
operator float()
{
int convert;
float num;
convert = (hrs * secTime * secTime) + (mins * secTime) + (secs);
num = static_cast<float>(convert);
return num;
}
};
int main()
{
int h, m, s;
long time;
cout << "Enter 24-hour time: \n";
cout << " Hours (0 to 23): "; cin >> h;
cout << " Minutes: "; cin >> m;
cout << " Seconds: "; cin >> s;
Time t(h, m, s); //make a time object t
cout << "You entered: "; //display the t
t.display();
Time t2=t++;
cout << "\n t2 is " ; t2.display(); cout << ". t is "; t.display();
cout <<"\tPostfix addition.";
Time t3=++t;
cout << "\n t3 is " ; t3.display(); cout << ". t is "; t.display();
cout <<"\tPrefix addition";
Time t4=t--;
cout << "\n t4 is " ; t4.display(); cout << ". t is "; t.display();
cout << "\tPostfix subtraction.";
Time t5=--t;
cout << "\n t5 is " ; t5.display(); cout << ". t is "; t.display();
cout << "\tPrefix subtraction.";
getch ();
return 0;
}
Some how when the time goes past 23:59:59 we have to change it to 00:00:00. Then when we use the -- operator the time has to go back to 23:59:59... I understand this is not how time works....but it is an exercise to help us with overloading operators.....if anyone can help that would be great...
Thanks