Operator overloading lecture1

32
Object-Oriented Object-Oriented Programming (OOP) Programming (OOP) Lecture No. 16 Lecture No. 16

Transcript of Operator overloading lecture1

Page 1: Operator overloading lecture1

Object-Oriented Object-Oriented Programming (OOP)Programming (OOP)

Lecture No. 16Lecture No. 16

Page 2: Operator overloading lecture1

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 &);

……

}}

Page 3: Operator overloading lecture1

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;

}}

Page 4: Operator overloading lecture1

Operator overloadingOperator overloading

►The following statement:The following statement:

Complex c3 = c1.Add(c2);Complex c3 = c1.Add(c2);

Adds the contents of Adds the contents of c2c2 to to c1c1 and assigns it to and assigns it to c3c3 (copy (copy constructor)constructor)

Page 5: Operator overloading lecture1

Operator overloadingOperator overloading

►To perform operations in a To perform operations in a single mathematical single mathematical statement e.g:statement e.g:

c1+c2+c3+c4c1+c2+c3+c4

►We have to explicitly write:We have to explicitly write:

c1.Add(c2.Add(c3.Add(c4)))c1.Add(c2.Add(c3.Add(c4)))

Page 6: Operator overloading lecture1

Operator overloadingOperator overloading

►Alternative way is:Alternative way is:

t1 = c3.Add(c4);t1 = c3.Add(c4);

t2 = c2.Add(t1);t2 = c2.Add(t1);

t3 = c1.Add(t2);t3 = c1.Add(t2);

Page 7: Operator overloading lecture1

Operator overloadingOperator overloading

►If the mathematical expression If the mathematical expression is big:is big:

Converting it to C++ code will Converting it to C++ code will involve complicated mixture of involve complicated mixture of function callsfunction callsLess readableLess readableChances of human mistakes are Chances of human mistakes are very highvery highCode produced is very hard to Code produced is very hard to maintainmaintain

Page 8: Operator overloading lecture1

Operator overloadingOperator overloading

►C++ provides a very elegant C++ provides a very elegant solution:solution:

““Operator overloadingOperator overloading””►C++ allows you to overload C++ allows you to overload common operators like common operators like ++, , -- or or ** etc…etc…►Mathematical statements don’t Mathematical statements don’t have to be explicitly converted have to be explicitly converted into function callsinto function calls

Page 9: Operator overloading lecture1

Operator overloadingOperator overloading

►Assume that operator Assume that operator + has+ has been overloadedbeen overloaded►Actual C++ code becomes:Actual C++ code becomes:

c1+c2+c3+c4c1+c2+c3+c4

►The resultant code is very The resultant code is very easy to read, write and easy to read, write and maintainmaintain

Page 10: Operator overloading lecture1

Operator overloading Operator overloading ►C++ automatically overloads C++ automatically overloads operators for pre-defined typesoperators for pre-defined types►Example of predefined types:Example of predefined types:

intint

floatfloat

doubledouble

charchar

longlong

Page 11: Operator overloading lecture1

Operator overloading Operator 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;

Page 12: Operator overloading lecture1

Operator overloading Operator overloading

The compiler probably calls the The compiler probably calls the correct overloaded low level function correct overloaded low level function for addition i.e:for addition i.e:

// // for integer addition:for integer addition:

Add(int a, int b)Add(int a, int b)

// // for float addition:for float addition:

Add(float a, float b)Add(float a, float b)

Page 13: Operator overloading lecture1

Operator overloadingOperator overloading

►Operator functions are not Operator functions are not usually called directlyusually called directly

►They are automatically invoked They are automatically invoked to evaluate the operations they to evaluate the operations they implementimplement

Page 14: Operator overloading lecture1

Operator overloadingOperator overloading

►List of operators that can be List of operators that can be overloaded in C++:overloaded in C++:

Page 15: Operator overloading lecture1

Operator overloadingOperator overloading

►List of operators that can’t be List of operators that can’t be overloaded:overloaded:

►Reason: They take name, rather Reason: They take name, rather than value in their argument than value in their argument except for except for ?:?:►?:?: is the only ternary operator is the only ternary operator in C++ and can’t be overloadedin C++ and can’t be overloaded

Page 16: Operator overloading lecture1

Operator overloadingOperator overloading

►The precedence of an The precedence of an operator is operator is NOTNOT affected due affected due to overloadingto overloading

►Example:Example:

c1*c2+c3c1*c2+c3

c3+c2*c1c3+c2*c1

both yield the same answerboth yield the same answer

Page 17: Operator overloading lecture1

Operator overloadingOperator overloading

►Associativity is Associativity is NOTNOT changed due to overloadingchanged due to overloading

►Following arithmetic Following arithmetic expression always is expression always is evaluated from left to right:evaluated from left to right:

c1 + c2 + c3 + c4c1 + c2 + c3 + c4

Page 18: Operator overloading lecture1

Operator overloadingOperator overloading

►Unary operators and Unary operators and assignment operator are right assignment operator are right associative, e.g:associative, e.g:

a=b=ca=b=c is same as is same as a=(b=c)a=(b=c)

►All other operators are left All other operators are left associative:associative:

c1+c2+c3c1+c2+c3 is same as is same as

(c1+c2)+c3(c1+c2)+c3

Page 19: Operator overloading lecture1

Operator overloadingOperator overloading

►Always write code Always write code representing the operatorrepresenting the operator

►Example:Example:

Adding subtraction code inside Adding subtraction code inside the + operator will create the + operator will create chaoschaos

Page 20: Operator overloading lecture1

Operator overloadingOperator overloading

►Creating a new operator is a Creating a new operator is a syntax error (whether unary, syntax error (whether unary, binary or ternary)binary or ternary)

► You cannot create You cannot create $$

Page 21: Operator overloading lecture1

Operator overloadingOperator overloading

►Arity of an operator is NOT Arity of an operator is NOT affected by overloadingaffected by overloading

►Example:Example:

Division operator will take exactly two Division operator will take exactly two operands in any case:operands in any case:

b = c / db = c / d

Page 22: Operator overloading lecture1

Binary operatorsBinary operators►Binary operators act on two Binary operators act on two quantitiesquantities►Binary operators:Binary operators:

Page 23: Operator overloading lecture1

Binary operatorsBinary operators

►General syntax:General syntax:

Member function:Member function:

TYPETYPE11 CLASS::operator B_OP( CLASS::operator B_OP(

TYPETYPE22 rhs){ rhs){

......

}}

Page 24: Operator overloading lecture1

Binary operatorsBinary operators

►General syntax:General syntax:

Non-member function:Non-member function:

TYPETYPE11 operator B_OP(TYPE operator B_OP(TYPE22 lhs, lhs, TYPETYPE33 rhs) rhs)

{{

......

}}

Page 25: Operator overloading lecture1

Binary operatorsBinary operators

►The “The “operator OPoperator OP” must have at ” must have at least one formal parameter of type least one formal parameter of type class (user defined type)class (user defined type)

►Following is an error:Following is an error:

int operator + (int, int);int operator + (int, int);

Page 26: Operator overloading lecture1

Binary operatorsBinary operators►Overloading + operator:Overloading + operator:

class Complex{class Complex{

private:private:

double real, img;double real, img;

public:public:

……

Complex operator +Complex operator +(const (const Complex & Complex & rhs);rhs);

};};

Page 27: Operator overloading lecture1

Binary operatorsBinary operators

Complex Complex::operator +(Complex Complex::operator +(const Complex & rhs){const Complex & rhs){

Complex t;Complex t;

t.real = real + rhs.real;t.real = real + rhs.real;

t.img = img + rhs.img;t.img = img + rhs.img;

return t;return t;

}}

Page 28: Operator overloading lecture1

Binary operatorsBinary operators►The return type is Complex so The return type is Complex so as to facilitate complex as to facilitate complex statements like:statements like:

Complex t = c1 + c2 + c3;Complex t = c1 + c2 + c3;

►The above statement is The above statement is automatically converted by the automatically converted by the compiler into appropriate compiler into appropriate function calls:function calls:

((c1.operator +(c2)c1.operator +(c2))).operator +.operator +(c3);(c3);

Page 29: Operator overloading lecture1

Binary operatorsBinary operators

►If the return type was If the return type was voidvoid, , class Complex{class Complex{

......

public:public:

void operator+(void operator+(

const Complex & rhs);const Complex & rhs);

};};

Page 30: Operator overloading lecture1

Binary operatorsBinary operators

void Complex::operator+(const void Complex::operator+(const Complex & rhs){Complex & rhs){

real = real + rhs.real;real = real + rhs.real;

img = img + rhs.img;img = img + rhs.img;

};};

Page 31: Operator overloading lecture1

Binary operatorsBinary operators

►we have to do the same we have to do the same operation operation c1+c2+c3c1+c2+c3 as: as:

c1+c2c1+c2

c1+c3c1+c3

// final result is stored in c1// final result is stored in c1

Page 32: Operator overloading lecture1

Binary operatorsBinary operators

►Drawback of void return type:Drawback of void return type:Assignments and cascaded Assignments and cascaded expressions are not possibleexpressions are not possibleCode is less readableCode is less readableDebugging is toughDebugging is toughCode is very hard to maintainCode is very hard to maintain