cs201- introduction to programming- lecture 31

34
Introduction to Programmi Introduction to Programmi Lecture 31 Lecture 31

Upload: bilal-ahmed

Post on 18-Dec-2014

23 views

Category:

Education


6 download

DESCRIPTION

Virtual University Course CS201- Introduction to Programming Lecture No 31 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

TRANSCRIPT

Page 1: CS201- Introduction to Programming- Lecture 31

Introduction to ProgrammingIntroduction to Programming

Lecture 31Lecture 31

Page 2: CS201- Introduction to Programming- Lecture 31

Operator Operator OverloadingOverloading

Page 3: CS201- Introduction to Programming- Lecture 31

Today’s LectureToday’s Lecture

– OperatorsOperators– Syntax for overloading Syntax for overloading

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

Page 4: CS201- Introduction to Programming- Lecture 31

Complex Complex NumberNumber

Page 5: CS201- Introduction to Programming- Lecture 31

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

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

Page 6: CS201- Introduction to Programming- Lecture 31

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

Page 7: CS201- Introduction to Programming- Lecture 31

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 [ ]

Page 8: CS201- Introduction to Programming- Lecture 31

a + ba + b

Page 9: CS201- Introduction to Programming- Lecture 31

Date.dayDate.day

Page 10: CS201- Introduction to Programming- Lecture 31

Example Example

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

{{

// Body of function// Body of function

}}

Page 11: CS201- Introduction to Programming- Lecture 31

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

Page 12: CS201- Introduction to Programming- Lecture 31

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

Page 13: CS201- Introduction to Programming- Lecture 31

Example Example class Complexclass Complex{{

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

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

}}

Page 14: CS201- Introduction to Programming- Lecture 31

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 ;

Page 15: CS201- Introduction to Programming- Lecture 31

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

Page 16: CS201- Introduction to Programming- Lecture 31

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

Page 17: CS201- Introduction to Programming- Lecture 31

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

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

Page 18: CS201- Introduction to Programming- Lecture 31

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

Complex Number

DoublePrecision Number

Page 19: CS201- Introduction to Programming- Lecture 31

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

Page 20: CS201- Introduction to Programming- Lecture 31

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

Page 21: CS201- Introduction to Programming- Lecture 31

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

Page 22: CS201- Introduction to Programming- Lecture 31

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

Complex Number Double

Precision Number

Complex Number

Page 23: CS201- Introduction to Programming- Lecture 31

Friend Friend FunctionFunction

Page 24: CS201- Introduction to Programming- Lecture 31

User DefinedUser Defined

Data typesData types

Page 25: CS201- Introduction to Programming- Lecture 31

<<<<

Page 26: CS201- Introduction to Programming- Lecture 31

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

}}

Page 27: CS201- Introduction to Programming- Lecture 31

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

C is a reference to a complex number

Page 28: CS201- Introduction to Programming- Lecture 31

i += 2 ;i += 2 ;

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

Page 29: CS201- Introduction to Programming- Lecture 31

c1 += c1 += c2 ;c2 ;

Page 30: CS201- Introduction to Programming- Lecture 31

ExampleExample

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

Page 31: CS201- Introduction to Programming- Lecture 31

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

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

}}

Page 32: CS201- Introduction to Programming- Lecture 31

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

Page 33: CS201- Introduction to Programming- Lecture 31

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

Page 34: CS201- Introduction to Programming- Lecture 31

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 ;

}}