Hello friends...I'm having confusion about binary operator overloading in the following program.
The question goes like this.

Qn. Create a class time with two member variables as hours and minutes of integer type,write default,parameterized and copy constructor. Overload necessary operators to compute T3=5+T1+T2, where T1,T2 and T3 are time objects. And the constant 5 is to be added to 'hours' member only.

For this i've created two friend function to overload the operators and the code is as given below. But its not working, It would be very much helpful if u could provide me with the logic behind this problem and what has gone wrong in the following piece of code:

#include<iostream>
class time
{
int hrs,min;
public:
void input()
{
cout<<"\n Hours:";
cin>>hrs;
cout<<"\n Minutes:";
cin>>min;
}
void output()
{
cout<<"\n Hours:"<<hrs;
cout<<"\n Minutes:"<<min;
}
friend time operator +(time x,time y);
friend time operator ++(time r,int p);
};


time operator+(time x,time y)
{
time temp;
temp.hrs=x.hrs+y.hrs;
temp.min=x.min+y.min;
return temp;
}

time operator ++(time r,int p)
{
time temp;
temp.hrs=p+r.hrs;
temp.min=r.min;
return temp;
}

int main(void)
{
time t1,t2,t3,t;
t1.input();
t2.input();
t=t1+t2;
t3=5+t;
t3.output();
}


Thanks in advance.

This article has been dead for over six months. Start a new discussion instead.