34
Introduction to Programmi Introduction to Programmi Lecture 31 Lecture 31

Introduction to Programming Lecture 31. Operator Overloading

Embed Size (px)

Citation preview

Introduction to ProgrammingIntroduction to Programming

Lecture 31Lecture 31

Operator Operator OverloadingOverloading

Today’s LectureToday’s Lecture

– OperatorsOperators– Syntax for overloading Syntax for overloading

operatorsoperators– How to overload How to overload operators ?operators ?

Complex Complex NumberNumber

complex c1 , c2 , x ;complex c1 , c2 , x ;

x = cadd ( c1 , c2 ) ;x = cadd ( c1 , c2 ) ;

x = cadd ( cadd ( a , b ) , c ) ;x = cadd ( cadd ( a , b ) , c ) ;

OperatorsOperators The complete list of C++ operators that are The complete list of C++ operators that are

overloaded is as followsoverloaded is as follows

+ - * / % ^ & + - * / % ^ & | ~ ! = < > += | ~ ! = < > += -= *= /= %= ^= &= |= -= *= /= %= ^= &= |= << >> >>= <<= = = != <= << >> >>= <<= = = != <= >= && | | ++ - - -> * , >= && | | ++ - - -> * , -> [ ] ( ) new new[ ] delete -> [ ] ( ) new new[ ] delete delete [ ]delete [ ]

a + ba + b

Date.dayDate.day

Example Example

Return_type operator + Return_type operator + (Argument_List)(Argument_List)

{{

// Body of function// Body of function

}}

a * b + c ;a * b + c ;

x = y + z ;x = y + z ;

Example Example class Complexclass Complex{{

private :private :double real ;double real ;double imag ;double imag ;

public :public :// member function// member function

}}

ExampleExampleComplex c1 , c2 ;Complex c1 , c2 ;

c1 = c2 ;c1 = c2 ;

Is equivalent toIs equivalent to

c1.real = c2.real ;c1.real = c2.real ;

c1.imag = c2.imag ; c1.imag = c2.imag ;

Complex operator + ( Argument_ Complex operator + ( Argument_ list ) ;list ) ;

Complex Complex :: operator + ( Complex Complex Complex :: operator + ( Complex c )c )

{{

Complex temp ;Complex temp ;

temp.real = real + c.real ;temp.real = real + c.real ;

temp.imag = imag + c.imag ;temp.imag = imag + c.imag ;

return temp ;return temp ;

}}

ExampleExample

Complex x , y , Complex x , y , z ;z ;

z = x + y ;z = x + y ;

z = x + d ;z = x + d ;Complex Number

Complex Number

DoublePrecision Number

Complex operator + ( double d Complex operator + ( double d ) ;) ;

z = x + y ;z = x + y ;z = x + d ;z = x + d ;

Complex Complex :: operator + ( Complex Complex Complex :: operator + ( Complex c )c )

{{

Complex temp ;Complex temp ;

temp.real = real + d ; temp.real = real + d ;

temp.imag = imag ;temp.imag = imag ;

return temp ;return temp ;

}}

ExampleExample

z = d + z = d + x ;x ;

Complex Number Double

Precision Number

Complex Number

Friend Friend FunctionFunction

User DefinedUser Defined

Data typesData types

<<<<

Example Example main ( )main ( )

{{

Complex c1 ( 1 , 2 ) , c2 ( 3 , 4 ) , Complex c1 ( 1 , 2 ) , c2 ( 3 , 4 ) , c3 ;c3 ;

c3 = c1 + c2 ;c3 = c1 + c2 ;

c1.display ( ) ;c1.display ( ) ;

c2.display ( ) ;c2.display ( ) ;

c3.display ( ) ;c3.display ( ) ;

}}

Complex operator + ( Complex & c Complex operator + ( Complex & c ) ;) ;

C is a reference to a complex number

i += 2 ;i += 2 ;

i = i + 2 ;i = i + 2 ;

c1 += c1 += c2 ;c2 ;

ExampleExample

Complex operator += ( Complex & Complex operator += ( Complex & c )c )

ExampleExampleComplex Complex :: operator += ( Complex & c )Complex Complex :: operator += ( Complex & c ){{

real += c.real ;real += c.real ;imag += c.imag ;imag += c.imag ;

}}

Complex operator + ( Complex & c1 , Complex & c2 )Complex operator + ( Complex & c1 , Complex & c2 )

{{Complex temp ;Complex temp ;temp.real = c1.getreal ( ) + c2.getreal ( ) ;temp.real = c1.getreal ( ) + c2.getreal ( ) ;temp.imag = c1.getimag ( ) + c2.getimag ( ) temp.imag = c1.getimag ( ) + c2.getimag ( ) ;;return temp ;return temp ;

}}

ExampleExample

ExampleExampleclass Stringclass String{{ private :private :

char s [ 30 ] ;char s [ 30 ] ;

public :public :

String ( ) String ( ) {{ strcpy ( s , "" ) ;strcpy ( s , "" ) ; }}

// Declaration (prototype) of overloaded sum operator// Declaration (prototype) of overloaded sum operator

String operator + ( String c ) ;String operator + ( String c ) ;} ;} ;

Example Example String String :: operator + ( String c )String String :: operator + ( String c )

{ {

String temp ;String temp ;

strcpy ( temp.s , "" ) ;strcpy ( temp.s , "" ) ;

strcat ( temp.s , s ) ;strcat ( temp.s , s ) ;

strcat ( temp.s , c.s ) ;strcat ( temp.s , c.s ) ;

return temp ;return temp ;

}}