Get premium membership and access questions with answers, video lessons as well as revision papers.

Create a C++ class called stopwatch that emulates a stopwatch that keeps track of elapse time. Use a constructor to initially set the elapse time...

      

Create a C++ class called stopwatch that emulates a stopwatch that keeps track of elapse time. Use a constructor to initially set the elapse time to 0.Provide two member function called start() and stop() that turns on and off the timer, respectively. Include a member function called show() that displays the elapsed time.Also, have the destructor function automatically display elapsed when stopwatch object is destroyed.

  

Answers


Davis
//Stopwatch emulator
#include
#include
using namespace std;
class stopwatch {
double begin, end;
public:
stopwatch();
~stopwatch();
void start();
void stop();
void show();
};
stopwatch::stopwatch()
{
begin = end = 0.0;
}
stopwatch::~stopwatch()
{
cout << "Stopwatch object being destroyed...";
show();
}
void stopwatch::start()
{
begin = (double) clock() /CLOCKS_PER_SEC;
}
void stopwatch::stop()
{
end = (double) clock() / CLOCKS_PER_SEC;
}
void stopwatch::show()
{
cout << "Elapsed time: "<< end - begin;
cout << "\n";
}
int main()
{
stopwatch watch;
long i;
watch.start ();
for (i=0; i<320000; i++) ; //time a for loop
watch.stop();
watch.show();
return 0;
}
Githiari answered the question on May 5, 2018 at 17:46


Next: Create an overload rotate() function using C++ new style that left-rotates the bits in its argument and returns the result.Overload it so it accepts ints...
Previous: Illustrate the change of a C++ stack so it dynamically allocates memory for the stack.Have the size of the stack specified by a parameter to...

View More Computer Science Questions and Answers | Return to Questions Index


Learn High School English on YouTube

Related Questions