CS201-33 - Introduction to Programming Lecture 33 In...

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Programming Lecture 33 In Today’s Lecture Operator overloading Assignment operator A “this” pointer Operator over loading using this pointer Conversion function Assignment Operator a = b ; Member Wise Assignment Member Wise Copy Example class String class String { char buf [ 100 ] ; public: String ( ) ; ~ String ( ) ; ... } ; main ( ) { String s1 ( “This is a test” ) ; String s2 ; s2 = s1 ; … … } Example s2 = s1 ; Example void String :: operator = ( String & s1 ) { delete buf ; buf = new char [ s1.buf + 1 ] ; strcpy ( buf , s1.buf ) ; } int i , j , k ; i = 5 ; j = 10 ; k = 15 ; i = j ; k = i ; Assignment Operator i = j = k ; k = i = j ; k = i = ++ j ; this pointer buf ; this pointer this ­> buf ; ( * this ).buf ; int i ; i = i ; // nothing happens String s [ 1000 ] = { “This is a Test” } ; s = s ; Self Assignment main ( ) { String s , * sPtr ; sPtr = & s ; . . . s = * sPtr ; } Example Example void String :: operator= ( String & other ) { if ( this == & other ) return ; ­­­­­­­­ } String & String :: operator = ( String & other ) { if ( this == & other ) return * this ; delete buf ; buf = new char [ other.length + 1 ] ; strcpy ( buf , other.buf ) ; return * this ; } Example s3 = s2 = s1 ; cout << a << b << c ; Example Date d1 , d2 ; d2 = d1 ++ ; d2 = d1 + 1 ; Example Date date1 , date2 ; date2 = date1 + 1 ; date2 = date1 ++ ; main ( ) { int i ; float x ; x = i ; i = x ; } Example main ( ) { Date d ; int i ; d = i ; } Example ? main ( ) { int i ; float x ; x = ( float ) i ; } Example Conversion Function Date ( ) { // body of function } double x = 1 / 3 ; Output: 0.33333 x * 3 ; Output: 0.99999 Common Business Oriented Language ...
View Full Document

This note was uploaded on 01/12/2010 for the course CS CS 201 taught by Professor Dr.naveedmalik during the Spring '09 term at Virtual University of Pakistan.

Ask a homework question - tutors are online