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.