Introduction to Programming Lecture 31 Operator Overloading Todays

  • Slides: 34
Download presentation
Introduction to Programming Lecture 31

Introduction to Programming Lecture 31

Operator Overloading

Operator Overloading

Today’s Lecture – Operators – Syntax for overloading operators – How to overload operators

Today’s Lecture – Operators – Syntax for overloading operators – How to overload operators ?

Complex Number

Complex Number

complex c 1 , c 2 , x ; x = cadd ( c

complex c 1 , c 2 , x ; x = cadd ( c 1 , c 2 ) ;

n Operators The complete list of C++ operators that are overloaded is as follows

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

a+b

a+b

Date. day

Date. day

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

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

a*b+c;

a*b+c;

x=y+z;

x=y+z;

Example class Complex { private : double real ; double imag ; public :

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

Example Complex c 1 , c 2 ; c 1 = c 2 ;

Example Complex c 1 , c 2 ; c 1 = c 2 ; Is equivalent to c 1. real = c 2. real ; c 1. imag = c 2. imag ;

Complex operator + ( Argument_ list ) ;

Complex operator + ( Argument_ list ) ;

Example Complex : : operator + ( Complex c ) { Complex temp ;

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

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

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

z=x+d; Complex Number Double Precision Number

z=x+d; Complex Number Double Precision Number

Complex operator + ( double d ) ;

Complex operator + ( double d ) ;

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

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

Example Complex : : operator + ( Complex c ) { Complex temp ;

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

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

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

Friend Function

Friend Function

User Defined Data types

User Defined Data types

Example main ( ) { Complex c 1 ( 1 , 2 ) ,

Example main ( ) { Complex c 1 ( 1 , 2 ) , c 2 ( 3 , 4 ) , c 3 ; c 3 = c 1 + c 2 ; c 1. display ( ) ; c 2. display ( ) ; c 3. display ( ) ; }

Complex operator + ( Complex & c ) ; C is a reference to

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

i += 2 ; i=i+2;

i += 2 ; i=i+2;

Example Complex operator += ( Complex & c )

Example Complex operator += ( Complex & c )

Example Complex : : operator += ( Complex & c ) { real +=

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

Example Complex operator + ( Complex & c 1 , Complex & c 2

Example Complex operator + ( Complex & c 1 , Complex & c 2 ) { Complex temp ; temp. real = c 1. getreal ( ) + c 2. getreal ( ) ; temp. imag = c 1. getimag ( ) + c 2. getimag ( ) ; return temp ; }

class String { private : Example char s [ 30 ] ; public :

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

Example String : : operator + ( String c ) { String temp ;

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