Overloading Unary Operators
Unary operators are those that have just one operand
such as increment (++), decrement (--), not (!), indexing ([])
and negation (-). Since their single operand is an object
of your class and since we're not worried about commutivity
like we are with binary operators, unary operators should be
overloaded as member functions. Overloading them as non-member
functions is also possible.
There are a couple of interesting things to note
- Unary negation as in y = -x; uses the
same symbol (the minus sign) as used in subtraction.
How does the compiler know the difference?
- The increment and decrement operators (++ and --)
each have two version -- pre-increment/decrement
as in y = ++x; and y = --x;
and post-increment/decrement as in y = x++; and y = x--;.
It takes a little bit of artificial coding to write functions for
each of these.
Overloading negation for Money
Here's an example using our familiar Money class.
This is the overloaded negation operator.
The prototype in Money.h
#include
#include
#include
using namespace std;
// Class for amounts of money in U.S. currency.
// Text display 8.3 (part 1), page 321 showing
// operator+ as a friend
// Modified for CMSC 202 coding standards
class Money
{
public:
Money( );
Money(double amount);
Money(int theDollars, int theCents);
Money(int theDollars);
double GetAmount( ) const;
int GetDollars( ) const;
int GetCents( ) const;
// Input() reads the dollar sign as well as the amount number.
void Input( );
void Output( ) const;
// Unary minus (negation)
const Money operator- ( ) const;
private:
//A negative amount is represented as negative dollars and
//negative cents. Negative $4.50 is represented as -4 and -50
int m_dollars;
int m_cents;
int DollarsPart(double amount) const;
int CentsPart(double amount) const;
int Round(double number) const;
};
The code in Money.cpp
// The unary minus (negation) operator for Money
const Money Money::operator- ( ) const
{
Money result;
result.m_dollars = -m_dollars;
result.m_cents = -m_cents;
return result;
}
Overloading the increment operator for Money
The increment operator make sense for the Money class
since it has an intuitive function -- add a penny.
To differentiate the pre-increment and post-increment functions,
the post-incrment function is defined to have a dummy
argument of type int. It doesn't do anything, it's not seen by
the user. It just tells the compiler that the function is
post-increment rather than pre-increment. The same thing is
done for pre- and post-decrement.
The definitions in Money.h
#include
#include
#include
using namespace std;
// Class for amounts of money in U.S. currency.
// Text display 8.3 (part 1), page 321 showing
// operator+ as a friend
// Modified for CMSC 202 coding standards
class Money
{
public:
Money( );
Money(double amount);
Money(int theDollars, int theCents);
Money(int theDollars);
double GetAmount( ) const;
int GetDollars( ) const;
int GetCents( ) const;
// Input() reads the dollar sign as well as the amount number.
void Input( );
void Output( ) const;
// PreIncrement operator
Money operator++( );
// PostIncrement operator (dummy parameter)
Money operator++( int dummy);
private:
//A negative amount is represented as negative dollars and
//negative cents. Negative $4.50 is represented as -4 and -50
int m_dollars;
int m_cents;
int DollarsPart(double amount) const;
int CentsPart(double amount) const;
int Round(double number) const;
};
The code in Money.cpp
// Increment operators for Money
// Modify this Money object and return
// a new one by value
// PreIncrement
Money Money::operator++( void )
{
// increment the cents
++m_cents;
// code here to adjust the dollars if necessary
// return a Money object just like this one
return Money( m_dollars, m_cents);
}
// PostIncrement
Money Money::operator++( int dummy )
{
// make a copy of this Money object
// before incrementing the cents
Money result(m_dollars, m_cents);
// now increment the cents
++m_cents;
// code here to adjust the dollars
// return the Money as it was before
// the increment
return result;
}