-2

I am writing a time of day project and part of it is supposed to overload the + operator to add two time objects and return another object as the result. I can't seem to get this down. We have to use a test bed to test it out.

This is what I have for the overload function

Time Time::operator +(const Time & that){
Time temp(*this);
*this =(hour, minute);
temp = add(that.hour, that.minute);
return temp;
}

This is the test I run

Time t24 = (1,1);
Time t25 = (1,1);
t24+t25;
if(t24.getMinute()== 2 && 
   t24.getHour()== 2 )
   good++;
else {  
  Memo1->AppendText(wxString("+ operator fails object. \n"));
  bad++;
}
Sam Mussmann
  • 5,883
  • 2
  • 29
  • 43

1 Answers1

0

I'd overload binary operators like +, -, etc. as free functions (possibly friend of Time class, if you need to access private Time data from these overloads):

Time operator+(const Time& lhs, const Time& rhs) {
    ... build a new Time adding lhs and rhs time values
    return Time(hewHour, newMinute);
}
Mr.C64
  • 41,637
  • 14
  • 86
  • 162