Top Banner
Introduction to Introduction to Programming Programming Lecture 32 Lecture 32
26

CS201- Introduction to Programming- Lecture 32

Dec 18, 2014

Download

Education

Bilal Ahmed

Virtual University
Course CS201- Introduction to Programming
Lecture No 32

Instructor's Name: Dr. Naveed A. Malik

Course Email: [email protected]
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS201- Introduction to Programming- Lecture 32

Introduction to Introduction to ProgrammingProgramming

Lecture 32Lecture 32

Page 2: CS201- Introduction to Programming- Lecture 32

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

ExampleExample

Complex operator - ( Complex c ) ;Complex operator - ( Complex c ) ;

Page 4: CS201- Introduction to Programming- Lecture 32

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

ExampleExample

void operator -= ( Complex c ) ;void operator -= ( Complex c ) ;

Page 6: CS201- Introduction to Programming- Lecture 32

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

Date operator + ( int Date operator + ( int i ) ;i ) ;

Page 8: CS201- Introduction to Programming- Lecture 32

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

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

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

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

Unary Unary OperatorOperator

Page 13: CS201- Introduction to Programming- Lecture 32

i ++i ++

i --i --

Page 14: CS201- Introduction to Programming- Lecture 32

date += 1 ; date += 1 ; Same asSame asdate ++ ;date ++ ;

Page 15: CS201- Introduction to Programming- Lecture 32

Date operator++ Date operator++ ( ) ;( ) ;

Page 16: CS201- Introduction to Programming- Lecture 32

Unary Unary Member Member OperatorOperator

Page 17: CS201- Introduction to Programming- Lecture 32

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

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

Code Code Reuse Reuse

Page 20: CS201- Introduction to Programming- Lecture 32

Comparison OperatorComparison Operator<<

>>

<=<=

>=>=

====

Page 21: CS201- Introduction to Programming- Lecture 32

boolbool

Page 22: CS201- Introduction to Programming- Lecture 32

bool operator > ( Date d ) bool operator > ( Date d ) ;;

Page 23: CS201- Introduction to Programming- Lecture 32

ExampleExample

Date d1 , d2 ;Date d1 , d2 ;

if ( d1 > d2 )if ( d1 > d2 )

Page 24: CS201- Introduction to Programming- Lecture 32

Date date ;Date date ;

date + 5 ;date + 5 ;

ExampleExample

Page 25: CS201- Introduction to Programming- Lecture 32

ExampleExample

5 + date ;5 + date ;

Page 26: CS201- Introduction to Programming- Lecture 32

InterfacInterfacee