![Page 1: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/1.jpg)
Introduction to Introduction to ProgrammingProgramming
Lecture 32Lecture 32
![Page 2: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/2.jpg)
In Last LectureIn Last Lecture– What is operator overloading ?What is operator overloading ?– Overload operatorsOverload operators
We also saw theWe also saw the– Binary OperatorsBinary Operators– Unary OperatorsUnary Operators– Member and Non-member Member and Non-member operatorsoperators
![Page 3: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/3.jpg)
ExampleExample
Complex operator - ( Complex c ) ;Complex operator - ( Complex c ) ;
![Page 4: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/4.jpg)
Complex Complex :: operator - ( Complex c )Complex Complex :: operator - ( Complex 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 5: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/5.jpg)
ExampleExample
void operator -= ( Complex c ) ;void operator -= ( Complex c ) ;
![Page 6: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/6.jpg)
void Complex :: operator -= ( Complex c )void Complex :: operator -= ( Complex c )
{{
real -= c.real ;real -= c.real ;
imag -= c.imag ;imag -= c.imag ;
}}
ExampleExample
![Page 7: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/7.jpg)
Date operator + ( int Date operator + ( int i ) ;i ) ;
![Page 8: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/8.jpg)
Date Date :: operator + ( int days )Date Date :: operator + ( int days ){{
Date Temp ;Date Temp ; int toHold ;int toHold ;
int daysInThisMonth = 0 ;int daysInThisMonth = 0 ; daysInThisMonth = daysInMonth ( month ) ;daysInThisMonth = daysInMonth ( month ) ; toHold = days ;toHold = days ;
ExampleExample
![Page 9: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/9.jpg)
if ( ( day + days ) >= daysInThisMonth )if ( ( day + days ) >= daysInThisMonth ) {{ Temp.month = month + 1 ;Temp.month = month + 1 ; if ( Temp.month > 12 )if ( Temp.month > 12 ) {{ Temp.day = 1 ;Temp.day = 1 ; Temp.month = 1 ;Temp.month = 1 ; Temp.year = year + 1 ;Temp.year = year + 1 ; }}
ExampleExample
![Page 10: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/10.jpg)
elseelse {{ toHold = day + days ;toHold = day + days ; if ( toHold > daysInThisMonth )if ( toHold > daysInThisMonth ) {{ Temp.day = toHold - daysInThisMonth ;Temp.day = toHold - daysInThisMonth ; }} Temp.year = year ;Temp.year = year ; }} }}
ExampleExample
![Page 11: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/11.jpg)
elseelse {{ Temp.day = days + day ;Temp.day = days + day ; Temp.month = month ;Temp.month = month ; Temp.year = year ;Temp.year = year ; }} return Temp ;return Temp ;}}
ExampleExample
![Page 12: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/12.jpg)
Unary Unary OperatorOperator
![Page 13: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/13.jpg)
i ++i ++
i --i --
![Page 14: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/14.jpg)
date += 1 ; date += 1 ; Same asSame asdate ++ ;date ++ ;
![Page 15: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/15.jpg)
Date operator++ Date operator++ ( ) ;( ) ;
![Page 16: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/16.jpg)
Unary Unary Member Member OperatorOperator
![Page 17: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/17.jpg)
ExampleExamplevoid Date :: operator ++ ( )void Date :: operator ++ ( ){{ if ( day == daysOfMonth ( day , month , year ) )if ( day == daysOfMonth ( day , month , year ) ) {{
if ( month < 12 )if ( month < 12 ){{
day = 1 ;day = 1 ;month ++ ;month ++ ;
}}elseelse{{ day = 1 ;day = 1 ; month = 1 ;month = 1 ;
year ++ ;year ++ ; }} else else
day ++ ;day ++ ;} }
![Page 18: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/18.jpg)
ExampleExampleDate Date :: operator + ( int days )Date Date :: operator + ( int days ){{ Date Temp ;Date Temp ;
for ( i = 1 ; i < days ; i ++ )for ( i = 1 ; i < days ; i ++ )++ ++ Temp Temp ;;
return Temp ;return Temp ;
}}
days = 5
![Page 19: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/19.jpg)
Code Code Reuse Reuse
![Page 20: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/20.jpg)
Comparison OperatorComparison Operator<<
>>
<=<=
>=>=
====
![Page 21: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/21.jpg)
boolbool
![Page 22: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/22.jpg)
bool operator > ( Date d ) bool operator > ( Date d ) ;;
![Page 23: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/23.jpg)
ExampleExample
Date d1 , d2 ;Date d1 , d2 ;
if ( d1 > d2 )if ( d1 > d2 )
![Page 24: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/24.jpg)
Date date ;Date date ;
date + 5 ;date + 5 ;
ExampleExample
![Page 25: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/25.jpg)
ExampleExample
5 + date ;5 + date ;
![Page 26: CS201- Introduction to Programming- Lecture 32](https://reader033.vdocuments.site/reader033/viewer/2022061211/5492e750b479596e4f8b460e/html5/thumbnails/26.jpg)
InterfacInterfacee