Operator Overloading

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



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class Clock
{
private:
	int timeHours; 
	int timeMin; 
	int timeSec; 
	int t1;
	int t2;
	int t3;
public:
	Clock();
	Clock(int);
	Clock(int, int);
	Clock(int, int, int);
	int getHours()
	{
		return timeHours;
	}
	int getMinutes()
	{
		return timeMin;
	}
	int getSeconds()
	{
		return timeSec;
	}
	void setHours(int hourValue) {
		if (hourValue >= 0 && hourValue <23) {
			timeHours = hourValue;
		}
		else if (hourValue<0) {
			timeHours = 0;
		}
		else if (hourValue>12) {
			timeHours = 12;
		}
	}
	void setMinutes(int minuteValue) {
		if (minuteValue >= 0 && minuteValue < 60) {
			timeMin = minuteValue;
		}
		else {
			timeMin = 0;
		}
	}
	void setSeconds(int secondValue) {
		if (secondValue >= 0 && secondValue < 60) {
			timeSec = secondValue;
		}
		else {
			timeSec = 0;
		}
	}
	void tick();
	void display();

};




Clock::Clock() {
	setHours(11);
	setMinutes(59);
	setSeconds(59);
}
Clock::Clock(int hourValue)
{
	setHours(hourValue);
}
Clock::Clock(int hourValue, int minuteValue)
{
	setHours(hourValue);
	setMinutes(minuteValue);
}
Clock::Clock(int hourValue, int minuteValue, int secondValue)
{
	setHours(hourValue);
	setMinutes(minuteValue);
	setSeconds(secondValue);
}




void Clock::tick() {
	
	timeSec++;
	
		if (timeSec >= 60)
		{
			timeMin++;
			timeSec = 0;
		}
		if (timeMin >= 60)
		{
			timeHours++;
			timeMin = 0;
		}
		//timeHours((getHours() % 12) + 1);

	}






int main()
{
	Clock time;
	int getSeconds, getMinutes, getHours;
	//int t1, t2;
	

	
	{
		cout << time.getHours << ":" << time.getMinutes << ":" << time.getSeconds << endl;
		time.tick();
		cout << time.getHours <<":"<< time.getMinutes<<":"<<time.getSeconds << endl;
	}
	
	system("pause");
	

	return 0;
Last edited on
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?
Last edited on
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.

See this for more info:
http://www.cplusplus.com/doc/tutorial/templates/
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
closed account (D80DSL3A)
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 ); }

Just some thoughts.
Last edited on
can I still use my
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Clock::tick() {
	
	timeSec++;
	
		if (timeSec >= 60)
		{
			timeMin++;
			timeSec = 0;
		}
		if (timeMin >= 60)
		{
			timeHours++;
			timeMin = 0;
		}
		//timeHours((getHours() % 12) + 1);

	}

this is what I have so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
 #include<iostream>
using namespace std;

class clock
{
	int secs;
	int hours(){ return (secs / 3600) % 24; }
	int minutes(){ return (secs % 3600) / 60; }
	int seconds(){ return secs % 60; }
	void set(int hours, int minutes, int seconds) { secs = seconds + 60 * minutes + 3600 * hours; }
	
	
	
	int getHours()
	{
		return (secs / 3600) % 24;
	}
	int getMinutes()
	{
		return (secs % 3600) / 60;
	}
	int getSeconds()
	{
		return secs % 60;
	}

	void setSeconds(int secondValue)
	{
		if (secondValue >= 0 && secondValue < 60)
 {
			secs = secondValue;
		}
		else
		{
			secs = 0;
		}
	}

};
clock::clock() 
{
	setHours(11);
	setMinutes(59);
	setSeconds(59);
}
clock::clock(int hourValue)
{
	setHours(hourValue);
}
clock::clock(int hourValue, int minuteValue)
{
	setHours(hourValue);
	setMinutes(minuteValue);
}
clock::clock(int hourValue, int minuteValue, int secondValue)
{
	setHours(hourValue);
	setMinutes(minuteValue);
	setSeconds(secondValue);
}

int main()
{
	clock time;
	int seconds;
	clock(int seconds = 0) : secs(seconds) {}
	Clock operator+(const Clock& c) { return Clock(secs + c.secs); }




	system("pause");
	return;
} 
I believe I didnt set the hours and minutes properly im getting errors still
Topic archived. No new replies allowed.