Upload
amy-dering
View
229
Download
2
Embed Size (px)
Citation preview
Today’s LectureToday’s Lecture
– OperatorsOperators– Syntax for overloading Syntax for overloading
operatorsoperators– How to overload How to overload operators ?operators ?
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 [ ]
Example Example
Return_type operator + Return_type operator + (Argument_List)(Argument_List)
{{
// Body of function// Body of function
}}
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 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 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
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
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 ;
}}