Reimplement the digital clock from the preceding exercise, representing the time as the minutes from midnight. This makes the pulse member function very easy, but you have to work harder to recover the hours and minutes. For example, 100 minutes after midnight is 1 hour and 40 minutes. C++

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter10: Classes And Data Abstraction
Section: Chapter Questions
Problem 10PE
icon
Related questions
Question

Reimplement the digital clock from the preceding exercise, representing the time as the minutes from midnight. This makes the pulse member function very easy, but you have to work harder to recover the hours and minutes. For example, 100 minutes after midnight is 1 hour and 40 minutes.

C++

void Clock::pulse()
{
get_minutes++;
if (get_minutes == 60)
{
get_minutes = 0;
get_hours++;
if (get_hours == 24)
{
get _hours = 0;
}
}
39
40
41
42
43
44
45
46
47
48
49
50
51
int Clock: :get_hours() const
{
return get_hours;
}
int Clock: :get_minutes() const
52
53
54
55
56
57
{
return get_minutes;
}
58
59
60
61
int main()
62
{
Clock my_clock; my_clock.reset();
63
64
for (int i = 0; i < 100; i++) { my_clock.pulse(); }
cout « my_clock.get_hours() « endl;
cout « "Expected: 1" « endl;
cout « my_clock.get_minutes() « endl;
cout « "Expected: 40" « endl;
65
66
67
68
69
70
for (int i = 0; i < 78; i++) { my_clock.pulse(); }
cout « my_clock.get_hours() « endl;
cout « "Expected: 2" « endl;
cout « my_clock.get_minutes() « endl;
cout « "Expected: 50" « endl;
71
72
<<
73
74
75
76
for (int i = 0; i < 1270; i++) { my_clock.pulse(); }
cout « my_clock.get_hours () « endl;
cout « "Expected: 0" « endl;
cout « my_clock.get_minutes() « endl;
cout « "Expected: 0" « endl;
77
78
79
80
81
82
83
return e;
84 }
Transcribed Image Text:void Clock::pulse() { get_minutes++; if (get_minutes == 60) { get_minutes = 0; get_hours++; if (get_hours == 24) { get _hours = 0; } } 39 40 41 42 43 44 45 46 47 48 49 50 51 int Clock: :get_hours() const { return get_hours; } int Clock: :get_minutes() const 52 53 54 55 56 57 { return get_minutes; } 58 59 60 61 int main() 62 { Clock my_clock; my_clock.reset(); 63 64 for (int i = 0; i < 100; i++) { my_clock.pulse(); } cout « my_clock.get_hours() « endl; cout « "Expected: 1" « endl; cout « my_clock.get_minutes() « endl; cout « "Expected: 40" « endl; 65 66 67 68 69 70 for (int i = 0; i < 78; i++) { my_clock.pulse(); } cout « my_clock.get_hours() « endl; cout « "Expected: 2" « endl; cout « my_clock.get_minutes() « endl; cout « "Expected: 50" « endl; 71 72 << 73 74 75 76 for (int i = 0; i < 1270; i++) { my_clock.pulse(); } cout « my_clock.get_hours () « endl; cout « "Expected: 0" « endl; cout « my_clock.get_minutes() « endl; cout « "Expected: 0" « endl; 77 78 79 80 81 82 83 return e; 84 }
Tester.cpp
1
#include <iostream>
2 using namespace std;
3 /**
A simulated digital clock.
*/
4
6.
class Clock
7
{
public:
/**
10
Sets the clock to 00:00.
11
*/
12
void reset ();
13
14
/**
15
Advances this clock to the next minute.
16
*/
17
void pulse();
18
19
/**
Gets the hours of this clock.
ereturn the hours (between e and 23)
*/
int get hours() const;
20
21
22
23
24
25
/**
26
Gets the minutes of this clock.
27
@return the minutes (between e and 59)
28
29
int get_minutes() const;
private:
int minutes_from midnight;
30
31
32 };
33
void Clock::reset ()
{
get_hours - 0;
get_minutes = 0;
34
35
36
37
38
Transcribed Image Text:Tester.cpp 1 #include <iostream> 2 using namespace std; 3 /** A simulated digital clock. */ 4 6. class Clock 7 { public: /** 10 Sets the clock to 00:00. 11 */ 12 void reset (); 13 14 /** 15 Advances this clock to the next minute. 16 */ 17 void pulse(); 18 19 /** Gets the hours of this clock. ereturn the hours (between e and 23) */ int get hours() const; 20 21 22 23 24 25 /** 26 Gets the minutes of this clock. 27 @return the minutes (between e and 59) 28 29 int get_minutes() const; private: int minutes_from midnight; 30 31 32 }; 33 void Clock::reset () { get_hours - 0; get_minutes = 0; 34 35 36 37 38
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Random Class and its operations
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning