13

Click here to load reader

Operator Overloading

Embed Size (px)

DESCRIPTION

This is a short slide on Operator Overloading With example for easy understanding.

Citation preview

Page 1: Operator Overloading
Page 2: Operator Overloading

Operator Overloading- It is one of the interesting and useful feature of OOP.

Definition - The ability to associate an existing operator with a member function and use it with objects of its classes as its operand is called operator overloading.

Format of the member function using the operator keyword:

return_ type operator op (arguments/parameters)

Description of operator overloading- Urinary operators. Binary operators.

o Arithmetic operatorso Compound assignment operatorso Comparison operators.

Page 3: Operator Overloading

The unary operators operate on a single operand and following are the examples of Unary operators:The increment (++) and decrement (--) operator.The unary minus (-) operator.The logical not (!) operator.The unary operators operate on the object for which they were called and normally, this operator appears on the left side of the object, as in ‘!’obj , ‘-’obj , and ‘++’obj but sometime they can be used as postfix as well like obj ‘++’ or obj ‘--’ .

Unary operator-

Page 4: Operator Overloading

/* Simple example to demonstrate the working of operator overloading*/#include <iostream>using namespace std;class temp{ private: int count; public: temp():count(5) {} void operator +() { count=count*2; } void Display() { cout<<"Count: "<<count; }};int main(){ temp t; +t; /* operator function void operator +() is called */ t.Display(); return 0;}

Page 5: Operator Overloading

Binary operator-Binary operator-

Binary operators are operators with two differentBinary operators are operators with two differentoperands. They can also be overloaded just likeoperands. They can also be overloaded just likeunary operator.unary operator.Two ways to overload a binary operator-Two ways to overload a binary operator-► As friend function they take two arguments-As friend function they take two arguments-

operator + (sample obj1, sample obj2 )operator + (sample obj1, sample obj2 )- One argument must be class object or reference to a- One argument must be class object or reference to a

class object.class object.

► As member function they take one argument-As member function they take one argument-operator + (sample obj1)operator + (sample obj1)

Page 6: Operator Overloading

1. Binary Arithmetic operator-1. Binary Arithmetic operator-

Arithmetic operators are binary operator so they Arithmetic operators are binary operator so they require two operands to perform the operations.require two operands to perform the operations.

Sample Sample :: operator +(sample a)Sample Sample :: operator +(sample a){{Sample temp;Sample temp; //temporary object//temporary objectTemp.counter=counter+a.counter;Temp.counter=counter+a.counter; //addition//additionReturn temp;Return temp; //return temp object//return temp object}}Now we are able to perform the addition of the Now we are able to perform the addition of the

objects with a statement,objects with a statement,obj3=obj1+obj2;obj3=obj1+obj2; //objects of class //objects of class

samplesample

Page 7: Operator Overloading

2. Compound assignment operator-2. Compound assignment operator-

You can overload the assignment operator (=) justYou can overload the assignment operator (=) just

as you can other operators and it can be used toas you can other operators and it can be used to

create an object just like the copy constructor.create an object just like the copy constructor.

void sample :: operator +=(sample a)void sample :: operator +=(sample a){ counter += a.counter; }{ counter += a.counter; } //assignment and //assignment and additionaddition

In this eg. there is no need for a temp. object. The function In this eg. there is no need for a temp. object. The function also needs no return value because the result of the also needs no return value because the result of the assignment operator is not assigned to anything.assignment operator is not assigned to anything.

Page 8: Operator Overloading

3. Comparison operator-3. Comparison operator-

Comparison and logical operator are Comparison and logical operator are binarybinary

operators that need two objects to beoperators that need two objects to be

Compared. The comparison operators Compared. The comparison operators that can be overloaded include that can be overloaded include <,>,<=,>=,==,!=. <,>,<=,>=,==,!=.

Page 9: Operator Overloading

+ - /

& | !

< > >=

<< >> !=

+= -= %=

|= *= >>=

-> ->* new [ ]

^ * %

= ~ ,

-- <= ++

|| == &&

&= /= ^=

() <<= [ ]

delete [ ] new delete

:: .*

. ?:

Overload able / non Overload able OperatorOverload able

operator-Non-overload able operator-

Page 10: Operator Overloading

Example-Example-

#include <iostream> class example { public:     int a;     int b;     example operator+(const example& obj);     void operator=(const example& obj); }; void example::operator=(const example& obj) {     (*this).a = obj.a;     (*this).b = obj.b;     return; }

Continued ....

Page 11: Operator Overloading

example example::operator+(const example& obj2) {     example tmp_obj = *this;     tmp_obj.a = tmp_obj.a + obj2.a;     tmp_obj.b = tmp_obj.b + obj2.b;     return tmp_obj; } int main(void) {    example obj1, obj2, obj3;     obj1.a = 3;     obj1.b = 2;     obj2.a = 5;     obj2.b = 4;     obj3.a = 0;     obj3.b = 0;     obj3 = obj1 + obj2;     std::cout<<obj3.a<<“\n"<<obj3.b<<"\n";     return 0; }

Page 12: Operator Overloading

Rules for overloading operator-

It looks simple to redefine the operators, but

there are some restriction in over loading them.

1. Only existing operators can be overloaded.

2. One operand must be of user defined type.

3. Syntax rule is same for both original operators and overloaded operators.

4. Some operators cannot be overloaded.

5. Friend function cannot be used to overload the operators but member function can be used.

Page 13: Operator Overloading

6. Unary operator, overloaded by the member function take no explicit argument and return no value. however friend function require one argument

7. Binary operators require one argument and those which are overloaded through a friend function require two arguments.

8. When using a binary operator overloaded through the member function, the left hand operator must be an object of the relevant class.

9. Binary operators such as +,-,* and / must return a value. They must not attempt to change there own argument.