C++: Creating a class for a stopwatch

  • Context: C/C++ 
  • Thread starter Thread starter Jamin2112
  • Start date Start date
  • Tags Tags
    Class
Click For Summary

Discussion Overview

The discussion revolves around creating a C++ class for a stopwatch application intended for Windows phones. Participants explore the design and implementation of the class, including its state management and functionality, while considering platform-specific requirements.

Discussion Character

  • Exploratory
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant outlines a basic structure for the Stopwatch class, including states (unstarted, running, paused) and public functions (start and stop).
  • Another participant suggests that the distinction between "unstarted" and "paused" may be unnecessary, proposing a simplified state model with only two states.
  • Concerns are raised about the appropriateness of asking for coding help, with one participant questioning the rules of the forum regarding such requests.
  • A suggestion is made to use the .NET framework's existing Stopwatch class, which could simplify the implementation process.
  • Another participant recommends using a high-resolution clock for timing, emphasizing the importance of accurate time measurement in the application.
  • A specific suggestion is made to utilize DispatchTimer with an event handler for timing in the Windows Phone environment.

Areas of Agreement / Disagreement

Participants express differing opinions on the design of the Stopwatch class, particularly regarding the number of states and the naming of public functions. There is no consensus on the best approach, and multiple viewpoints remain active in the discussion.

Contextual Notes

Participants mention platform-specific considerations for timing functionality, indicating that the choice of time library may depend on the Windows Phone environment.

Jamin2112
Messages
973
Reaction score
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
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
            ;
    {
}
 
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?
 
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?
 
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.
 
Use DispatchTimer, it has an event handler named Tick.

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

Similar threads

  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
4
Views
3K
  • · Replies 26 ·
Replies
26
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 33 ·
2
Replies
33
Views
7K
Replies
4
Views
3K
  • · Replies 75 ·
3
Replies
75
Views
6K
Replies
3
Views
4K