Let’s look at a technique for giving object operations a prettier look.
C++ lets you extend operator overloading to user-defined types, permitting you, say, to use the + symbol to add two objects. Again, the compiler uses the number and type of operands to determine which definition of addition to use. Overloaded operators can often make code look more natural. For example, a common computing task is adding two arrays. Usually, this winds up looking like the following for loop:
for (int i = 0; i < 20; i++)
evening[i] = sam[i] + janet[i]; // add element by element
But in C++, you can define a class that represents arrays and that overloads the + operator so that you can do this:
evening = sam + janet; // add two array objects
This simple addition notation conceals the mechanics and emphasizes what is essential, and that is another goal of OOP.
To overload an operator, you use a special function form called an
operator
For example, operator+() overloads the + operator and operator*() overloads the * operator. The
district2 = sid + sara;
The compiler, recognizing the operands as belonging to the Salesperson class, replaces the operator with the corresponding operator function:
district2 = sid.operator+(sara);
The function then uses the sid object implicitly (because it invoked the method) and the sara object explicitly (because it’s passed as an argument) to calculate the sum, which it then returns. Of course, the nice part is that you can use the nifty + operator notation instead of the clunky function notation.
C++ imposes some restrictions on operator overloading, but they’re easiest to understand after you’ve seen how overloading works. So let’s develop a few examples to clarify the process and then discuss the limitations.
Time on Our Hands: Developing an Operator Overloading Example
If you worked on the Priggs account for 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) don’t match a built-in type. Chapter 7, “Functions: C++’s Programming Modules,” handles a similar case by defining a travel_time structure and a sum() function for adding such structures. Now let’s generalize that to a Time class, using a method to handle addition. Let’s begin with an ordinary method, called Sum(), and then see how to convert it to an overloaded operator. Listing 11.1 shows the class declaration.
Listing 11.1. mytime0.h
// mytime0.h -- Time class before operator overloading
#ifndef MYTIME0_H_
#define MYTIME0_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 h = 0, int m = 0);
Time Sum(const Time & t) const;
void Show() const;
};
#endif