Time on Our Hands

If you worked on the Priggs account 2 hours 35 minutes in the morning and 2 hours 40 minutes in the afternoon, how long did you work altogether on the account? Here's an example where the concept of addition makes sense, but the units that you are adding (a mixture of hours and minutes) doesn't match a built-in type. Chapter 7 handled a similar case by defining a travel_time structure and a sum() function for adding such structures. Now we can generalize that to a Time class using a method to handle addition. Let's begin with an ordinary method, then see how to convert it to an overloaded operator. Listing 10.1 shows the class declaration.

Listing 10.1 mytime0.h.
 // mytime0.h -- Time class before operator overloading #ifndef ...

Get The Waite Group's C++ Primer Plus, Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.