Overloading

I have to overload the operator and im having some trouble





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;
}  
What operator are you trying to overload? Where is the code showing how you tried to overload this mysterious operator?
@jib: this is a continuation from http://www.cplusplus.com/forum/general/162049/
closed account (D80DSL3A)
Sorry if I caused confusion with my suggestions.

Do you need functions for setting hours, minutes and seconds individually?
You have a multitude of constructors. Some create ambiguities.

If a Clock was created by this code Clock time(11); should Clock(int seconds = 0) or Clock(int hourValue) be called? The compiler doesn't know, so you get an "ambiguous function call" error.

Can you do with just 2 constructors? One where all 3 time parts are given:
Clock( int hours, int minutes, int seconds );
and one where the total seconds is given:
Clock( int seconds=0 );

I presently have this for all of the Clock class functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Clock
{
private:
    int secs;
public:
    Clock( int hours, int minutes, int seconds ) { set(hours, minutes, seconds); }
    Clock( int seconds=0 ): secs(seconds) {}
    int getHours()const { return (secs/3600)%24; }
    int getMinutes()const { return (secs%3600)/60; }
    int getSeconds()const { return secs%60; }
    int totalSeconds()const { return secs; }
    void tick() { ++secs; }
    void set(int hours, int minutes, int seconds) { secs = seconds + 60*minutes + 3600*hours; }
    void display()const { cout << getHours() << ':' << getMinutes() << ':' << getSeconds() << '\n'; }
    Clock operator+( const Clock& c )const { return Clock( secs + c.secs ); }
};

Are any other functions necessary?

Here's an example of the use of Clocks in the main()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    Clock c1(2,27,15);
    cout << c1.totalSeconds() << '\n';// 8835 seconds since the start of the day
    c1.display();

    Clock c2(4,40,30);// create a 2nd clock
    c2.display();

    Clock c3 = c1 + c2;//Create a 3rd Clock. add the clock readings of the 1st two Clocks
    c3.display();

    c1.set(23,59,58);// reset the reading for Clock 1
    c1.display();
    c1.tick();// add 1 second
    c1.display();
    c1.tick();// add one second
    c1.display();

This output is produced:
8835
2:27:15
4:40:30
7:7:45
23:59:58
23:59:59
0:0:0

What else do you need to be able to do with Clocks?

@LB. Thanks for providing the thread continuity.
Last edited on
It just has to add up the times given...I'm understanding it a little bit better now(after an extensive Google search) I decided to give the user an option to input that seemed to work out better for my understanding
Topic archived. No new replies allowed.