CS201- Introduction to Programming- Lecture 30

26
Introduction to Introduction to Programming Programming Lecture 30 Lecture 30

TAGS:

description

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

Transcript of CS201- Introduction to Programming- Lecture 30

Page 1: CS201- Introduction to Programming- Lecture 30

Introduction to Introduction to ProgrammingProgramming

Lecture 30Lecture 30

Page 2: CS201- Introduction to Programming- Lecture 30

In Today’s In Today’s Lecture Lecture

– ReferencesReferences– Differences Differences

Between References Between References

and Pointersand Pointers

Page 3: CS201- Introduction to Programming- Lecture 30

ReferenceReference

Page 4: CS201- Introduction to Programming- Lecture 30

&&

Page 5: CS201- Introduction to Programming- Lecture 30

int & i ; int & i ; ‘i’ is a reference to an integer‘i’ is a reference to an integerint * i ;int * i ; ‘i’ is a pointer to an integer‘i’ is a pointer to an integer

Page 6: CS201- Introduction to Programming- Lecture 30

int i ;int i ;int & j = i ;int & j = i ;

Page 7: CS201- Introduction to Programming- Lecture 30

Example Example 11main ( )main ( )

{{

int i ;int i ;

int & j = i ;int & j = i ;

i = 123 ;i = 123 ; Output Output

cout << i << endl ;cout << i << endl ; 123123

cout << j << endl ;cout << j << endl ; 123123

i++ ;i++ ;

cout << i << endl ;cout << i << endl ; 124124

cout << j ;cout << j ; 124124

}}

Page 8: CS201- Introduction to Programming- Lecture 30

Example 2Example 2void swap ( int x , int void swap ( int x , int y )y )

{{int temp = x ; int temp = x ; x = y ; x = y ; y = temp ;y = temp ;

}}

Page 9: CS201- Introduction to Programming- Lecture 30

Example 2Example 2main ( )main ( ){{

int x , y ;int x , y ;x = 10 ;x = 10 ;

y = 20 ;y = 20 ;swap ( x , y ) ;swap ( x , y ) ;cout << x << y ;cout << x << y ;

}}

Page 10: CS201- Introduction to Programming- Lecture 30

Example 2Example 2main ( )main ( ){{

int x , y ;int x , y ;x = 10 ; y = 20 ;x = 10 ; y = 20 ;swap ( & x , & y ) ;swap ( & x , & y ) ;cout << x << y ;cout << x << y ;

}}

Page 11: CS201- Introduction to Programming- Lecture 30

swap ( int * i , int * swap ( int * i , int * j ) ;j ) ;

Page 12: CS201- Introduction to Programming- Lecture 30

void swap ( int * i , int * j )void swap ( int * i , int * j )

{{

int * temp = i ; int * temp = i ;

* i = * j ; * i = * j ;

* j = * temp ;* j = * temp ;

}}

Example 2Example 2

Page 13: CS201- Introduction to Programming- Lecture 30

void swap ( int & i , int & j )void swap ( int & i , int & j )

{{

int temp = i ; int temp = i ;

i = j ; i = j ;

j = temp ;j = temp ;

}}

Example 3Example 3

Page 14: CS201- Introduction to Programming- Lecture 30

Example 3Example 3main ( )main ( ){{int x , y ;int x , y ;x = 10 ;x = 10 ;

y = 20 ;y = 20 ;swap ( x , y ) ;swap ( x , y ) ;cout << x << y ;cout << x << y ;

}}

Page 15: CS201- Introduction to Programming- Lecture 30

constconst

Page 16: CS201- Introduction to Programming- Lecture 30

Example 4Example 4struct bstruct b

{{

int serno ;int serno ;

char text [ 1000 ] ; // A lot of charschar text [ 1000 ] ; // A lot of chars

} b = { 123, "This is a } b = { 123, "This is a BIGBIG structure" } ; structure" } ;

void valfunc ( b ) ; // Call by valuevoid valfunc ( b ) ; // Call by value

void ptrfunc ( * b ) ; // Call by pointervoid ptrfunc ( * b ) ; // Call by pointer

void reffunc ( & b ) ; // Call by referencevoid reffunc ( & b ) ; // Call by reference

Page 17: CS201- Introduction to Programming- Lecture 30

void main ( )void main ( ){{ valfunc ( b ) ; valfunc ( b ) ; // Passing the variable itself// Passing the variable itself

ptrfunc ( & b ) ; ptrfunc ( & b ) ; // Passing the address of the variable// Passing the address of the variable

reffunc ( b ) ; reffunc ( b ) ; // Passing a reference to the variable// Passing a reference to the variable

}}

Example 4Example 4

Page 18: CS201- Introduction to Programming- Lecture 30

Example 4Example 4struct bstruct b{{ int serno ; int serno ; char text [ 1000 ] ; // A lot of charschar text [ 1000 ] ; // A lot of chars} b = { 123 , "This is a BIG structure" } ;} b = { 123 , "This is a BIG structure" } ;

void valfunc ( b ) ; // Call by valuevoid valfunc ( b ) ; // Call by value

void ptrfunc ( const * b ) ; // Call by pointervoid ptrfunc ( const * b ) ; // Call by pointer

void reffunc ( const & b ) ; // Call by referencevoid reffunc ( const & b ) ; // Call by reference

Page 19: CS201- Introduction to Programming- Lecture 30

Difference Between Difference Between References and References and

PointersPointers

Page 20: CS201- Introduction to Programming- Lecture 30

main ( ) main ( )

{{

int i ;int i ;

int & refi = i ;int & refi = i ;

cout << “The address of the cout << “The address of the reference is reference is

” << & refi ;” << & refi ;

cout << & i ;cout << & i ;

}}

Page 21: CS201- Introduction to Programming- Lecture 30

Dangling Dangling ReferencReferenc

ee

Page 22: CS201- Introduction to Programming- Lecture 30

References as Return References as Return ValuesValuesint mynum = 0 ; // Global variableint mynum = 0 ; // Global variable

int & num ( )int & num ( ){{ return mynum ;return mynum ;}}void main ( )void main ( ){{ int i ;int i ;

mynum = 100 ;mynum = 100 ;i = num ( ) ;i = num ( ) ;

num ( ) = 200 ;num ( ) = 200 ;}}

Page 23: CS201- Introduction to Programming- Lecture 30

int & i ;int & i ;

double & f ;double & f ;

char & c ;char & c ;

Page 24: CS201- Introduction to Programming- Lecture 30

a = b = c ;a = b = c ;

Page 25: CS201- Introduction to Programming- Lecture 30

a = b ;a = b ;

Page 26: CS201- Introduction to Programming- Lecture 30

cout << “The values of the cout << “The values of the

integer is ”<< i << endl ; integer is ”<< i << endl ;