CS201- Introduction to Programming- Lecture 33

Post on 18-Dec-2014

19 views 1 download

Tags:

description

Virtual University Course CS201- Introduction to Programming Lecture No 33 Instructor's Name: Dr. Naveed A. Malik Course Email: cs201@vu.edu.pk

Transcript of CS201- Introduction to Programming- Lecture 33

Introduction to Introduction to ProgrammingProgrammingLecture 33Lecture 33

In Today’s In Today’s Lecture Lecture

Operator overloadingOperator overloading Assignment operatorAssignment operator A “this” pointerA “this” pointer Operator over loading using this pointer Operator over loading using this pointer Conversion functionConversion function

AssignmenAssignment Operatort Operator

a = b ;a = b ;

Member Member Wise Wise

AssignmentAssignment

Member Wise Member Wise CopyCopy

ExampleExampleclass Stringclass String

{{

char buf [ 100 ] ;char buf [ 100 ] ;

public:public:

String ( ) ;String ( ) ;

~ String ( ) ;~ String ( ) ;

......

} ;} ;

main ( )main ( )

{{

String s1 ( “This is a test” ) ; String s1 ( “This is a test” ) ;

String s2 ;String s2 ;

s2 = s1 ;s2 = s1 ;

……

……

}}

ExampleExample

s2 = s2 = s1 ;s1 ;

ExampleExamplevoid String :: operator = ( String & void String :: operator = ( String &

s1 )s1 )

{{

delete buf ;delete buf ;

buf = new char [ s1.buf + 1 ] ;buf = new char [ s1.buf + 1 ] ;

strcpy ( buf , s1.buf ) ;strcpy ( buf , s1.buf ) ;

}}

Assignment Assignment OperatorOperatorint i , j , k ;int i , j , k ;

i = 5 ;i = 5 ;j = 10 ;j = 10 ;k = 15 ;k = 15 ;i = j ;i = j ;k = i ;k = i ;

i = j = k ;i = j = k ;k = i = j ;k = i = j ;

k = i = ++ k = i = ++ j ;j ;

this this pointerpointer

buf ;buf ;this this -> -> buf ; buf ; ( * this ).buf ;( * this ).buf ;

this pointerthis pointer

int i ;int i ; i = i ; i = i ; // nothing happens// nothing happens

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

s = s ;s = s ;

Self Self AssignmentAssignment

main ( )main ( ){{

String s , * sPtr ;String s , * sPtr ;sPtr = & s ;sPtr = & s ;

..

..

..s = * sPtr ;s = * sPtr ;

}}

ExampleExample

void String :: operator= ( String & void String :: operator= ( String & other )other )

{{ if ( this == & other )if ( this == & other )

return ;return ;----------------

}}

ExampleExample

String & String :: operator = ( String & other )String & String :: operator = ( String & other ){{ if ( this == & other )if ( this == & other ) return * this ;return * this ; delete buf ;delete buf ; buf = new char [ other.length + 1 ] ;buf = new char [ other.length + 1 ] ; strcpy ( buf , other.buf ) ;strcpy ( buf , other.buf ) ; return * this ;return * this ;}}

ExampleExample

s3 = s2 = s3 = s2 = s1 ;s1 ;

cout << a << b << cout << a << b << c ;c ;

ExampleExample

Date d1 , d2 ;Date d1 , d2 ;d2 = d1 ++ ;d2 = d1 ++ ;d2 = d1 + 1 ;d2 = d1 + 1 ;

ExampleExample

Date date1 , date2 ;Date date1 , date2 ;date2 = date1 + 1 ;date2 = date1 + 1 ;date2 = date1 ++ ;date2 = date1 ++ ;

main ( )main ( ){{ int i ;int i ; float x ;float x ; x = i ;x = i ; i = x ;i = x ;

}}

ExampleExample

main ( )main ( ){{

Date d ;Date d ; int i ;int i ; d = i ;d = i ;

}}

ExampleExample

??

main ( )main ( ){{int i ;int i ;float x ;float x ;x = ( float ) i ;x = ( float ) i ;

}}

ExampleExample

Conversion Conversion FunctionFunction

Date ( )Date ( )

{{

// body of function// body of function

}}

double x = 1 / 3 ; Output: double x = 1 / 3 ; Output: 0.333330.33333

x * 3 ; x * 3 ; Output: Output: 0.999990.99999

Common Business Common Business Oriented LanguageOriented Language