class Time { public: Time (int hours, int minutes); const Time operator-(const Time& rhs) const; // difference private: int m_hours; int m_minutes; // 0..59 }; // 0..23

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter12: Points, Classes, Virtual Functions And Abstract Classes
Section: Chapter Questions
Problem 33SA
icon
Related questions
Question

C++

In H27, Time and Again, you revisited H01, using structures and member functions. Now, let's do
something similar with overloaded operators. This version of Time will use a 24 hour clock rather than
a 12. Start with the difference.
class Time
{
public:
Time (int hours, int minutes);
const Time operator-(const Time& rhs) const; // difference
private:
int m_hours;
int m_minutes; // 0..59
};
// 0..23
time.cpp
1
#include "time.h"
2
using namespace std;
3
const Time Time::operator-(const Time& rhs) const
{
// Complete this operator
4
7
}
Tester.cpp
1
#include <iostream>
2
#include <climits>
3
#include "time.h"
4
5
using namespace std;
int main()
{
Time a(3, 57), b(1, 5);
cout « (a - b) « endl;
cout « "Expected: 2:52" « endl « endl;
7
9
10
11
12
a = Time(12, 0); b = Time(0, 38);
cout « (a - b) « endl;
cout « "Expected: 11:22" << endl « endl;
13
14
15
16
a = Time(5, 15); b = Time(7, 59);
cout « (a - b) « endl;
cout « "Expected: 21:16" << endl « endl;
17
18
19
20
a = Time(0, 0); b = Time(1, 15);
cout « (a - b) « endl;
cout « "Expected: 22:45" << endl;
}
21
22
23
24
CodeCheck
Reset
Transcribed Image Text:In H27, Time and Again, you revisited H01, using structures and member functions. Now, let's do something similar with overloaded operators. This version of Time will use a 24 hour clock rather than a 12. Start with the difference. class Time { public: Time (int hours, int minutes); const Time operator-(const Time& rhs) const; // difference private: int m_hours; int m_minutes; // 0..59 }; // 0..23 time.cpp 1 #include "time.h" 2 using namespace std; 3 const Time Time::operator-(const Time& rhs) const { // Complete this operator 4 7 } Tester.cpp 1 #include <iostream> 2 #include <climits> 3 #include "time.h" 4 5 using namespace std; int main() { Time a(3, 57), b(1, 5); cout « (a - b) « endl; cout « "Expected: 2:52" « endl « endl; 7 9 10 11 12 a = Time(12, 0); b = Time(0, 38); cout « (a - b) « endl; cout « "Expected: 11:22" << endl « endl; 13 14 15 16 a = Time(5, 15); b = Time(7, 59); cout « (a - b) « endl; cout « "Expected: 21:16" << endl « endl; 17 18 19 20 a = Time(0, 0); b = Time(1, 15); cout « (a - b) « endl; cout « "Expected: 22:45" << endl; } 21 22 23 24 CodeCheck Reset
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Function Arguments
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