CS201- Introduction to Programming- Lecture 30
-
Upload
bilal-ahmed -
Category
Education
-
view
25 -
download
0
description
Transcript of CS201- Introduction to Programming- Lecture 30
![Page 1: CS201- Introduction to Programming- Lecture 30](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/1.jpg)
Introduction to Introduction to ProgrammingProgramming
Lecture 30Lecture 30
![Page 2: CS201- Introduction to Programming- Lecture 30](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/2.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/3.jpg)
ReferenceReference
![Page 4: CS201- Introduction to Programming- Lecture 30](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/4.jpg)
&&
![Page 5: CS201- Introduction to Programming- Lecture 30](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/5.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/6.jpg)
int i ;int i ;int & j = i ;int & j = i ;
![Page 7: CS201- Introduction to Programming- Lecture 30](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/7.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/8.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/9.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/10.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/11.jpg)
swap ( int * i , int * swap ( int * i , int * j ) ;j ) ;
![Page 12: CS201- Introduction to Programming- Lecture 30](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/12.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/13.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/14.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/15.jpg)
constconst
![Page 16: CS201- Introduction to Programming- Lecture 30](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/16.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/17.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/18.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/19.jpg)
Difference Between Difference Between References and References and
PointersPointers
![Page 20: CS201- Introduction to Programming- Lecture 30](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/20.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/21.jpg)
Dangling Dangling ReferencReferenc
ee
![Page 22: CS201- Introduction to Programming- Lecture 30](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/22.jpg)
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](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/23.jpg)
int & i ;int & i ;
double & f ;double & f ;
char & c ;char & c ;
![Page 24: CS201- Introduction to Programming- Lecture 30](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/24.jpg)
a = b = c ;a = b = c ;
![Page 25: CS201- Introduction to Programming- Lecture 30](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/25.jpg)
a = b ;a = b ;
![Page 26: CS201- Introduction to Programming- Lecture 30](https://reader033.fdocuments.net/reader033/viewer/2022042606/5492e755b4795900548b45f2/html5/thumbnails/26.jpg)
cout << “The values of the cout << “The values of the
integer is ”<< i << endl ; integer is ”<< i << endl ;