O'Reilly logo

C++ Primer Plus by Stephen Prata

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required