class Time { private: int hr, min; public: Time operator+ (Time); Time(int x, in
ID: 3537573 • Letter: C
Question
class Time
{
private: int hr, min;
public: Time operator+ (Time);
Time(int x, int y) {hr =x; min = y; }
void show( ) { cout << hr <<":" << min <<endl; }
};
Time Time::operator+(Time y)
{
Time z(0,0);
z.hr = hr + y.hr;
z.min = min + y.min;
if(z.min > 59)
{ z.hr++; z.min = z.min -60; }
return z;
}
------------------------------ Who can figure out what is going on with this main(), given the above Time definition? ------
void main(void)
{
Time a(2, 50), b(3, 40);
a = a + b;
First, what is an equivalent way of writing "a = a + b; ?"
Explanation / Answer
Time a(2, 50), b(3, 40);
a = a + b;
adding hts 2 +3 = 5
now adding minutes 50+40 = 90 since it is greater than 59
hours++ = 5+1 = 6 minuts = minuts - 60 = 90-60 = 30
a = 6.30
First, what is an equivalent way of writing "a = a + b; ?"
a = a.operator+(b);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.