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 ...

Get C++ Primer Plus 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.