COSC 2415 - Data Structures and
ITSE 2445 - Data Structures
Bob Comer, Professor of Computer Studies


Overloading Binary Operators

This page is intended as a supplement to the material in Chapter 4 of the textbook on overloading binary operators. Be sure to read the material in the book. Pay careful attention to the discussion of using member functions versus using non-member functions for overloading binary operators.

In case you are rusty on your math terminology, here is a little review. In the expression

    2 + 3

the left-hand operand is the integer 2, the right-hand operand is the integer 3, and the operator is +. The result of evaluating the expression is a new integer, 5.

The first example of overloading a binary operator in the book overloads the insertion operator <<. For example:

    cout << "Hello"

This is a pretty complicated and unusual case. The left-hand operator is an output stream, the right-hand operand (in this case) is a string, and the result is a reference to the output stream (cout).

Overloading the binary math operators is a much simpler and common case. Typically, the two operands and the result are all the same data type. This is the case for the + operator above where the operands and the result are all integers.

As an example, we will use a class called Point that represents a point in an x-y plane. A Point is identified by two numbers called the x-coordinate and the y-coordinate. For more information, see:

http://en.wikipedia.org/wiki/Cartesian_coordinates

The sum of 2 points is a point whose x-coordinate is the sum of the x-coordinates of the operands, and whose y-coordinate is the sum of the y-coordinates of the operands. In other words, if

    p1 is ( x1, y1 ) and

    p2 is ( x2, y2 ) then

    p1 + p2 is ( x1 + x2, y1 + y2 )

Normally you have a choice of using a member function or using a non-member function to overload a binary operator. Using non-member functions is a little simpler, so we will look at this first.

Overloading a binary operator using a non-member function

For a non-member function, all arguments to the function must be passed in the parameter list. That is, they must be passed explicitly. The operands of the operator are passed to the overloaded function as arguments. The left-hand argument is the first argument, and the right-hand argument is the second argument. Remember that passing an argument by value will result in a copy of the argument being placed in the parameter. Objects are typically passed by reference to avoid the overhead of making a copy. The binary math operators do not modify their operands, so the parameters should be constant reference parameters. The prototype for the overloaded + operator for the Point class is:

     Point operator+( const Point& leftPoint, const Point& rightPoint );

Given Points a, b, and c, an example call to the overloaded + operator is:

    c = a + b;

But an equivalent call that shows how the arguments are passed is:

    c = operator+( a, b );

For a complete example of overloading the binary + operator using a non-member function, you can view or download the files below. Note: Depending on your browser, when you click on the links below, you may see nicely formatted program code, or all the returns may be stripped from the file. However, if you save the file to your local machine, you should be able to open and view the file "normally" using your editor.

main_nonmem.cpp - driver program

point_nonmem.h - Point class header file

point_nonmem.cpp - Point class implementation file

Overloading a binary operator using a member function

Member functions are normally called using an object of the class. The object used to call a member function does not appear explicitly in the parameter list. But it is available to be used in the function. The object used to call the function can be accessed using a pointer called this. While neither the object nor the pointer this appears in the parameter list for the member function, we say that the object (or actually the this pointer) is passed implicitly to the member function.

When you use a member function to overload a binary operator, the left-hand operand is the object that calls the overloaded operator. That means that the function only needs one explicit parameter, the right-hand operand. So the prototype for the overloaded + operator for the Point class is:

     Point Point::operator+( const Point& rightPoint )const;

Given Points a, b, and c, an example call to the overloaded + operator is:

    c = a + b;

But an equivalent call that shows clearly that the left-hand operand is the object that calls the overloaded + operator is:

    c = a.operator+( b );

For a complete example of overloading the binary + operator using a member function, you can view or download the files below. Note: Depending on your browser, when you click on the links below, you may see nicely formatted program code, or all the returns may be stripped from the file. However, if you save the file to your local machine, you should be able to open and view the file "normally" using your editor.

main_mem.cpp - driver program

point_mem.h - Point class header file

point_mem.cpp - Point class implementation file