{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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 is the syntax for the reference variable. Now let’s see why and how we use it and what is its advantage? Let’s look at an example that will explain the reference variable. Suppose we have the following three different functions.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern