I am going to input the question directly from my assignment sheet, i'm not looking for the exact code to the whole problem, I do need help figuring out overloaded operators though so example syntax of a overloaded + math operator would be beneficial. here is the problem...
* denotes complete as far as i can tell.
***The Time class includes two integer member variables (Hour and Minute), the mutator functions setHour and setMinute, and the accessor functions getHour and getMinute. These functions should be inline.
***The time class should also include two constructors and one destructor. The default constructor should initialize hour and minute to 0 and display the message "Hello from the Default Constructor". The second constructor should assign a value to hour and minute. The destructor should display the message "Goodbye from the Destructor" These functions should also be inline.
Now in order to add and subtract hour and minute amounts, add an operator + function that overloads the standard + math operator and an operator - function that overloads the standard - math operator. ***You all need to add a simplify function which checks for values in minute greater than 59 or less then 0 and handles them accordingly to hour. Example 2 in hour and 65 in min would give 3 in hour and 5 in min. These functions should not be inline.
The main program will create three Time objects(time1, time2, time3) The user will be prompted to enter two times which will be stored in the first and second objects. The first and second objects will then be added and placed in the third object then displayed, then subtracted put in the third object then displayed. Use the default constructor with the third object declaration and the overloaded constructor with the first and second object declarations.
* End Of Assigment*****
* Start Code*****
#include<iostream>
#include<cstdlib>
using namespace std;
class TIME
{
private:
int min;
int hour;
public:
void tickTime();
void simplify();
TIME operator +();
TIME operator -();
TIME()
{
min = 0;
hour = 0;
cout << "Hello!!! From constructor\n";
}
TIME(int h, int m)
{
hour = h;
min = m;
}
~TIME()
{
cout << "Goodbye!!! From destructor.\n";
system("pause");
}
void setMin ( int m )
{
min = m;
}
void setHour (int h)
{
hour = h;
}
int getMin() const
{
return min;
}
int getHour() const
{
return hour;
}
};
void TIME :: tickTime()
{
min++;
if (min>59)
{
min=00;
hour++;
if (hour>23)
hour = 00;
}
return;
}
void TIME :: simplify()
{
{
if (min>=60)
{
hour += (min/60);
min = min % 60;
}
else if (min < 0)
{
hour -= ((abs(min) / 60) + 1);
min = 60 - (abs(min) % 60);
}
if (hour>23)
hour = 00;
}
return;
}
TIME TIME:: operator+()
{
return;
}
TIME TIME:: operator-()
{
return;
}
int main()
{
int min, hour;
TIME t;
int i, h, m;
cout << "Enter Hour: ";
cin >> h;
cout << "Enter Minute: ";
cin >> m;
t.setHour(h);
t.setMin(m);
cout << endl << endl;
for (i=1; i<=5; i++)
{
t.simplify();
t.tickTime();
cout << t.getHour() << ":" << t.getMin() << endl << endl;
}
system("pause");
return 0;
}
***End Code***
**Current Sample Output*** Hello!!! From constructor Enter Hour: 9 Enter Minute: 29
9:30
9:31
9:32
9:33
9:34
Press any key to continue . . . Goodbye!!! From destructor. Press any key to continue . . . ***End of Sample Output***
Thank you for the help, I really just dont understand overloading, I think i will be able to figure out the objects and all the other fun stuff though.