I am new to C++ and need help in writing a C++ program to design, implement and
ID: 3691759 • Letter: I
Question
I am new to C++ and need help in writing a C++ program to design, implement and test a countdown timer class named Timer. This class mimics a real-world timer by counting off seconds, starting from the initial value. When the timer reaches zero, it beeps (by sending the alert character, ‘’ to the standard output device). Some appropriate operations might be the following: ---- Create a timer instantiating it to a specified number of seconds ---- Start the timer -----Reset the timer to some value When the Start operation is invoked, it should repeatedly decrement and output the current value of the timer approximately every second. To delay the program for one second use a For loop whose body does absolutely nothing : that is its body is the null statement. Experiment with the number of loop iterations to achieve as close to a one-second delay as you can. Display the timer value at the same position in the center of the screen. Each output should overwrite the previous value displayed like a real-world timer. Use proper indentation, appropriate comments, and meaningful identifiers thought the program.
Explanation / Answer
below are the code with comments to make easy understand the code:
#include <iostream.h>
#include <conio.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
//class declare//
class CDtimer {
CDtimer();
void start();
void stop();
void reset();
bool process();
private:
bool rset;
bool processing;
unsigned long begin;
unsigned long end;
};
CDtimer::CDtimer() {
rset = true;
processing = false;
begin = 0;
end = 0;
}
//function body//
void CDtimer::start() {
if(! processing) {
if(rset)
begin = (unsigned long) clock();
else
begin -= end - (unsigned long) clock();
processing = true;
rset = false;
}
}
void CDtimer::stop() {
if(processing) {
end = (unsigned long) clock();
processing = false;
}
}
void CDtimer::reset() {
bool inProcessing = processing;
if(inProcessing)
stop();
rset = true;
begin = 0;
end = 0;
if(inProcessing)
start();
}
bool CDtimer::process() {
return processing;
}
//main function//
void main() {
CDtimer t;
bool quit = false;
char choice;
t.start();
while(! quit) {
cout << " S start/stop " << endl;
cout << " R reset" << endl;
cout << " Q quit" << endl;
cout << endl;
choice = getch();
switch(choice) {
case 'S':
if(t.process()) {
t.stop();
printf("");
//for beep//
}
else {
t.start();
cout << "started" << endl;
}
break;
case 'R':
t.reset();
cout << "rset" << endl;
break;
case 'Q':
quit = true;
break;
}
cout << "------------------------------" << endl;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.