create a class that simulates an alarm clock. In this class you should *store ti
ID: 3584473 • Letter: C
Question
create a class that simulates an alarm clock. In this class you should *store time in hours, minutes, and seconds. Note if time is am or pm. (hint: you should have separate private members for the alarm and the clock. Do not forget to have a character variable repersenting am or pm) *initialize the clock to a specified time. *allow the clock to increment to the next second (you need to take into account things like if the clock's time is 11:59:59 AM and you increment by a second, the time will be 12:00:00 PM. You may need to consider some iterated if statements.) *set the alarm and have the alarm print "Wake Up" when the set time is reached. (you may wish to create a private function that provides the wished-for print out when the alarm time is reached and the alarm clock is on) *display the present time. *use the class in a program that uses the functions requiring displaying of time and setting of the alarm. Include 2 constructors. One constructor should be the default constructor that will initialize the object to 12:00:00 AM. The second constructor should take parameters for hours, minutes, seconds, and AM/PM. Both constructors will provide the private members with the time. In addition, have both constructors set the alarm clock as off. (you will need a Boolean variable that determines whether the alarm is on or off) . The function or method you use to set the alarm will set the alarm on.Explanation / Answer
The standard C++ library includes a number of functions and structures to assist programmers in dealing with time in their applications. In this tutorial you will create a C++ class called Clock that will serve as a simple wrapper around the time_t and tm structures used by C's time library, and include a simple sample program that will use this Clock class to output the system time to the console.
Other People Are Reading
How to Create a Digital Clock in Java How to Make a Clock in C
Things You'll Need
C++ Compiler
C++ Standard Library
Text editor or C++ IDE
Instructions
1
Create your files. This program will call for three files: a clock.h header file to define the Clock class, a clock.cpp C++ source file to define the implementation of the Clock class, and finally a simple example program using this class in main.cpp.
2
Define the Clock class. Open the clock.h header file and define the structure of the Clock class by entering the following code into it.
#ifndef _CLOCK_H
#define _CLOCK_H
class Clock {
public:
int getHours();
int getMinutes();
int getSeconds();
std::string getTime();
Clock();
private:
};
#endif /* _CLOCK_H */
The class has been defined as having four major functions (besides the constructor Clock()). The getHours, getMinutes, and getSeconds functions will each retrieve a different part of the time from the C++ standard time library, using the current system time as its guide. The getTime() function will format these into a standard HH:MM:SS string, as you are used to seeing on digital clocks.
The ifndef, define, and endif tags are optional; however, it is a good practice to get into using these. When building larger projects, including these tags will ensure that a given header file is loaded into memory only once. This reduces the risk of circular dependency errors, and making a habit of always including them in new header files now will save you many headaches later on down the road.
3
Implement the Clock class. Open your clock.cpp file, and implement all the functions you've defined in your header file by entering the following:
#include
#include
#include "clock.h"
Clock::Clock() {
}
int Clock::getHours() {
time_t seconds = time(NULL);
struct tm *timeinfo = localtime(&seconds);
return timeinfo->tm_hour;
}
int Clock::getMinutes() {
time_t seconds = time(NULL);
struct tm *timeinfo = localtime(&seconds);
return timeinfo->tm_min;
}
int Clock::getSeconds() {
time_t seconds = time(NULL);
struct tm *timeinfo = localtime(&seconds);
return timeinfo->tm_sec;
}
std::string Clock::getTime() {
std::string time;
std::stringstream out;
out << getHours();
time = out.str();
out.str("");
time += ":";
out << getMinutes();
time += out.str();
out.str("");
time += ":";
out << getSeconds();
time += out.str();
return time;
}
Starting at the top is the constructor, Clock(). This is a very simple class, so there is no need to do anything special here, so the constructor is left empty.
Next are the getHours, minutes, and seconds functions. These functions retrieve the local time, measured in seconds, GMT, since January of 1970 on most computers, converts it into the tm structure provided by the C++ libraries, and finally retrieves the hours, minutes, and seconds out of this value in a human-readable form.
Finally, getTime puts these values together in a string with the common format which separates hours, minutes and seconds by colons.
4
Create a simple program to use it. Open your main.cpp file and write the following program into it.
#include
#include "clock.h"
int main (int argc, char * const argv[]) {
Clock *clock = new Clock();
std::cout << clock->getTime();
return 0;
}
This very simple program creates a new clock object, gets the current system time from it, and outputs it to the standard out. Finally it returns 0 to the operating system, to inform the operating system that the program ran successfully with no errors.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.