Adding an Addition Operator

It’s a simple matter to convert the Time class to using an overloaded addition operator. You just change the name of Sum() to the odder-looking name operator+(). That’s right: You just append the operator symbol (+, in this case) to the end of operator and use the result as a method name. This is one place where you can use a character other than a letter, a digit, or an underscore in an identifier name. Listings 11.4 and 11.5 reflect this small change.

Listing 11.4. mytime1.h

// mytime1.h -- Time class before operator overloading#ifndef MYTIME1_H_#define MYTIME1_H_class Time{private:    int hours;    int minutes;public:    Time();    Time(int h, int m = 0);    void AddMin(int m);    void AddHr(int h);    void Reset(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.