Like this presentation? Why not share!

## by Abdul Waqar, Student at University Of Central Punjab on Jul 22, 2013

• 283 views

### Views

Total Views
283
Views on SlideShare
283
Embed Views
0

Likes
1
4
0

No embeds

### Categories

Uploaded via SlideShare as Microsoft PowerPoint

### Report content

• Object-Oriented ProgrammingObject-Oriented Programming (OOP)(OOP) Lecture No. 16Lecture No. 16
• Operator overloadingOperator overloading ►Consider the following class:Consider the following class: class Complex{class Complex{ private:private: double real, img;double real, img; public:public: Complex Add(const Complex &);Complex Add(const Complex &); Complex Subtract(const Complex &);Complex Subtract(const Complex &); Complex Multiply(const Complex &);Complex Multiply(const Complex &); …… }}
• Operator overloadingOperator overloading ►Function implementation:Function implementation: Complex Complex::Add(Complex Complex::Add( const Complex & c1){const Complex & c1){ Complex t;Complex t; t.real = real + c1.real;t.real = real + c1.real; t.img = img + c1.img;t.img = img + c1.img; return t;return t; }}
• Operator overloadingOperator overloading ►If the mathematical expression isIf the mathematical expression is big:big: Converting it to C++ code will involveConverting it to C++ code will involve complicated mixture of function callscomplicated mixture of function calls Less readableLess readable Chances of human mistakes are veryChances of human mistakes are very highhigh Code produced is very hard to maintainCode produced is very hard to maintain
• Operator overloadingOperator overloading ►C++ provides a very elegantC++ provides a very elegant solution:solution: ““Operator overloadingOperator overloading ”” ►C++ allows you to overloadC++ allows you to overload common operators likecommon operators like ++,, -- oror ** etc…etc… ►Mathematical statements don’t haveMathematical statements don’t have to be explicitly converted into functionto be explicitly converted into function callscalls
• Operator overloadingOperator overloading ►Example:Example: float x;float x; int y;int y; x = 102.02 + 0.09;x = 102.02 + 0.09; Y = 50 + 47;Y = 50 + 47;
• Operator overloadingOperator overloading ►Operator functions are not usuallyOperator functions are not usually called directlycalled directly ►They are automatically invoked toThey are automatically invoked to evaluate the operations theyevaluate the operations they implementimplement
• Operator overloadingOperator overloading ►List of operators that can’t beList of operators that can’t be overloaded:overloaded: ►Reason: They take name, ratherReason: They take name, rather than value in their argument exceptthan value in their argument except forfor ?:?: ►?:?: is the only ternary operator inis the only ternary operator in C++ and can’t be overloadedC++ and can’t be overloaded
• Operator overloadingOperator overloading ►Unary operators and assignmentUnary operators and assignment operator are right associative, e.g:operator are right associative, e.g: a=b=ca=b=c is same asis same as a=(b=c)a=(b=c) ►All other operators are leftAll other operators are left associative:associative: c1+c2+c3c1+c2+c3 is same asis same as (c1+c2)+c3(c1+c2)+c3