1 / 34

Introduction to Programming

Introduction to Programming . Lecture 31. Operator Overloading. Today’s Lecture. Operators Syntax for overloading operators How to overload operators ?. Complex Number. complex c1 , c2 , x ; x = cadd ( c1 , c2 ) ;. x = cadd ( cadd ( a , b ) , c ) ;. Operators.

ericbaker
Download Presentation

Introduction to Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Programming Lecture 31

  2. Operator Overloading

  3. Today’s Lecture • Operators • Syntax for overloading operators • How to overload operators ?

  4. Complex Number

  5. complex c1 , c2 , x ; x = cadd ( c1 , c2 ) ;

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

  7. Operators • The complete list of C++ operators that are overloaded is as follows + - * / % ^ & | ~ ! = < > += -= *= /= %= ^= &= |= << >> >>= <<= = = != <= >= && | | ++ - - -> * , -> [ ] ( ) new new[ ] delete delete [ ]

  8. a + b

  9. Date.day

  10. Example Return_type operator + (Argument_List) { // Body of function }

  11. a * b + c ;

  12. x = y + z ;

  13. Example class Complex { private : double real ; double imag ; public : // member function }

  14. Example Complex c1 , c2 ; c1 = c2 ; Is equivalent to c1.real = c2.real ; c1.imag = c2.imag ;

  15. Complex operator + ( Argument_ list ) ;

  16. Example Complex Complex :: operator + ( Complex c ) { Complex temp ; temp.real = real + c.real ; temp.imag = imag + c.imag ; return temp ; }

  17. Complex x , y , z ;z = x + y ;

  18. z = x + d ; Complex Number Complex Number Double Precision Number

  19. Complex operator + ( double d ) ;

  20. z = x + y ;z = x + d ;

  21. Example Complex Complex :: operator + ( Complex c ) { Complex temp ; temp.real = real + d ; temp.imag = imag ; return temp ; }

  22. z = d + x ; Complex Number Complex Number Double Precision Number

  23. Friend Function

  24. User Defined Data types

  25. <<

  26. Example main ( ) { Complex c1 ( 1 , 2 ) , c2 ( 3 , 4 ) , c3 ; c3 = c1 + c2 ; c1.display ( ) ; c2.display ( ) ; c3.display ( ) ; }

  27. Complex operator + ( Complex & c ) ; C is a reference to a complex number

  28. i += 2 ; i = i + 2 ;

  29. c1 += c2 ;

  30. Example Complex operator += ( Complex & c )

  31. Example Complex Complex :: operator += ( Complex & c ) { real += c.real ; imag += c.imag ; }

  32. Example Complex operator + ( Complex & c1 , Complex & c2 ) { Complex temp ; temp.real = c1.getreal ( ) + c2.getreal ( ) ; temp.imag = c1.getimag ( ) + c2.getimag ( ) ; return temp ; }

  33. Example class String { private : char s [ 30 ] ; public : String ( ) { strcpy ( s , "" ) ; } // Declaration (prototype) of overloaded sum operator String operator + ( String c ) ; } ;

  34. Example String String :: operator + ( String c ) { String temp ; strcpy ( temp.s , "" ) ; strcat ( temp.s , s ) ; strcat ( temp.s , c.s ) ; return temp ; }

More Related