How to Write a C++ Program to Simulate a Digital Clock

Save

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.

Things You'll Need

  • C++ Compiler
  • C++ Standard Library
  • Text editor or C++ IDE
  • 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.

  • 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()

    GO

    int getMinutes()

    GO

    int getSeconds()

    GO

    std::string getTime()

    GO

    Clock()
    GO
    private:
    }
    GO

    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.

  • 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 <time.h>
    include <sstream>
    include \"clock.h\"

    Clock::Clock() {
    }

    int Clock::getHours() {

    time_t seconds = time(NULL)

    GO

    struct tm *timeinfo = localtime(&amp;seconds)

    GO

    return timeinfo->tm_hour

    GO
    }

    int Clock::getMinutes() {

    time_t seconds = time(NULL)

    GO

    struct tm *timeinfo = localtime(&amp;seconds)

    GO

    return timeinfo->tm_min

    GO

    }

    int Clock::getSeconds() {

    time_t seconds = time(NULL)

    GO

    struct tm *timeinfo = localtime(&amp;seconds)

    GO

    return timeinfo->tm_sec

    GO

    }

    std::string Clock::getTime() {
    std::string time
    GO
    std::stringstream out
    GO
    out << getHours()
    GO
    time = out.str()
    GO
    out.str(\"\")
    GO
    time += \":\"
    GO
    out << getMinutes()
    GO
    time += out.str()
    GO
    out.str(\"\")
    GO
    time += \":\"
    GO
    out << getSeconds()
    GO
    time += out.str()
    GO
    return time
    GO

    }

    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.

  • Create a simple program to use it. Open your main.cpp file and write the following program into it.

    include <iostream>
    include \"clock.h\"

    int main (int argc, char const argv[]) {
    Clock
    clock = new Clock()
    GO

    std::cout &lt;&lt; clock->getTime()

    GO

    return 0

    GO
    }

    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 Searches

References

Promoted By Zergnet

Comments

Resources

You May Also Like

  • How to Write a Simple Program in C++

    It's common practice that instruction in C++ programming begins with creating a "Hello world" program. This involves printing a "Hello world" message...

  • How to Use Date and Time in a C++ Program

    Dates and times have frequent usage in C++ programs. Windows programs use several different time formats: System time, local time, file time,...

  • How to Make a Digital Clock

    If third-party software digital clocks bore you, build your own using Microsoft's Hypertext Application (HTA) programming language. Using features provided by Internet...

  • How to Make a Clock in C

    A simple coded clock is useful in many potential applications. The ctime library provide a C programmer with many useful functions that...

  • How to Change the Clock on Mercedes ML350

    The Mercedes M-Class, which includes the ML350, ML550 and ML63, has an advanced driving control system, which is to be expected from...

  • How to Set the Alarm in Windows

    While Windows does not have a built in alarm, there are ways of making your Windows PC function like an alarm clock....

  • How to Set the Analog Time on a Casio HDC 600 Watch

    The Casio HDC-600 is a series of watches. While there are many different sub-models, they are all basically the same. The Casio...

  • How to Set a Digital Stove Clock

    Many stoves, ranges, and ovens come manufactured with digital displays including timers and clocks. Setting the clock can sometimes be confusing because...

  • Scoreboard Alarm Clock Instructions

    Team Sports America offers a variety of scoreboard clocks and alarm clocks. You can purchase these clocks with the logo and colors...

  • Brookstone Digital Desk Clock Instructions

    Brookstone digital desk clocks are easy to program, use and maintain. With a convenient auto-set feature and additional user-friendly functions like an...

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

M
Is DIY in your DNA? Become part of our maker community.
Submit Your Work!