You are on page 1of 3

Operator Overloading

• Some commonly used operators: {+,-,*,/}


• Behaviour is defined for most included
Intro to C++ part 7 types
• Can define the behaviour for new types
Daniel Wigdor with data from: • Behaviour must be explicitly declared
1. Richard Watson, notes on C++ • To be useful, behaviour should be intuitive
2. Winston, P.H., “On to C++”
ISBN: 0-201-58043-8

Using Overloaded Operators Avoid Temptation!


• This behaviour is included in C++: • It is tempting to some to make unintuitive
int x = 17; choices.
int y = 18;
int z = x + y; // z == 35 • “+” could behave like “-”, or “*” like “/”
• But this is not: • This is clearly undesirable, since it makes
Queue q1,q2,q3; coding harder for the client & team
q1.Enqueue(‘a’); q2.Enqueue(‘b’);
q3 = q1 + q2; // what is q3?
members
• We must decide on a behaviour, and
implement it

Step 1: Deciding What “+” Is Step 2: Implement Choice


• Q1 = {‘a’,’b’,’c’}, Q2 = {‘x’,’y’,’z’} • We’ll go with Q3 = {[Q1],[Q2]}
• Q3 = Q1 + Q2 • How this is done (in the .h file first):
• What should Q3 contain, and in what class Queue {
public:
order? // (regular functions go here)

• {‘a’,’x’,’b’,’y’,’c’,’z’}, {‘a’,’b’,’c’,’x’,’y’,’z’}, // now, define that the operator will be


// overloaded. Note only one param:
{‘x’,’y’,’z’,’a’,’b’,’c’},… Queue operator + (const Queue &);

• There are 6! possibilities. Want the …


};
“intuitive” one. // that was the prototype – need the definition

1
Definition in the CPP File Oops
// + operator definition
Queue Queue::operator + (const Queue & rhs) {
//lhs is “this”
Queue result; // new Queue to be returned • That last definition had an error. See what
// store this.contents & rhs.contents in result it was?
for (int i = 0; i < n; i++) {
result.contents[i] =contents[i ];
}
for (int i = n; i < n + rhs.n; i++) {
result.contents[i] =rhs.contents[i-n];
}
// update size
result.n = n+rhs.n;

return result;
}

Fixed Definition We’re not done yet!


// + operator definition
Queue Queue::operator + (const Queue & rhs) {
//lhs is “this”
Queue result; // new Queue to be returned • This code works, but it performs only a
// store this.contents & rhs.contents in result
“shallow” (bit-by-bit) copy:
for (int i = 0; i < n; i++) {
result.contents[i] =contents[i ]; Queue q1,q2,q3;
} q1.Enqueue(‘a’); q2.Enqueue(‘b’);
for (int i = n; i < n+rhs.n && i < MAX_QUEUE_SIZE; i++) q3 = q1 + q2; // what is q3?
{
result.contents[i] =rhs.contents[i-n];
} • This is OK for the class as currently
written (with an array) but not for other
// update size
result.n = min(n+rhs.n, MAX_QUEUE_SIZE);

return result; implementations (like a dynamic array, or


} a linked-list).

class Queue {
public:
Step 3: Assignment Operator // (regular functions go here)
// now, define that the operator will be
// overloaded. Note only one param:
• We now must define the behaviour of the …
Queue & operator = (const Queue &);

“=” operator. };

• We will make the intuitive choice of Queue& Queue::operator = (const Queue &rhs) {

overwriting the data on the lhs with the // make sure not copying onto itself
if (this != & rhs) {
data of the rhs //copy data from old to new
n = rhs.n;
• Add the prototype to the class (.h file) // this is the “deep copy” part
for (int i=0; i<rhs.n; i++) {
• Add the definition to the class (.cpp file) contents[i] = rhs.contents[i];
}
}
return *this;

2
Still Need To…
• Provide a destructor
• Provide a copy -constructor
• Overload other operators?

You might also like