CS 202 Fall 2013  >  Notes for Thursday, October 3, 2013

CS 202 Fall 2013
Notes for Thursday, October 3, 2013

Operator Overloading (cont’d) [14.5]

Comparison Operators

Operator functions for comparison operators will return bool by value. If their parameters are objects, then they take them by reference to const.

[C++]

bool operator==(const MyNum & a, const MyNum & b)
{
    ...
}

Remember that we are writing our operators as global functions.

Operators == and < are generally the most fundamental. Make the rest of the comparisons out of these two.

[C++]

bool operator!=(const MyNum & a, const MyNum & b)
{
    return !(a == b);
}

bool operator>(const MyNum & a, const MyNum & b)
{
    return b < a;
}

As before, some operator functions may need to be friends of a class. Above, we might need to declare each of operators == and < as a friend of class MyNum. But the other comparisons (!=, <=, >, >=) do not need to be friends, as they only need to call == and <.

[C++]

class MyNum {
    ...
    friend bool operator==(const MyNum & a, const MyNum & b);
    friend bool operator<(const MyNum & a, const MyNum & b);
    ...
};

Increment & Decrement Operators

There are two kinds of ++ operator: pre-increment (++x) and post-increment (x++). These differ in the value they return.

[C++]

int a = 3;
int b = 3;
cout << (++a) << endl;  // Prints "4"
cout << (b++) << endl;  // Prints "3"
// Now both a, b are 4

If you think about it, pre-increment makes sense: increment, then return the new value. But post-increment has to return a value that has gone away. So it needs to save the old value before doing the increment. For this reason:

One more note, and then some code: pre-increment and post-increment have the same name and the same operands. We need some way to distinguish them. And so the Great Minds of C++ decreed:

[C++]

// Pre-increment
MyNum & operator++(MyNum & a)
{
    ...
    return a;
}

// Post-increment
MyNum operator++(MyNum & a, int dummy)
{
    MyNum save_a(a);
    ++a;  // Call pre-increment
    return save_a;
}

The above discussion also applies to pre-decrement (--x) and post-decrement (x--).

Summary: Fundamental & Not-So-Fundamental

Fundamental Operation Others to Make from It
+= Binary +
== !=
< <=, >, >=
Pre ++ Post ++
Pre -- Post --

Here is code for operators in the right-hand column, above, that probably works no matter how the left-hand-column operator is written.

[C++]

// Binary + operator
// Use +=
MyNum operator+(const MyNum & a, const MyNum & b)
{
    MyNum copy_a(a);
    copy_a += b;
    return copy_a;
}

// != operator
// Use ==
bool operator!=(const MyNum & a, const MyNum & b)
{
    return !(a == b);
}

// <= operator
// Use <
bool operator<=(const MyNum & a, const MyNum & b)
{
    return !(b < a);
}

// > operator
// Use <
bool operator>(const MyNum & a, const MyNum & b)
{
    return b < a;
}

// >= operator
// Use <
bool operator>=(const MyNum & a, const MyNum & b)
{
    return !(a < b);
}

// Post ++ operator
// Use pre ++
MyNum operator++(MyNum & a, int dummy)
{
    MyNum save_a(a);
    ++a;
    return save_a;
}

// Post -- operator
// Use pre --
MyNum operator--(MyNum & a, int dummy)
{
    MyNum save_a(a);
    --a;
    return save_a;
}

For today’s lab work, see the 10/3 Challenge.


CS 202 Fall 2013: Notes for Thursday, October 3, 2013 / Updated: 3 Oct 2013 / Glenn G. Chappell