cs201- introduction to programming- lecture 31
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](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/1.jpg)
Introduction to ProgrammingIntroduction to Programming
Lecture 31Lecture 31
![Page 2: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/2.jpg)
Operator Operator OverloadingOverloading
![Page 3: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/3.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/4.jpg)
Complex Complex NumberNumber
![Page 5: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/5.jpg)
complex c1 , c2 , x ;complex c1 , c2 , x ;
x = cadd ( c1 , c2 ) ;x = cadd ( c1 , c2 ) ;
![Page 6: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/6.jpg)
x = cadd ( cadd ( a , b ) , c ) ;x = cadd ( cadd ( a , b ) , c ) ;
![Page 7: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/8.jpg)
a + ba + b
![Page 9: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/9.jpg)
Date.dayDate.day
![Page 10: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/11.jpg)
a * b + c ;a * b + c ;
![Page 12: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/12.jpg)
x = y + z ;x = y + z ;
![Page 13: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/13.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/14.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/15.jpg)
Complex operator + ( Argument_ Complex operator + ( Argument_ list ) ;list ) ;
![Page 16: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/17.jpg)
Complex x , y , Complex x , y , z ;z ;
z = x + y ;z = x + y ;
![Page 18: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/18.jpg)
z = x + d ;z = x + d ;Complex Number
Complex Number
DoublePrecision Number
![Page 19: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/19.jpg)
Complex operator + ( double d Complex operator + ( double d ) ;) ;
![Page 20: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/20.jpg)
z = x + y ;z = x + y ;z = x + d ;z = x + d ;
![Page 21: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/22.jpg)
z = d + z = d + x ;x ;
Complex Number Double
Precision Number
Complex Number
![Page 23: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/23.jpg)
Friend Friend FunctionFunction
![Page 24: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/24.jpg)
User DefinedUser Defined
Data typesData types
![Page 25: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/25.jpg)
<<<<
![Page 26: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/26.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/27.jpg)
Complex operator + ( Complex & c Complex operator + ( Complex & c ) ;) ;
C is a reference to a complex number
![Page 28: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/28.jpg)
i += 2 ;i += 2 ;
i = i + 2 ;i = i + 2 ;
![Page 29: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/29.jpg)
c1 += c1 += c2 ;c2 ;
![Page 30: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/30.jpg)
ExampleExample
Complex operator += ( Complex & Complex operator += ( Complex & c )c )
![Page 31: CS201- Introduction to Programming- Lecture 31](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/31.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/32.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/33.jpg)
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](https://reader033.vdocuments.us/reader033/viewer/2022051514/5492e751b47959ad258b47f6/html5/thumbnails/34.jpg)
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 ;
}}