Question: Four integers are read from input, where the first two integers are the minutes and seconds of timeMinSec1 and the second two integers are the
Four integers are read from input, where the first two integers are the minutes and seconds of timeMinSec1 and the second two integers are the minutes and seconds of timeMinSec2. Define two functions to overload the - operator. The first function overloads the - operator to subtract two time values. The second function overloads the - operator to subtract a time and an integer representing the number of minutes.
Ex: If the input is 7 22 4 4, then the output is:
7 minutes, 22 seconds 4 minutes, 4 seconds Difference: 3 minutes, 18 seconds 7 minutes, 22 seconds 4 minutes Difference: 3 minutes, 22 seconds
Note: The difference of two time values is:
the difference of the number of minutes
the difference of the number of seconds
Note: The difference of a time and an integer representing the number of minutes is:
the difference of the number of minutes and the integer
the number of seconds is unchanged
Code:
#include
class TimeMinSec { public: TimeMinSec(int minutes = 0, int seconds = 0); void Print() const; TimeMinSec operator-(TimeMinSec rhs); TimeMinSec operator-(int rhs); private: int m; int s; };
TimeMinSec::TimeMinSec(int minutes, int seconds) { m = minutes; s = seconds; }
// No need to accommodate for overflow or negative values
// ADD CODE HERE // DO NOT CHANGE ANY OTHER PART OF THE CODE, JUST ADD HERE
void TimeMinSec::Print() const { cout << m << " minutes, " << s << " seconds"; }
int main() { int minutes1; int seconds1; int minutes2; int seconds2; cin >> minutes1; cin >> seconds1; cin >> minutes2; cin >> seconds2; TimeMinSec timeMinSec1(minutes1, seconds1); TimeMinSec timeMinSec2(minutes2, seconds2); TimeMinSec difference1 = timeMinSec1 - timeMinSec2; TimeMinSec difference2 = timeMinSec1 - minutes2; timeMinSec1.Print(); cout << endl; timeMinSec2.Print(); cout << endl; cout << "Difference: "; difference1.Print(); cout << endl; cout << endl; timeMinSec1.Print(); cout << endl; cout << minutes2 << " minutes" << endl; cout << "Difference: "; difference2.Print(); cout << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
