lesson22-overloadingoperators

24
Operator Overloading

Upload: sumant-luhar

Post on 03-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 1/24

Operator Overloading

Page 2: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 2/24

Reading Chapter 8

Lesson 22 - Operator Overload

Page 3: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 3/24

Operators There's many operators in C++

relational (doing comparisons)

logical (joining conditions) arithmetic

also, we recently learned about new and delete

Lesson 22 - Operator Overload

Page 4: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 4/24

Different Behaviours Some operators have multiple behaviours,

depending on the context: e.g. asterisk is multiplication or indirection, depending

on whether it's a binary or unary operator e.g. ampersand is address of, bitwise-AND, and

reference

These operators can be considered overloaded,similar to functions, since they behave differently depending on the items on the right and left of them

 You can overload operators yourself if you wish

Lesson 22 - Operator Overload

Page 5: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 5/24

How to Overload an Operator Need to write a class method that contains the

keyword operator between the return type andoperator being overloaded e.g. Time Time::operator + (Time n); Must be an operator in the C++ language

Must be a class method (not really, but let’s assume sofor now)

This will then allow you to use that operator with thatclass in the context described by the parameters andreturn type

Lesson 22 - Operator Overload

Page 6: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 6/24

Issues Have to be careful about what the parameters and

return type are

 What the parameters and return type are define thecontext under which the overloaded operator will workand what it will do

This way, you can get greater functionality out of yourclasses

 You choose what you want the operator to do

Lesson 22 - Operator Overload

Page 7: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 7/24

Parameters You're always working with the current object

(through the use of the this pointer, if necessary)

The parameter list will provide any additionalobjects or items beside the this pointer

In a binary operator, the this pointer is the LHS objectand the (only) parameter is the RHS object

In a unary operator, it is assumed that the operator is onthe left (prefix form) of the object and that there are noparameters

Lesson 22 - Operator Overload

Page 8: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 8/24

Restrictions Can’t change precedence of operator 

Can’t change number of operands needed by theoperator

Can’t overload .

::

.*

?: (ternary conditional operator)

sizeof 

Lesson 22 - Operator Overload

Page 9: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 9/24

Guidelines for Overloading Operators Super-Major Rule #1: Don’t confuse the users of the class. 

Others: Use common sense. Don’t overload an operator just for the sake of 

having it. If you define arithmetic operators, maintain the usual arithmetic

identities. e.g. If you can do a – b + b, it should equal a.

 You should provide arithmetic operators only when they make

logical sense to users. e.g. Allow adding date + 5 but not 5 + date or date1 + date2.

Look at http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.9

Lesson 22 - Operator Overload

Page 10: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 10/24

Overloading The Assignment Operator Parameter: const reference to the object Return value: const reference to the object, which

should likely be *this e.g.

const three_d & three_d::operator=(const three_d&op2)

{

x = op2.x; // These are integer assignments

y = op2.y; // and the = retains its originalz = op2.z; // meaning relative to them.

return *this;

}

Lesson 22 - Operator Overload

Page 11: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 11/24

Overloading Arithmetic Binary Operators Parameter: const reference to the object or

primitive data type on the right-hand side of theoperator

The textbook doesn’t use this convention and instead just passes it by value

Return value: the object by value The *this object is the object on the left-hand side

of the operator This implies that you must have an object on the left-hand side of the operator in order for this to work  We will cover the situation where you have a primitive data type

on the left-hand side later when we learn about friend functions

Lesson 22 - Operator Overload

Page 12: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 12/24

Example of Overloading an Arithmetic

Binary Operatorthree_d three_d::operator+(const three_d &op2)

{

three_d temp;

temp.x = x + op2.x; // These are integer additions

temp.y = y + op2.y; // and the + retains is originaltemp.z = z + op2.z; // meaning relative to them.

return temp;

}

Lesson 22 - Operator Overload

Page 13: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 13/24

Overloading Arithmetic Prefix Unary

Operators A Prefix Unary Operator is one that has the object on

the right-hand side (e.g. ++obj)

Parameter: none (since it’s unary and the object is onthe right this time)

Return value: the object

The *this object is the object on the right-hand side of 

the operator

Lesson 22 - Operator Overload

Page 14: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 14/24

Example of Arithmetic Prefix

Unary Operator Overloadingthree_d &three_d::operator++()

{

x++; // increment x, y, and z

y++;

z++;

return *this;

}

Lesson 22 - Operator Overload

Page 15: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 15/24

Overloading Arithmetic

Postfix Unary Operators What if you want to overload a postfix unary operator

(i.e. one that has the object on the left-hand side)?

There’s a special case that is used for this  Use a dummy int parameter that you ignore  When used with a unary operator that can be postfix,

this automatically indicates to the compiler that itshould be postfix

Return const object by value

Lesson 22 - Operator Overload

Page 16: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 16/24

Example of Arithmetic Postfix

Unary Operator Overloadingconst three_d three_d::operator++(int)

{

three_d temp;

x++; // increment x, y, and zy++;

z++;

temp.x = x;

temp.y = y;

temp.z = z;return temp;

}

Lesson 22 - Operator Overload

Page 17: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 17/24

Overloading Relational Operators ==, <, >, etc.

Parameter: const reference to the object or primitive

data type on the right-hand side of the operator  Again, the textbook doesn’t use this convention and

instead just passes it by value

Return true or false (as bool)

The textbook returns int instead

Lesson 22 - Operator Overload

Page 18: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 18/24

Example of Overloading a Relational

Operator e.g.

bool three_d::operator==(const three_d &op2)

{

if( (x == op2.x) && (y == op2.y) && (z == op2.z) )return true;

else

return false;

}

Lesson 22 - Operator Overload

Page 19: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 19/24

Exampleint main()

{

three_d a(1, 2, 3), b(10, 10, 10), c;

a.show();

b.show();

c = a + b; // add a and b together, need to overload + and =

c.show();c = a + b + c; // add a, b and c together

c.show();

c = b = a; // demonstrate multiple assignment

c.show();

b.show();

++c; // increment cc.show();

return 0;

}

Lesson 22 - Operator Overload

Page 20: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 20/24

More of the Example...#include <iostream.h>class three_d {

int x, y, z; // 3-D coordinates

public:

three_d() { x = y = z = 0; }

three_d(int i, int j, int k) {x = i; y = j; z = k; }

three_d operator+(const three_d &op2);

const three_d &operator=(const three_d &op2);

const three_d operator++(void); // prefix version of

++void show(void) ;

} ;

// Overload +

three_d three_d::operator+(const three_d &op2)

{

three_d temp;

temp.x = x + op2.x; // These are integer additions

temp.y = y + op2.y; // and the + retains is originaltemp.z = z + op2.z; // meaning relative to them.

return temp;

}

// Overload assignment

const three_d & three_d::operator=(const three_d&op2)

{

x = op2.x; // These are integer assignments

y = op2.y; // and the = retains its original

z = op2.z; // meaning relative to them.

return *this;

}

// Overload the prefix version of ++.

const three_d three_d::operator++()

{

x++; // increment x, y, and z

y++;

z++;

return *this;

}

void three_d::show()

{

cout << x << ", ";

cout << y << ", ";

cout << z << "\n";

}

Lesson 22 - Operator Overload

Page 21: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 21/24

Warning!!!! If you check in various textbooks and on the Internet,

 you will find many different uses of const andreferences for parameters and return types foroverloaded operators and just as many statements thattheir way is the right way 

The justification is usually missing, sooooo ...

Lesson 22 - Operator Overload

Page 22: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 22/24

Justification about Parameters (Binary only) Parameters should usually be const references if 

they are objects because

1.  you usually don’t change the argument on the left orright of a binary operator in normal use so it shouldbe const

2.  you shouldn’t pass objects by value because it’s far

less efficient than passing as a reference

Lesson 22 - Operator Overload

Page 23: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 23/24

Justification about Return Values for

Assignment If the operation is changing the LHS operand, then

 you are really affecting this – so you need to return aconst reference to the object (which is *this)

It makes sense because you are simply assigning to theitem on the left hand side so it’s already taken care of  

Lesson 22 - Operator Overload

Page 24: Lesson22-OverloadingOperators

7/29/2019 Lesson22-OverloadingOperators

http://slidepdf.com/reader/full/lesson22-overloadingoperators 24/24

Justification about Return Values for Other

Arithmetic Operators Object is returned by value

 You have to create a new object (as a local variable)that is assigned to. Then, a copy of the object is

returned. If you were to return a reference to the local variable, it

 would be invalid as soon as the function returned.

If you were to return *this, you would be changing the

object on the right-hand side of the operator.

Lesson 22 - Operator Overload