Docsity

Operator Overloading and Assignment Operators in C++: A Deep Dive, Slides of Computer Programming

Partial preview of the text

Download Operator Overloading and Assignment Operators in C++: A Deep Dive and more Slides Computer Programming in PDF only on Docsity!

Introduction to Programming

Lecture 33

In Today’s Lecture

 Operator overloading

 Assignment operator

 A “this” pointer

 Operator over loading using this pointer

 Conversion function

a = b ;

Member Wise Assignment

Example class String { char buf [ 100 ] ; public: String ( ) ; ~ String ( ) ; ... } ; Docsity.com

main ( ) { String s1 ( “This is a test” ) ; String s2 ; s2 = s1 ; … … } Example

Example void String :: operator = ( String & s1 ) { delete buf ; buf = new char [ s1.buf + 1 ] ; strcpy ( buf , s1.buf ) ; }

Assignment Operator int i , j , k ; i = 5 ; j = 10 ; k = 15 ; i = j ; k = i ;

k = i = ++ j ;

this pointer

int i ; i = i ; // nothing happens

String s [ 1000 ] = { “This is a Test” } ;

s = s ;

main ( )

String s , * sPtr ;

sPtr = & s ;

s = * sPtr ;

Example

void String :: operator= ( String & other )
if ( this == & other )
return ;

Example