More Overloaded Operators
Some other operations make sense for the Time class. For example, you might want to subtract one time from another or multiply a time by a factor. This suggests overloading the subtraction and multiplication operators. The technique is the same as for the addition operator: you create operator-() and operator*() methods. That is, you add the following prototypes to the class declaration:
Time operator-(const Time & t) const;Time operator*(double n) const;
Listing 11.7 shows the new header file.
Listing 11.7. mytime2.h
// mytime2.h -- Time class after operator overloading#ifndef MYTIME2_H_#define MYTIME2_H_class Time{private: int hours; int minutes;public: Time(); Time(int h, int m = 0); void AddMin(int ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access