{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# CS201-32 - Introduction to Programming Lecture 32 – What...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Programming Lecture 32 – What is operator overloading ? – Overload operators We also saw the – Binary Operators – Unary Operators – Member and Non­member operators In Last Lecture Example Complex operator ­ ( Complex c ) ; Complex Complex :: operator ­ ( Complex c ) { Complex Temp ; Temp.real = real ­ c.real ; Temp.imag = imag ­ c.imag ; return Temp ; } Example Example void operator ­= ( Complex c ) ; Example void Complex :: operator ­= ( Complex c ) { real ­= c.real ; imag ­= c.imag ; } Date operator + ( int i ) ; Example Date Date :: operator + ( int days ) { Date Temp ; int toHold ; int daysInThisMonth = 0 ; daysInThisMonth = daysInMonth ( month ) ; toHold = days ; if ( ( day + days ) >= daysInThisMonth ) { Temp.month = month + 1 ; if ( Temp.month > 12 ) { Temp.day = 1 ; Temp.month = 1 ; Temp.year = year + 1 ; } Example if ( ( day + days ) >= daysInThisMonth ) else { toHold = day + days ; if ( toHold > daysInThisMonth ) { Temp.day = toHold ­ daysInThisMonth ; } Temp.year = year ; } } Example else Example { Temp.day = days + day ; Temp.month = month ; Temp.year = year ; } return Temp ; } Unary Operator i ++ i ­­ date += 1 ; Same as date ++ ; Date operator++ ( ) ; Unary Member Operator void Date :: operator ++ ( ) { if ( day == daysOfMonth ( day , month , year ) ) if { if ( month < 12 ) { day = 1 ; day month ++ ; } else { day = 1 ; day month = 1 ; month year ++ ; year } else day ++ ; day } Example Date Date :: operator + ( int days ) { days = 5 Date Temp ; for ( i = 1 ; i < days ; i ++ ) ++ Temp ; return Temp ; Example } Code Reuse Comparison Operator < > <= >= bool bool operator > ( Date d ) ; Example Date d1 , d2 ; if ( d1 > d2 ) Date date ; date + 5 ; Example Example 5 + date ; Interface ...
View Full Document

{[ snackBarMessage ]}