You have learned that it is possible to overload member functions in a class. In this section, you will learn that it is also possible to overload C++ operators. In C++, new definitions can be applied to such familiar operators as +, -, *, and / in a given class.
The concept of operator overloading is common in numerous programming languages, even if it is not specifically implemented. For example, all compiled languages make it possible to add two integers, two floats, or two doubles (or their equivalent types) with the + operator. This is the essence of operator overloading— using the same operator on different data types. In C++, it is possible to extend this simple concept even further. In most compiled languages, it is not possible, for example, to take a complex number, matrix, or character string and add them together with the + operator.
These operations are valid in all programming languages:
3 + 8 3.3 + 7.2
These operations are typically not valid operations:
If the last three operations were possible with the + operator, the workload of the programmer would be greatly reduced when designing new applications. The good news is that in C++, the + operator can be overloaded and the previous three operations can be made valid. Many additional operators can also be overloaded. Operator overloading is used extensively in C++. You will find examples throughout the various Microsoft C++ libraries.
Overloading Operators and Function Calls
In C++, the operators shown in Table 17-1 can be overloaded.
Table 17-1: Operators that Can Be Overloaded in C++
+
?
*0
/
=
<
>
+=
?=
*0=
/=
<<
>>
>>=
<<=
= =
!=
<=
>=
++
—
%
&
^^
!
|
~
&=
^=
|=
&&
||
%=
[]
( )
new
Delete
The main restrictions are that the syntax and precedence of the operator must remain unchanged from its originally defined meaning. Another important point is that operator overloading is valid only within the scope of the class in which overloading occurs.
Overloading Syntax
In order to overload an operator, the operator keyword is followed by the operator itself:
type operator opr(param list)
For example:
angle_value operator +(angle_argument);
Here, angle_value is the name of the class type, followed by the operator keyword, then the operator itself (+) and a parameter to be passed to the overloaded operator.
Within the scope of a properly defined class, several angles specified in degrees/minutes/seconds could be directly added together:
In this example, the symbol for degrees is (d), for minutes (m), and for seconds (s).
The carry information from seconds-to-minutes and from minutes-to-hours must be handled properly. A carry occurs in both cases when the total number of seconds or minutes exceeds 59. This doesn’t have anything to do with operator overloading directly, but the program must take this fact into account if a correct total is to be produced, as shown here:
// // opover.cpp // C++ program illustrates operator overloading. // Program will overload the “+” operator so that // several angles, in the format degrees minutes seconds, // can be added directly. // Copyright (c) Chris H. Pappas and William H. Murray, 1998 //
The divide and modulus operations are performed on the sums to ensure correct carry information.
Further details of the program’s operation are omitted since you have seen most of the functions and modules in earlier examples. However, it is important to remember that when you overload operators, proper operator syntax and precedence must be maintained.
The output from this program shows the sum of the four angles to be as follows: