Question: In C++, Implement the following class that represents a clock. Clock - hour: int - minute: int - meridiem: string + Clock() + Clock(hr: int,

In C++, Implement the following class that represents a clock.

Clock

- hour: int

- minute: int

- meridiem: string

+ Clock()

+ Clock(hr: int, min: int, mer: string)

+ setTime(hr: int, min: int, mer: string): void

+ setHour(hr: int): void + setMinute(min: int): void

+ setMeridiem(mer: string): void

+ getHour(): int

+ getMinute(): int

+ getMeridiem(): string

+ void tick()

+ string asString()

+ string asStandard()

Implementation Details:

The default constructor should set the clock to midnight (12:00 am)

Hour must be in the range 1 to 12

Minute must be in the range 0 to 59

Meridiem must be the string am or the string pm

The constructor that accepts a time as parameters and all of the setters (mutators) must perform error checking. If an error is detected, print an appropriate error message and stop the program. The exit() function can be used to stop the program.

tick() increments the minute value and handles any rollover. For example, a clock currently set to 11:59 am would become 12:00 pm after executing tick() .

asString() returns the current time in a format suitable for printing to the screen (i.e. 1:05 pm). Note the leading zero for values of minutes less than 10.

asStandard() returns the current time in 24-hour clock format(i.e. 13:05). Both the hour and minute values should be 2 digit numbers (use a leading zero for values less than 10).

Here are the first few lines of code to get started:

#include

#include

#include

class Clock {

};

Clock::Clock()

{

setTime(12, 0, \"am\");

}

And here is the main() that was given to test this:

int main() {

Clock c;

cout

cout

cout

c.tick();

c.tick();

cout

cout

cout

for (int i = 0; i

c.tick();

cout

cout

cout

cout

// Continue testing constructors and tick()

// Continue testing getters and setters....

return 0;

}

Example Execution

After default constructor:

12:00am 00:00

After 2 ticks:

12:02am

00:02

After 185 more ticks:

3:07am

03:07

After parameter constructor:

11:59am

11:59

After 2 ticks:

12:01pm

12:01

After 185 more ticks:

3:06pm

15:06

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!