Question: C++ Update the program that is already provided for the Digital Time class to add a new member function that computes the difference in time
C++
Update the program that is already provided for the Digital Time class to add a new member function that computes the difference in time between two different DigitalTime objects.
Most of the code is already provided to create a class for a digital time. You need to provide the code for a new member function named interval_since which will compute the time difference between two objects
Three files are already provided for you on the class website:
dtime.h a header file that defines the digital time class dtime.cpp the implementation code for the digital time class
C++DigitalTimeApplication.cpp sample code that tests the digital time class
============================================================
C++DigitalTimeApplication.cpp // // You need to complete the member function interval_since before the project is finished // // C++ADTclassDigitalTime.cpp : Defines the entry point for the console application. #include "stdafx.h" // for Microsoft C++ only #include "dtime.h" #includeusing namespace std; int main(int argc, char* argv[]) { // create a DigitalTime object and name it time1 DigitalTime time1(14,22); // initialize time to 14:22 (2:44pm) cout << "The starting time should be 14:22:" << endl; cout << time1 << endl << endl; // display the time cout << "Advance by 32 minutes. The result should be 14:54" << endl; time1.advance(32); // add 32 minutes. Should be 14:54 cout << time1 << endl << endl; // display the time cout << "Advance by 1 hour, 30 minutes. The result should be 16:14" << endl; time1.advance(1,20); // add 1 hour, 30 minutes. Should be 16:14 cout << time1 << endl << endl; // display the time cout << "Input a new time "; cin >> time1; cout << time1 << endl << endl; // display the time cout << "Advance by 5 minutes" << endl; time1.advance(5); // advance 5 minutes cout << time1 << endl << endl; // display the time // // You need to complete the member function interval_since before the project is finished // cout << "You need to complete the program to display elapsed time" << endl; int hours, minutes; DigitalTime time2(8, 25); DigitalTime time3(14, 0); DigitalTime time4(22, 0); // the time difference between 8:25 and 14:00 (2:00pm) should be 5 hours, 35 minutes time3.interval_since(time2, hours, minutes); cout << "The time difference between " << time2 << " and " << time3 << " is " << hours << " hours and " << minutes << " minutes" << endl; // the time difference between 22:00(10pm) and 8:25 is 10 hours 25 minutes time2.interval_since(time4, hours, minutes); cout << "The time difference between " << time4 << " and " << time2 << " is " << hours << " hours and " << minutes << " minutes" << endl; }
==================================================================
dtime.cpp #include "stdafx.h" //DISPLAY 12.2 Implementation File for DigitalTime //Implementation file dtime.cpp (Your system may require some //suffix other than .cpp): This is the IMPLEMENTATION of the ADT DigitalTime. //The interface for the class DigitalTime is in the header file dtime.h. #include#include #include #include "dtime.h" using namespace std; //These FUNCTION DECLARATIONS are for use in the definition of //the overloaded input operator >>: void read_hour(istream& ins, int& the_hour); //Precondition: Next input in the stream ins is a time in 24-hour notation, //like 9:45 or 14:45. //Postcondition: the_hour has been set to the hour part of the time. //The colon has been discarded and the next input to be read is the minute. void read_minute(istream& ins, int& the_minute); //Reads the minute from the stream ins after read_hour has read the hour. int digit_to_int(char c); //Precondition: c is one of the digits '0' through '9'. //Returns the integer for the digit; for example, digit_to_int('3') returns 3. bool operator ==(const DigitalTime& time1, const DigitalTime& time2) { return (time1.hour == time2.hour && time1.minute == time2.minute); } //Uses iostream and cstdlib: DigitalTime::DigitalTime(int the_hour, int the_minute) { if (the_hour < 0 || the_hour > 23 || the_minute < 0 || the_minute > 59) { cout << "Illegal argument to DigitalTime constructor."; exit(1); } else { hour = the_hour; minute = the_minute; } } DigitalTime::DigitalTime( ) : hour(0), minute(0) { //Body intentionally empty. } void DigitalTime::advance(int minutes_added) { int gross_minutes = minute + minutes_added; minute = gross_minutes%60; int hour_adjustment = gross_minutes/60; hour = (hour + hour_adjustment)%24; } void DigitalTime::advance(int hours_added, int minutes_added) { hour = (hour + hours_added)%24; advance(minutes_added); } //Uses iostream: ostream& operator <<(ostream& outs, const DigitalTime& the_object) { outs << the_object.hour << ':'; if (the_object.minute < 10) outs << '0'; outs << the_object.minute; return outs; } //Uses iostream: istream& operator >>(istream& ins, DigitalTime& the_object) { read_hour(ins, the_object.hour); read_minute(ins, the_object.minute); return ins; } int digit_to_int(char c) { return ( static_cast (c) - static_cast ('0') ); } //Uses iostream, cctype, and cstdlib: void read_minute(istream& ins, int& the_minute) { char c1, c2; ins >> c1 >> c2; if (!(isdigit(c1) && isdigit(c2))) { cout << "Error illegal input to read_minute "; exit(1); } the_minute = digit_to_int(c1)*10 + digit_to_int(c2); if (the_minute < 0 || the_minute > 59) { cout << "Error illegal input to read_minute "; exit(1); } } //Uses iostream, cctype, and cstdlib: void read_hour(istream& ins, int& the_hour) { char c1, c2; ins >> c1 >> c2; if ( !( isdigit(c1) && (isdigit(c2) || c2 == ':' ) ) ) { cout << "Error illegal input to read_hour "; exit(1); } if (isdigit(c1) && c2 == ':') { the_hour = digit_to_int(c1); } else //(isdigit(c1) && isdigit(c2)) { the_hour = digit_to_int(c1)*10 + digit_to_int(c2); ins >> c2;//discard ':' if (c2 != ':') { cout << "Error illegal input to read_hour "; exit(1); } } if ( the_hour < 0 || the_hour > 23 ) { cout << "Error illegal input to read_hour "; exit(1); } } void DigitalTime::interval_since(const DigitalTime& previous_time, int& hours_in_interval, int& minutes_in_interval) const { // you need to complete this part of the program to compute the // a) hours_in_interval, and // b) minutes_in_interval hours_in_interval = 0; minutes_in_interval = 0; return; }
=================================================================
dtime.h #pragma once //DISPLAY 12.2 Implementation File for DigitalTime //Implementation file dtime.cpp (Your system may require some //suffix other than .cpp): This is the IMPLEMENTATION of the ADT DigitalTime. //The interface for the class DigitalTime is in the header file dtime.h. #include "stdafx.h" //DISPLAY 12.1 Interface File for DigitalTime //Header file dtime.h: This is the INTERFACE for the class DigitalTime. //Values of this type are times of day. The values are input and output in //24-hour notation, as in 9:30 for 9:30 AM and 14:45 for 2:45 PM. #includeusing namespace std; class DigitalTime { public: friend bool operator ==(const DigitalTime& time1, const DigitalTime& time2); //Returns true if time1 and time2 represent the same time; //otherwise, returns false. DigitalTime(int the_hour, int the_minute); //Precondition: 0 <= the_hour <= 23 and 0 <= the_minute <= 59. //Initializes the time value to the_hour and the_minute. DigitalTime( ); //Initializes the time value to 0:00 (which is midnight). void advance(int minutes_added); //Precondition: The object has a time value. //Postcondition: The time has been changed to minutes_added minutes later. void advance(int hours_added, int minutes_added); //Precondition: The object has a time value. //Postcondition: The time value has been advanced //hours_added hours plus minutes_added minutes. friend istream& operator >>(istream& ins, DigitalTime& the_object); //Overloads the >> operator for input values of type DigitalTime. //Precondition: If ins is a file input stream, then ins has already been //connected to a file. friend ostream& operator <<(ostream& outs, const DigitalTime& the_object); //Overloads the << operator for output values of type DigitalTime. //Precondition: If outs is a file output stream, then outs has already been //connected to a file. void interval_since(const DigitalTime& previous_time, int& hours_in_interval, int& minutes_in_interval) const; //Precondition: The object has a time value. //Precondition: The previous_time object has a time value //Postcondition: The hours_in_interval indicates the number of hours that have passed //Postcondition: The minutes_in_interval indicates the number of minutes that have passed private: int hour; int minute; };
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
