p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

c_plus_plus_programming thread: C++ Class for a alarm clock


Message #1 by bonds_girl26@y... on Fri, 1 Feb 2002 03:54:45
I hope someone can help me with this. I'm kinda stumped on it. I want to 

make a class to represent an alarm clock. It should include at least 2 

intialization operations: 1 to provide default values and the other to set 

the data to client-supplied values. It should also include operations to 

display the current time and the alarm time, change the current time and 

alarm time, as well as a funtion for on/off.  
Message #2 by "Sean Lamb" <sean@f...> on Fri, 1 Feb 2002 08:08:44 -0600
'Twas brillig Thursday 31 January 2002 21:54, when you scrobe:

> I hope someone can help me with this. I'm kinda stumped on it. I want to

> make a class to represent an alarm clock. It should include at least 2

> intialization operations: 1 to provide default values and the other to set

> the data to client-supplied values. It should also include operations to

> display the current time and the alarm time, change the current time and

> alarm time, as well as a funtion for on/off.



The first step I always take in developing a new class is to define the data 

that it needs to store and the functions that it needs to operate.  From your 

description, you've already identified the following:



   Functions required by spec (usually in public:):

      current and alarm time constructor

      set current and alarm time

      display current time

      display alarm time

      turn alarm on

      turn alarm off



   Data storage needed (usually in either protected: or private:):

      current time

      alarm time

      alarm status



I would also recommend including functions for a default constructor, 

destructor and a function to sound the alarm bell.  If you don't inlcude a 

default constructor or a destructor, the compiler will make one for you, but 

you can't trust the data that is stored in the data members when you use the 

default constructor, and if you're dynamically allocating memory, then you 

need to deallocate the memory in the destructor.  The third recommended 

function is there because an alarm clock isn't very useful if the alarm 

doesn't sound.



Most clocks that I've seen store the times as the number of seconds since a 

specific time.  In this case, you can probably get away with storing the 

number of seconds away from midnight, so the current time and alarm time data 

members would be some form of long integer.  The alarm status data member 

could probably be done with a boolean (on or off).



The next step is to write the code.  HTH!



-- 

Sean Lamb, Software Engineer - sean@f...

         while( ) { s/$badcode/$goodcode/g; }

"A day without laughter is a day wasted." -- Groucho Marx

Message #3 by bonds_girl26@y... on Fri, 1 Feb 2002 18:25:54
Do you think you could help with the code?



April





> 'Twas brillig Thursday 31 January 2002 21:54, when you scrobe:

> > I hope someone can help me with this. I'm kinda stumped on it. I want 

to

> > make a class to represent an alarm clock. It should include at least 2

> > intialization operations: 1 to provide default values and the other to 

set

> > the data to client-supplied values. It should also include operations 

to

> > display the current time and the alarm time, change the current time 

and

> > alarm time, as well as a funtion for on/off.

> 

> The first step I always take in developing a new class is to define the 

data 

> that it needs to store and the functions that it needs to operate.  From 

your 

> description, you've already identified the following:

> 

>    Functions required by spec (usually in public:):

>       current and alarm time constructor

>       set current and alarm time

>       display current time

>       display alarm time

>       turn alarm on

>       turn alarm off

> 

>    Data storage needed (usually in either protected: or private:):

>       current time

>       alarm time

>       alarm status

> 

> I would also recommend including functions for a default constructor, 

> destructor and a function to sound the alarm bell.  If you don't inlcude 

a 

> default constructor or a destructor, the compiler will make one for you, 

but 

> you can't trust the data that is stored in the data members when you use 

the 

> default constructor, and if you're dynamically allocating memory, then 

you 

> need to deallocate the memory in the destructor.  The third recommended 

> function is there because an alarm clock isn't very useful if the alarm 

> doesn't sound.

> 

> Most clocks that I've seen store the times as the number of seconds 

since a 

> specific time.  In this case, you can probably get away with storing the 

> number of seconds away from midnight, so the current time and alarm time 

data 

> members would be some form of long integer.  The alarm status data 

member 

> could probably be done with a boolean (on or off).

> 

> The next step is to write the code.  HTH!

> 

> -- 

> Sean Lamb, Software Engineer - sean@f...

>          while( ) { s/$badcode/$goodcode/g; }

> "A day without laughter is a day wasted." -- Groucho Marx

Message #4 by "Tomas Palmer" <tomasp@g...> on Sat, 2 Feb 2002 11:36:51 -0800
Sounds like a homework assignment.

Isolate the current time data from both the display and the alarm

functionality. This way you can set the time at any time and the display

function will just use the time data without caring what it is set to. A

similar function can be used to display the alarm time. Use the same

format to store the data.

Now start a thread that wakes up every 1 minute (or any granularity you

need) and update the current time. If the alarm has been set, compare

the alarm time to the current time and if they are the same. If they are

the same call an alarm member function. If an alarm isn't set or it

isn't time to alarm then let the thread go back to sleep.

This will allow the user to change the time, set the alarm and turn the

alarm on with simple functions. To during setting of the data variables

make sure that you use a synchronization object while the data is being

updated.

Have fun.



Clear Skies....PLEASE!

Tom=E1s

www.littlebear-observatory.com

www.cosmiccontrols.com

www.i-codesmith.com





-----Original Message-----

From: bonds_girl26@y... [mailto:bonds_girl26@y...]

Sent: Friday, February 01, 2002 3:55 AM

To: C++_Programming

Subject: [c_plus_plus_programming] C++ Class for a alarm clock



I hope someone can help me with this. I'm kinda stumped on it. I want to



make a class to represent an alarm clock. It should include at least 2

intialization operations: 1 to provide default values and the other to

set

the data to client-supplied values. It should also include operations to



display the current time and the alarm time, change the current time and



alarm time, as well as a funtion for on/off. 




$subst('Email.Unsub').




  Return to Index