C++: Creating a class for a stopwatch

  • C/C++
  • Thread starter Jamin2112
  • Start date
  • Tags
    Class
In summary, a stopwatch is an object that gets instantiated, has 3 states, and has 2 public functions: start and stop. The inner workings of this object will subsequently be called by those functions. The laps need to be stored in a linked list or similar structure. Each new lap gets pushed back onto the structure. This calls for a vector, according to Bjarne Stroustrop.
  • #1
Jamin2112
986
12
Today I wanted to use my Windows phone to time myself doing a workout. After realizing that my phone doesn't have a stopwatch, I thought, "Oh, I'll make one." I've never made a Windows phone app before, but Microsoft has some documentation that should guide me later: http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj681687(v=vs.105).aspx.

I'd like to do this as correctly as possible. There will be no fancy algorithms involved, which should make it easier to focus on straightforward implementation.

I'm sure you've all used a stopwatch. A stopwatch has two buttons, a left one and a right one. You push the right one to first get the clock running.

While the clock is running, if you push the right button, you pause the clock, and if you push the left button, you put the stopwatch into the next "lap", i.e. it saves the the time up until now in a previous lap. The number of laps is indefinite.

While the clock is paused, if you push the right button, you start the clock again, and if you push the left button, you've made the current lap your final lap and officially ended your workout/race/whatever.

Now, here's what my programming intuition tells me:

  • This should be a class. A stopwatch, as I've described it, is an object that gets instantiated. So the first thing I should do is write class Stopwatch in Notepad.
  • A stopwatch object is in one of 3 states: it's either unstarted, timing a lap or paused on a lap. So I should have a private variable clock_mode cur_mode; where clock_mode is an enumerator enum clock_mode {unstarted, running, paused};
  • There should be 2 and only 2 public functions: void start() and void stop(). The inner workings of this object will subsequently be called by those functions.
  • The laps need to be stored in a linked list or similar structure. Each new lap gets pushed back onto the structure. This calls for a vector, according to Bjarne Stroustrop: .
  • I will be needing to bring in <ctime.h> or whichever library is the one for time.


Any thoughts so far? I need help gaining a better programming intuition.
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Hopefully this shows you what my gameplan looks like:

Code:
#include <vector>

class Stopwatch
{
    private:
    enum clock_mode {unstarted, running, paused};
    clock_mode cur_mode; 
    struct lap {int lap_num; double time;};
    int lap_count;
    std::vector<lap> laps_vec;

    public: 
    Stopwatch();
    ~Stopwatch();
    void right_button();
    void left_button();
};

Stopwatch::Stopwatch()
{
    cur_mode = unstarted;
    lap_count = 0;
}

Stopwatch::~Stopwatch()
{
    // trivial destructor
}

void Stopwatch::right_button()
{
     switch (cur_mode)
     {
        case unstarted:
            // start lap 1
            ;
        case running:
            // pause
            ;
        case paused:
            // start running again
            ;

     }
}

void Stopwatch::left_button()
{
    switch (cur_mode)
    {
        case running:
            // advance to the next lap
            ;
        case paused:
            // reset
            ;
    {
}
 
  • #3
there is a stopwatch class in he .net framewrok. c++ is on the .net framework. system.diagnostics.stopwatch

https://code.google.com/p/cpp-stopwatch/

not to be opprobrious but isn't asking us to code for you kinda against the rules?
 
  • #4
Superposed_Cat said:
not to be opprobrious but isn't asking us to code for you kinda against the rules?

When did I ask you to code for me?

And what rules are you talking about?
 
  • #5
What is the difference between "unstarted" and "paused"? Clue: the difference is the value of an unrelated elapsed time counter. So, you need only 2 states in your enum.

Your public functions (API) should represent the buttons. That is what a real stopwatch exposes. So your API should expose 2 functions: leftbutton and rightbutton (usually named start/stop and lap on a real device). Otherwise, your API will be unintuitive and anyone using your code will be sad, and whine on proggit about poor API design in modern software.

I don't know what time library you need for a windows phone. Getting time right is one of those platform-specific things and I don't use the Windows platforms. Whatever you pick, you should ensure it has a high resolution clock. You might time the task directly, or ask the operating system to time it for you. I would recommend the latter.
 
  • #6
Use DispatchTimer, it has an event handler named Tick.

http://developer.nokia.com/community/wiki/Implement_timers_in_Windows_Phone
 

1. What is a class in C++?

A class in C++ is a blueprint for creating objects. It contains data members and functions that define the properties and behaviors of the objects that will be created from it.

2. How do you create a class for a stopwatch in C++?

To create a class for a stopwatch in C++, you would use the keyword "class" followed by the name of the class. Within the class, you would define the data members to store the start and end time, as well as functions to start, stop, and reset the stopwatch.

3. What are the benefits of using a class for a stopwatch in C++?

Using a class for a stopwatch in C++ allows for easier organization and management of the stopwatch's properties and functions. It also allows for the creation of multiple stopwatch objects with different start and end times.

4. Can a C++ class for a stopwatch be customized?

Yes, a C++ class for a stopwatch can be customized according to the specific needs of the program. Additional functions or data members can be added, and existing ones can be modified to fit the desired functionality.

5. How do you use a C++ class for a stopwatch in a program?

To use a C++ class for a stopwatch in a program, you would first need to instantiate an object of the class. Then, you can use the object's functions to start, stop, and reset the stopwatch, as well as access its data members to retrieve information such as the elapsed time.

Similar threads

  • Electrical Engineering
Replies
26
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
7
Views
5K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top