CS201- Introduction to Programming- Lecture 33

32
Introduction to Introduction to Programming Programming Lecture 33 Lecture 33

description

Virtual University Course CS201- Introduction to Programming Lecture No 33 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Transcript of CS201- Introduction to Programming- Lecture 33

Page 1: CS201- Introduction to Programming- Lecture 33

Introduction to Introduction to ProgrammingProgrammingLecture 33Lecture 33

Page 2: CS201- Introduction to Programming- Lecture 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

Page 3: CS201- Introduction to Programming- Lecture 33

AssignmenAssignment Operatort Operator

Page 4: CS201- Introduction to Programming- Lecture 33

a = b ;a = b ;

Page 5: CS201- Introduction to Programming- Lecture 33

Member Member Wise Wise

AssignmentAssignment

Page 6: CS201- Introduction to Programming- Lecture 33

Member Wise Member Wise CopyCopy

Page 7: CS201- Introduction to Programming- Lecture 33

ExampleExampleclass Stringclass String

{{

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

public:public:

String ( ) ;String ( ) ;

~ String ( ) ;~ String ( ) ;

......

} ;} ;

Page 8: CS201- Introduction to Programming- Lecture 33

main ( )main ( )

{{

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

String s2 ;String s2 ;

s2 = s1 ;s2 = s1 ;

……

……

}}

ExampleExample

Page 9: CS201- Introduction to Programming- Lecture 33

s2 = s2 = s1 ;s1 ;

Page 10: CS201- Introduction to Programming- Lecture 33

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 ) ;

}}

Page 11: CS201- Introduction to Programming- Lecture 33

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 ;

Page 12: CS201- Introduction to Programming- Lecture 33

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

Page 13: CS201- Introduction to Programming- Lecture 33

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

Page 14: CS201- Introduction to Programming- Lecture 33

this this pointerpointer

Page 15: CS201- Introduction to Programming- Lecture 33

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

this pointerthis pointer

Page 16: CS201- Introduction to Programming- Lecture 33

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

Page 17: CS201- Introduction to Programming- Lecture 33

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

s = s ;s = s ;

Page 18: CS201- Introduction to Programming- Lecture 33

Self Self AssignmentAssignment

Page 19: CS201- Introduction to Programming- Lecture 33

main ( )main ( ){{

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

..

..

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

}}

ExampleExample

Page 20: CS201- Introduction to Programming- Lecture 33

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

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

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

}}

ExampleExample

Page 21: CS201- Introduction to Programming- Lecture 33

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

Page 22: CS201- Introduction to Programming- Lecture 33

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

Page 23: CS201- Introduction to Programming- Lecture 33

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

Page 24: CS201- Introduction to Programming- Lecture 33

ExampleExample

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

Page 25: CS201- Introduction to Programming- Lecture 33

ExampleExample

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

Page 26: CS201- Introduction to Programming- Lecture 33

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

}}

ExampleExample

Page 27: CS201- Introduction to Programming- Lecture 33

main ( )main ( ){{

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

}}

ExampleExample

??

Page 28: CS201- Introduction to Programming- Lecture 33

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

}}

ExampleExample

Page 29: CS201- Introduction to Programming- Lecture 33

Conversion Conversion FunctionFunction

Page 30: CS201- Introduction to Programming- Lecture 33

Date ( )Date ( )

{{

// body of function// body of function

}}

Page 31: CS201- Introduction to Programming- Lecture 33

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

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

Page 32: CS201- Introduction to Programming- Lecture 33

Common Business Common Business Oriented LanguageOriented Language