0

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.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
MjsGoku
  • 3
  • 2
  • 1
    Can you be specific on what exactly is not working and post the code snippets from those parts? – yizzlez May 01 '14 at 01:35
  • 1
    Take a look here: http://stackoverflow.com/questions/4421706/operator-overloading?rq=1 But the problem is I don't know how to help you because I don't know what you **want/expect** – yizzlez May 01 '14 at 01:40
  • I don't know what the syntax for an overloaded operator looks like, so example syntax would help me i believe but like i said, i do not really want my specific problem done for me, i just want some guidance on how professional programmers would go about solving this. – MjsGoku May 01 '14 at 01:44
  • OP: Your concern about guidance is certainly valid. However, computing is merely an abstraction and for the most part is non-deterministic. What this means, you must have an _intent_ as to what you wish *to do* or *not do*. Consequently any code and pointers thereupon based will be limited to this. Why? Operator Overloading is merely a means to an end. TL;DR: **What are you trying to do? Give an example.** – jrd1 May 01 '14 at 02:11
  • Ok to add using an overloaded operator, what should my syntax look like, if i am trying to add two Hours and Mins, what would that look like using an overloaded operator? – MjsGoku May 01 '14 at 02:15
  • Have a read [here](http://en.wikibooks.org/wiki/C%2B%2B_Programming/Operators/Operator_Overloading). Might help you. The point that people are trying to make is that operator overloading in C++ is very, very, VERY generic (well, really in any language that allows it). You're basically just defining a symbol that does some operation in the context of some objects. The ``+`` operator doesn't actually have to do anything remotely resembling addition. That's why they want to know what you're trying to accomplish. – aruisdante May 01 '14 at 03:00

2 Answers2

0

The overload operators to add two TIME objects and subtract a TIME object from another:

  TIME operator+(TIME const& rhs) const;
  TIME operator-(TIME const& rhs) const;

You need the const member function and TIME const& for the argument because when you do:

  TIME c = a + b;

you would like to be able deal with the case where a and b are const objects.

In the implementation:

TIME TIME::operator+(TIME const& rhs) const
{
   TIME res;
   // Compute the resultant
   // ...

   // Return the resultant
   return res;
}

TIME TIME::operator-(TIME const& rhs) const
{
   TIME res;
   // Compute the resultant
   // ...

   // Return the resultant
   return res;
}

Now you can add and subtract instances of TIME.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • And, in general, the mathematical operators should be ``const`` and accept ``const`` because they should not be mutators, though there's no requirement for them to be so. – aruisdante May 01 '14 at 03:02
0

Your implementation will look like this :-

TIME TIME::operator+(TIME const& rhs) const
{
   TIME res;
   res.hour = this->hour + rhs.hour;
   res.min = this->min + rhs.min;
   return res;
}

TIME TIME::operator-(TIME const& rhs) const
{
   TIME res;
   res.hour = this->hour - rhs.hour;
   res.min = this->min - rhs.min;

   return res;
}

where "this" is the object on LHS.