Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Programming Assignment Help needed ASAP For this C++ program can you please complete Time.cpp file using overloaded operators. Below are the screenshots that can

C++ Programming Assignment Help needed ASAP

For this C++ program can you please complete Time.cpp file using overloaded operators. Below are the screenshots that can you be helpful. So can you please complete the Time.cpp file with the C++ code and return to me ASAP. Thanks

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed
7.23 Program 3: The Time class and operator overloading This assignment gives you experience writing overloaded operators for handling input/output, comparisons, and arithmetic operations. Earlier in the semester, we discussed how to model time as a data type, briefly defining a Time structure. I've converted this structure to a class and written the four member functions we discussed (set (), display (), advance(), and lessThan ( )) and the class constructors. I've also written a main program to test the code you generate. Your job will be to write the following overloaded operators, which have prototypes already written in Time . h and empty definitions (that you must complete) in Time . cpp . Input/output operators: > Comparison operators: == != . Arithmetic operators: + - += -= . Increment operators: ++ as both pre- and post-increment Operator specifications Input/output Both the input and output operators should handle times entered in the form h:mm _M or hh: mm _M, where . horhh = # of hours, using 1 or 2 digits . mm = # of minutes, always using 2 digits _M = AM or PM Examples of valid times: 7:30 PM, 12:22 AM, 9:09 PM. Comparison Each comparison operator tests the relationship between two Time objects, returning a boolean value indicating if the specified condition is true or false. As noted above, the conditions being tested are equality (==), inequality (!=), less than (). Arithmetic The arithmetic operators perform addition or subtraction between two Time objects as follows: . When adding two times, take the first time and advance it by the number of hours and minutes specified in the second time . When subtracting one time from another, take the first time and move it back by the number of hours and minutes specified in the second time. Keep in mind that a time after noon (12:00 PM) is 12 hours later than the same time before noon. The following examples demonstrate theComparison Each comparison operator tests the relationship between two Time objects, returning a boolean value indicating if the specified condition is true or false. As noted above, the conditions being tested are equality (==), inequality (!=), less than (). Arithmetic The arithmetic operators perform addition or subtraction between two Time objects as follows: . When adding two times, take the first time and advance it by the number of hours and minutes specified in the second time. . When subtracting one time from another, take the first time and move it back by the number of hours and minutes specified in the second time Keep in mind that a time after noon (12:00 PM) is 12 hours later than the same time before noon. The following examples demonstrate the desired arithmetic: . 3:35 PM + 12:17 AM = 3:52 PM (add 0 hours, 17 minutes) . 8:00 AM + 7:06 PM = 3:06 PM (add 19 hours (since 7 PM = 12 + 7 = 19 hours), 6 minutes) . 4:15 AM - 3:20 AM = 12:55 AM (subtract 3 hours, 20 minutes) 1:15 PM - 3:20 PM = 9:55 PM (subtract 15 hours, 20 minutes) The difference between the two addition operators is the += operator stores the sum in the calling object, while the + operator creates a new object to store the sum and returns a copy of that new object, without modifying the calling object. In other words, the following two statements calculate the same sum (T2 + T3), but the first one stores that sum in T1, while the second one stores the sum in T2: T1 = 12 + 13; T2 += 13; // shorthand for 12 = 12 + 13; The difference between the - and -= operators is similar. Increment The pre- and post-increment operators are similar to the add operations, but, as unary operators, each of them adds a single minute to the time stored in the calling object. Recall that the difference between pre- and post-increment operators is essentially that the pre-increment operator changes the calling object and then returns a reference to it, while the post-increment operator copies the calling object and returns its original value after changing the original object. Hints Suggested order I'd suggest writing these operators in the following order: . Output operator ( >): This operator is only used in the first two test cases--we don't combine input tests with any other operator tests-- but moving on to this one is a logical extension of the output operator. . Comparison operators (==, !=, ): All of these operators are similar, and checking how two Times compare to one another is relatively straightforward. . Arithmetic operators (+, -, +=, -=): Understanding how to correctly add and subtract Time objects is the hardest part of this assignment, in my opinion. There are a couple of special cases to account for, as described below. I recommend handling the simple + and - operators before the augmented assignments += and -=. . Increment operators (++): The increment operators are definitely simpler than the add and subtract operators, but you have to understand addition to write these correctly. Code reuse This program offers a great chance to practice one of the key tenets of programming: reusing existing code when writing new functions. You can use some of the operators to help you write others; you may also be able to use some of the four member functions I wrote to help you write your operators. Math on Time objects From personal experience (again, the advance( ) function looked a lot different originally), I can tell you it is much easier to do math on the miltime data members of Time objects and update the other three data members (hours, minutes, and AMorPM) afterwards than it is to do math on hours and minutes and fix everything else after that. See the new advance ( ) function for an example. The trickiest part of these operators is accounting for: 1. Cases in which you add or subtract enough minutes to affect the number of hours. For example, 1:50 AM + 1:15 AM = 3:05 AM (since "2:65 AM" isn't a valid time). 2. Cases in which you add or subtract enough hours to change from AM to PM or vice versa. For example, 11:09 PM + 1:23 AM = 12:32 AM.Code reuse This program offers a great chance to practice one of the key tenets of programming: reusing existing code when writing new functions. You can use some of the operators to help you write others: you may also be able to use some of the four member functions I wrote to help you write your operators. Math on Time objects From personal experience (again, the advance(} function looked a lot differentoriginally]. I can tell you it is much easier to do math on the miltime data members of Time objects and update the other three data members (hours, minutes, and AltorPM] afterwards than it is to do math on hours and minutes and x everything else after that. See the new aduance(} function for an example. The trickiest part ofthese operators is accounting for: 1. Cases in which you add or subtract enough minutes to affect the number of hours. For example. 1:50 AM + 1:15 PM = 3:05 AM [since '2:65 Alvl' isn't a valid time]. 2. Cases in which you add or subtract enough hours to change from AM to PM or vice versa. For example, 11:09 PM + 1:23 AM = 12:32 AM. 33\"...\" ' 123.1: Program 3: The Time class and operator overloading D! 100 U Down oade ble fles Time.h , Time.cpp .and RTC.Cpp Download Current file: time_test_p3.cpp Load default template... /* * EECE. 3220: Data Structures * Instructor: M. Geiger * time_test_p3. cpp: Main program to test Time class for Program 3 she #include "Time.h" #include 10 using namespace std; 11 12 // RunTestCases() handles all input and output for actual test cases 13 // When running in submit mode, main() should only contain a call 14 // to this function; leaving the call out of your main() function 15 // allows you to run the program in develop mode and test your work 16 // without worrying about the test cases 17 // Function definition can be found in RTC. cpp (which is read-only) 18 void RunTestCases(); 19 20 int main() { 21 RunTestCases(); // See function description above 22 // Comment out/remove this function call if you want 23 to write your own test code, but make sure your 24 main() function only contains this function call 25 when you're ready to run the program in submit mode 26 return 0; 27 }Current file: Time.cpp Load default template.. * EECE. 3220: Data Structures $ Instructor: M. Geiger $ Time. cpp: Time function definitions $ Includes blank definitions for overloaded operators to be written for Program 3 9 #include "Time.h" // Necessary for Time class definition; implicitly includes 10 #include // Necessary for setw(), setfill() 11 using sto::setw; 12 using sto::setfill; 13 14 /* *> OVERLOADED OPERATORS TO BE ADDED FOR PROGRAM 3 * */ 15 /* *> PREVIOUSLY DEFINED FUNCTIONS START ON LINE 145 (BEFORE YOU START ADDING CODE) *> >/ 16 /* * * UPDATED 10/11 TO FIX PARAMETERIZED CONSTRUCTOR, advance() **>/ 17 18 // Output operator 19 0stream& operator > (istream& in, Time& rhs) { 35 36 37 * Read time assuming it is written in form: 38 himm _M or hh:mm _M 39 * where: 40 hor hh = # of hours (i or 2 digits)Current file: Time.cpp - Load default template.. 40 h or hh = # of hours (1 or 2 digits) 41 = # of minutes (always 2 digits) 42 = AM or PM 43 44 45 return in; 46 } 47 48 // Comparison operators 49 bool Time: :operator ==(const Time& Phs) { 50 51 52 * Returns true if calling object matches rhs, 53 false otherwise 54 55 56 return false; 57 58 59 bool Time: :operator !=(const Time& phs) { 60 61 62 * Returns true if calling object doesn't match rhs, 63 false otherwise 64 65 66 return false; 67 } 68 69 bool Time: :operator (const Time& phs) {Current file: Time.cpp Load default template. 78 bool Time: :operator >(const Time& rhs) { 80 81 82 * Returns true if calling object is greater 83 (Later in day) than phs, false otherwise 84 85 86 return false; 87 } 88 89 // Arithmetic operators 90 Time Time : :operator +(const Time& rhs) { 91 Time sum; 92 93 94 * Add two Time objects and return sum 95 See examples in spec 96 97 98 return sum; 99 } 100 101 102 Time Time : :operator - (const Time& rhs) { 103 Time diff; 104 105 106 * Subtract two Time objects and return difference 187 See examples in spec 108 109 110 return diff; 111 } 112 113 Time& Time: :operator += (const Time& phs) { 114 115 116 * Same as + operator, but modifies calling object 117 and returns reference to calling objectCurrent file: Time.cpp - Load default template. 115 116 * Same as + operator, but modifies calling object 117 and returns reference to calling object 118 119 } 121 Time& Time: :operator -=(const Time& phs) { 122 123 124 Same as - operator, but modifies calling object 125 and returns reference to calling object 126 127 128 } 129 130 // Increment operators- -adds 1 minute to current time 131 Time& Time: : operator++() { 132 133 * Pre- increment operator 134 135 } 136 137 Time Time: :operator++(int) { 138 139 * Post - increment operator 140 141 } 142 /*> > END OVERLOADED OPERATORS TO BE ADDED FOR PROGRAM 3 * *$/ 143 /**> DO NOT MODIFY CODE BELOW THIS LINE * **/ 144 // Default constructor 145 Time: :Time() : hours(@), minutes(@), miltime(@), AMorPM('A' ) 146 {} 147 148 // Parameterized constructor 149 Time : :Time(unsigned h, unsigned m, char AP) : hours(h), minutes (m), AMorPM(AP) { 150 miltime = 100 * h + m; 151 152 /*** FIXED 10/11: ORIGINAL VERSION DID NOT CORRECTLY HANDLE 12 AM OR 12 PM *$*/ 153 if (AP == 'p' && h != 12) 154 miltime += 1200:Current file: Time.cpp Load default template.. 142 /*** END OVERLOADED OPERATORS TO BE ADDED FOR PROGRAM 3 * *$/ 143 /*** DO NOT MODIFY CODE BELOW THIS LINE * **/ 144 // Default constructor 145 Time : :Time() : hours (@), minutes(@), miltime(@), AMorPM('A' ) 146 {} 147 148 // Parameterized constructor 149 Time : : Time (unsigned h, unsigned m, char AP) : hours(h), minutes (m), AMorPM(AP) { 150 miltime = 100 * h + m; 151 152 (*** FIXED 10/11: ORIGINAL VERSION DID NOT CORRECTLY HANDLE 12 AM OR 12 PM * **/ 153 if (AP == 'p' && h != 12) 154 miltime += 1260; 155 else if (AP == 'A' & h == 12) 156 miltime -= 1260; 157 } 158 159 // Set time dato members 160 void Time: : set (unsigned h, unsigned m, char AP) { 161 hours = h; 162 minutes = m; 163 AMorPM = AP; 164 miltime = 100 * h + m; 165 if (AP == 'P' ) 166 miltime += 1260; 167 } 168 169 // Print time to desired output stream 170 void Time : : display (ostream& out) { 171 out / 180 /$* * NEW VERSION DOES ALL MATH ON MILTIME AND THEN CORRECTS HOURS, MINUTES * *$/ 181 void Time : : advance (unsigned h, unsigned m) {Current file: Time.cpp - Load default template... 1 13 / WOULD MIND IS JAIL IVY LUNVULVILL 180 /** * NEW VERSION DOES ALL MATH ON MILTIME AND THEN CORRECTS HOURS, MINUTES * *$/ 181 void Time : : advance (unsigned h, unsigned m) { 182 183 unsigned tempMT = h * 100 + m; // Temporary miltime representing amount 184 of time to advance by, since math 185 is much easier using miltime! 186 187 // If sum of minutes >= 60, need to account for extra hour added 188 if (minutes + m >= 60) 189 miltime = (miltime + tempMT + 46) % 2400; // * 2400 ensures time between @ & 2359 198 (since minutes adjustment guarantees 191 Last two digits 12) 208 hours -= 12; 201 202 // Special case 2: 12:XX AM --> miltime / 100 = 0 203 else if (hours = = 0) 284 hours = 12; 205 206 minutes = miltime % 160; 287 // Figure out if new time is in AM or PM 209 AMorPM = (miltime >(istream& in, Time& phs) ; 31 32 // Comparison operators 33 bool operator ==(const Time& rhs) ; 34 bool operator !=(const Time& rhs); 35 bool operator (const Time& rhs); 37 38 // Arithmetic operators 39 Time operator +(const Time& rhs); 40 Time operator -(const Time& rhs); 41 Time& operator +=(const Time& rhs) ; 42 Time& operator -=(const Time& rhs) ; 43 44 // Increment operators--adds 1 minute to current time 45 Time& operator#+(); 46 Time operatort+(int) ; 47 /**END OVERLOADED OPERATORS TO BE ADDED FOR PROGRAM 3 * * */ 48 49 50 private: 51 unsigned hours, minutes; // Time in hes/mins (combined with AM/PM) 52 char AMorPM; // Indicates morning/afternoon- - 'A' for AM, 'P' for PM 53 unsigned miltime; // Military (24-hour) time equivalent 54 3 55 56 #endif // TIME_HCurrent file: Time.h Load default template... 1 * EECE. 3220: Data Structures W N * Instructor: M. Geiger 4 5 * Time. h: Time class definition 6 * Includes prototypes for overloaded operators to be written for Program 3 8 9 // Header guard directives--ensures header included only once 10 #ifndef TIME_H 11 #define TIME_H 12 13 #include 14 using sto::ostream; 15 using std::istream; 16 17 class Time { 18 public: 19 *** FUNCTIONS DEFINED FOR YOU IN Time. cpp ** */ 20 Time (); // Default constructor 21 Time (unsigned h, unsigned m, char AP); // Parameterized constructor 22 void set (unsigned h, unsigned m, char AP); // Set time data members 23 void display (ostream &out); // Print time to desired output stream 24 void advance (unsigned h, unsigned m) ; // Advance time by given number of hours and minutes 25 bool lessThan (const Time &phs) ; // Returns true if calling object is less than argument 26 27 *** OVERLOADED OPERATORS TO BE ADDED FOR PROGRAM 3 **#/ 28 // Input/output operators 29 friend ostream &operator (istream& in, Time& rhs) ; 31

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mobile Communications

Authors: Jochen Schiller

2nd edition

978-0321123817, 321123816, 978-8131724262

More Books

Students also viewed these Programming questions

Question

=+ How can credentialism hurt economic growth?

Answered: 1 week ago