I have a question about overloading operators my program works like this but I was told about overloading operators and I didnt understand it...at all I know I can use t1 and t2 but unless im not declaring them correctly its not working...at all
Please edit your post and make sure your code is [code]between code tags[/code] so that it has syntax highlighting and line numbers, as well as proper indentation.
What specifically is confusing to you about operator overloading?
I don't think I'm understanding the syntax.basically the original program added a second to the previous time. I was told about operator overloading, so when I tried it I thought it would be as simple as as int t1 and t2 and cout t1+t2. That didn't work. So I guess my question is am I missing syntax. Also I looked it up on YouTube and I found something about creating a friend function in the class it's self. PS thanks I was wondering how to do that, I'm pretty new to this site.
The compiler doesn't magically know what you want to happen when you write t1 + t2, you have to tell it. The way you tell the compiler anything is by writing code.
An overloaded operator is just a normal function with a special name, and it gets called when you use the operator. For the + operator the name would be operator+.
The + operator has a left hand side and a right hand side. If you define it as a member of the class, the left hand side is this and the right hand side is the single argument to the function. If you define it as a friend function or free function, it takes two arguments where the first is the left hand side and the second is the right hand side.
I tried giving them the get.hours and so forth. I've seen the lhs and rhs around as well I'm guessing that's what you are referring to as left hand side and right hand side? So am I looking at something like std:: operator+//the get.hours,minutes,seconds kind of thing or am I still way off
It seems to me you're over complicating the problem by keeping track of seconds, minutes and hours as separate data members.
I would keep only seconds, then calculate hours, minutes, etc. in terms of the total seconds.
eg:
1 2 3
// in class Clock
int secs;// total seconds is the only data member
void set(int hours, int minutes, int seconds) { secs = seconds + 60*minutes + 3600*hours; }
Just call the set function in the constructor.
Other functions are then:
1 2 3 4
// getter functions for the parts of the time
int hours(){ return (secs/3600)%24; }
int minutes(){ return (secs%3600)/60; }
int seconds(){ return secs%60; }
In operator+ you then just need to add the 2 clocks secs values.
Assuming we have a constructor which takes total seconds (in addition to one which takes hours, minutes and seconds ): Clock( int seconds=0 ): secs(seconds) {}
The overload for + can be this simple: Clock operator+( const Clock& c ) { return Clock( secs + c.secs ); }