CS301-Lec17 handout - CS301 Data Structures Lecture No. 17...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 17 ___________________________________________________________________ Data Structures Lecture No. 17 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 4 Summary Reference Variables Sample Program After demonstrating the use of const and reference variables in the interface class for a binary search tree in the previous lectures, we will now see what these reference variables are and how does internal process go on during the use of reference variables. Reference Variables Before proceeding ahead, there is need to know why the reference variables are used; and what benefits a programmer can get while employing them. We will discuss these all things in detail with the help of examples. The symbol &, used for reference variable has a few different purposes with respect to its occurrence in the code. In C++ programming, we have seen that when the ampersand sign i.e. & appears in front of a variable name, it is the address operator. It returns the address of the variable in front of which it is written. Thus for example, if x is a variable name, then &x ; will return the address of the variable x . In general we can say that variablename ; will return the address of the variable. We know that an address can be stored in a pointer. To further understand this concept, let’s suppose that there are following lines in our code. int x ; int* ptr = &x; The first line declares a variable x of type int while the second one declares a pointer to int and assigns it the address of x . This address of variable x is returned by the & sign written in front of the variable x . Thus a pointer variable is initialized by assigning it the address of a variable. This address of the variable is gotten by using the & sign with the variable name. The other place where & sign can appear is the signature of the function where it appears after the type of the parameter. Consider the insert and remove methods from BinarySearchTree class that were declared as the following. Page 1 of 14
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS301 – Data Structures Lecture No. 17 ___________________________________________________________________ void insert( const EType& x ); void remove( const EType& x ); Notice that the & sign is after the type. Here we define the class and functions as templates. Whenever, we use these methods, the type EType will be replaced with a proper data type. Suppose that we have designed the BinarySearchTree class to hold the integers only. This means that there are no templates and the class can be defined only for integers. So the insert and remove methods of this class will be as follows. void insert( const int& x ); void remove( const int& x ); Here, in the function signature, & sign after the type of the parameter indicates that the parameter is a reference variable. We can use this & sign with any data type i.e. built- in or user defined, of an argument to show that this is a reference variable. This
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/21/2010 for the course CS CS301 taught by Professor Dr.naveed malik during the Spring '10 term at Virtual University of Pakistan.

Page1 / 14

CS301-Lec17 handout - CS301 Data Structures Lecture No. 17...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online