Skip to content
VUBuddy

CS201 Quiz Lectures 30-39 2022

Introduction to Programming PDF 7.6 MBUpdated 23 Oct 2026
Quiz CS201 Quiz Lectures 30-39 2022 PDF 7.6 MB
vubuddy.com
Type
Quiz
Course
CS201 Introduction to Programming
Size
PDF 7.6 MB
Updated
23 Oct 2026
Free download 0 of 3 used today
Download free · 7.6 MB

You get 3 free files a day on the website. Add the Chrome extension for 30 a day.

Unlimited downloads in the Android app Get it free on Google Play

Preview text

Text from pages 2, 3, 4 of 213. Download the file for all 213 pages.

Lecture No. 30

Reading Material

Deitel & Deitel - C++ How to Program Chapter. 3

  1. 17

Summary 1) Reference data type 2) Example 1 3) 4) Dangling References 5) Example 2

Reference data type

Out today’s topic is about references. This is a very important topic from the C++ prospective. Today we will see what is a reference, how can we use them. C++ defines a thing by which we can create an alias or synonym of any data type. That synonym is called reference. How do we declare a reference? We declare it by using & operator. Now it is little bit confusing. We have used & as address-of operator and here we are using it for referencing. We will write as

int &i;

It means that i is a reference to an integer. Keep that statement very clear in your mind. It is easier to read from right to left. A reference is a synonym. If we want to give two names to same thing then we use reference. Reference has to be initialized when it is declared. Suppose if we have an integer as i and we want to give it second name. We will reference it with j as:

int &j = i;

We declared an integer reference and initialized it. Now j is another name for i. Does it mean that it creates a new variable? No, its not creating a new variable. Its just a new name for the variable which already exists. So if we try to manipulate i and j individually, we will came to know that we have been manipulating the same number. Lets take a look at a very simple example. In the main function we take an int variable i and then we write int &j = i; Now we assign some value (say 123) to i. Now display the value of i using cout. It will show its value as 123. Display the value of j using cout. We will not use & operator to display the value of j. We will only use it at the time of declaration and later we don’t need it. The & is not reference operator rather it acts as reference declarator. The value of j will be same as of i i.e. 123.

int i; int &j = i; i = 123; cout << “\n The value of i = “ << i; cout << “\n The value of j = “ << j;

Now what will happen if we increment i as i++; and print the values of i and j. You will note that the value of i and j both have been incremented. We have only incremented i but j is automatically incremented. The reason is that both are referring to the same location in the memory. j is just another name for i.

What is the benefit of reference and where can we use it? References are synonyms and they are not restricted to int’s, we can have reference of any data type. We can also take reference of a class. We wrote a function to show the use of pointers. That function is used to interchange two numbers. If we have two integers x and y. We want that x should contain the value of y and y should get the value of x. One way of doing this is in the main program i.e.

int x = 10; int y = 20; int tmp; tmp = y; y = x; x = tmp;

The values of both x and y have been interchanged. We can also swap two numbers using a function. Suppose we have a swap function as swap(int x, int y) and we write the above code in it, what will happen? Nothing will be changed in the calling program. The reason is call by value. So when the main function calls the function swap(x, y). The values of x and y will be passed to the swap function. The swap function will get the copies of these variables. The changes made by the swap function have no effect on the original variables. Swap function does interchange the values but that change was local to the swap function. It did not effect anything in the main program. The values of x and y in the main program remains same.

We said that to execute actual swap function we have to call the function by reference. How we did that. We did not send x and y rather we sent the addresses of x and y. We used address operator to get the addresses. In the main function we call swap function as swap(&x, &y); In this case we passed the addresses of two integers. The prototype of the swap function is swap(int*, int*) which means that swap function is expecting pointers of two integers. Then we swap the values of i and j using the * notations. It works and in the main program, the values are interchanged. This was a clumsy way. We can use reference in this case with lot of ease. Let us see how we can do that. Lets rewrite the swap function using references. The prototype will be as:

swap (int &i, int &j);

Swap is a function that is expecting i as a reference to an integer and the second argument is j which is also a reference to an integer. The calling function has to pass references. What will we write in the body of the function? Here comes the elegance of the references. In the body we will treat i and j as they are ordinary integers. We will take a temporary integer and interchange the values of i and j.

swap (int &i, int &j)

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

In the main program, you will see that the values of two integers have been interchange. What is the way to call this function? In the main program, we will call this function as swap(x, y). Its an ordinary call but the function is expecting addresses which is automatically done. The memory locations of the integers are passed and the function is interchanging the original numbers. This is one beautiful example in which we avoided all the cumbersome of pointer notation. What is the downfall of this? As nothing comes for free. In this case when you are reading the main function you will see swap (x, y) which seems a call by value. This is a rule of C/C++ that when we pass two variables to some function they are passed by values. You will have to look for the definition of swap function to realize that it is not call by value but is call by reference. Second thing is if we have another swap function, which is receiving two integers. Can we define two functions as swap(int x, int y) and swap(int &x, int &y)? One function is receiving two integers and other is receiving two references of integers. Can we do that? Types are different so we can overload. Unfortunately not, in the main function the way to call both functions is same i.e. swap(x, y). How does the compiler know that which functions is being called? There is no way for the compiler to find out. Therefore there is an ambiguity and that is not allowed. The only thing to realize is the side effect. Side effects are critical to take care of whenever you are doing call by reference. Here in this example we do want that two numbers should be interchanged. There may be some situation where we want to send the references and don’t want that original data should be affected. These situations arise when we want to pass a large data structure to a function. To understand this we have to understand how the function call executes. We have discussed it before, now lets recap it. In real world, suppose I am reading a book. While reading I notice a word which I think I have to look up. I stop reading, markup the page and then look that word in dictionary or encyclopedia. While reading the definition of that word I look another special word which I need to lookup. I put a marker here and go for looking the definition of that new word. Eventually I understand all the words I need to look up. Now I want to go to the point at which I left the study. I close the dictionary or encyclopedia and goes back to the original book which I was studying. Now I understand all the words in my study and continue the study from the point where I

All CS201 files →
CS201 Introduction to Programming All CS201 handouts, past papers, quizzes and MCQs in one place. Open course page

Get more out of VUBuddy, for free

Android app Free on Google Play
  • Unlimited downloads
  • The explanation behind every MCQ answer
  • Study offline on slow data
Get the Android app
Chrome extension Free on the Chrome Web Store
  • 30 downloads a day instead of 3
  • Sign in and buy Pro Semester here
Add to Chrome

One Pro Semester membership works in both the extension and the app.