Time Class and Operator Overloading

Lengkap Disiarkan 3 tahun lepas Dibayar semasa penghantaran
Lengkap Dibayar semasa penghantaran

Write the following overloaded operators, which have prototypes already written in Time.h and empty definitions (that you must complete) in [login to view URL]:

• 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

• h or hh = # 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 (<), and greater 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 = T2 + T3;

T2 += T3; // shorthand for T2 = T2 + T3;

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 (<<): Testing every other operator is easier when you can print the result, and it's easy to declare and initialize Time objects

• Input 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. (Remember, I couldn't get the advance() function right the first time!) 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

Pengaturcaraan C++

ID Projek: #27876296

Tentang projek

5 cadangan Projek jarak jauh Aktif 3 tahun lepas

Dianugerahkan kepada:

mubashirmalick

Hi! I am an expert in C, C++ and data structures. I would be easily able to help you regarding any task related to them. Kindly message me to discuss more details. Thank you!

$50 USD dalam sehari
(9 Ulasan)
2.7

5 pekerja bebas membida secara purata $52 untuk pekerjaan ini

usamamw141

Hi there, I am expert in C++ and I can do this task today. Feel free to inbox me any time. Thank you.

$50 USD dalam sehari
(72 Ulasan)
5.4
braincenter

Hello, Hope this message finds you well, I checked your details and I believe that my experience is what you are looking 4. I have been working on similar projects for the past eight years, and I have the essential sk Lagi

$100 USD dalam 3 hari
(4 Ulasan)
2.4
hnizami6649

Hello! I'm a professional programmer in computer science. I look forward to the best possible time and on a low budget to solve your informatics tasks, projects, and tasks. Are you stuck in the middle of an IT task, Lagi

$30 USD dalam 2 hari
(0 Ulasan)
0.0