look at references and function calling using ReferenceDemo.cpp 2. Download and
ID: 3756575 • Letter: L
Question
look at references and function calling using ReferenceDemo.cpp
2. Download and try to compile the file. Why doesn't it compile? I'm not just looking for which line(s) have the problem, but rather what is wrong with those line(s).
3. What is happening when onePlus() is called? In other words, what exactly does the function receive as an argument? Why isn't the value of the variable in main() altered?
4. Conversely, consider the call to increment(). What is the argument here and why is the value of the variable in main() changed?
5. Finally, consider the code related to value(). First of all, make sure you understand what Pair is and what the declaration of pairs means.
6. Next, consider a call to value(). What does the function receive as an argument? What is the meaning of const in this context?
7. What does value() return? How can it be legal and what does it mean to apply the ++operator to its return value?
=============ReferenceDemo.cpp==============
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void refIncrement(int& a)
{
a++;
}
void onePlus(int a)
{
a++;
}
struct Pair
{
string name;
int val;
};
vector<Pair> pairs;
int& value(const string& s)
{
for (int i=0; i<pairs.size(); i++) {
if (s == pairs[i].name) return pairs[i].val;
}
Pair p = {s, 0};
pairs.push_back(p);
return pairs[pairs.size()-1].val;
}
int main()
{
int i = 1;
int& r = i;
int x = r;
cout << "Basic reference manipulations:" << endl;
cout << "Values of i, r, and x (original): " << i << " " << r << " " << x << endl;
r = 2;
cout << "Values of i, r, and x (after updating r): " << i << " " << r << " " << x << endl;
int& xr;
xr = x;
cout << "Addresses of i, r, and x: " << &i << " " << &r << " " << &x << endl;
cout << " Pass by value:" << endl;
onePlus(i);
cout << "Values of i, r, and x (after calling onePlus(i)): " << i << " " << r << " " << x << endl;
onePlus(r);
cout << "Values of i, r, and x (after calling onePlus(r)): " << i << " " << r << " " << x << endl;
onePlus(x);
cout << "Values of i, r, and x (after calling onePlus(x)): " << i << " " << r << " " << x << endl;
cout << " Pass by reference:" << endl;
refIncrement(i);
cout << "Values of i, r, and x (after calling refIncrement(i)): " << i << " " << r << " " << x << endl;
refIncrement(r);
cout << "Values of i, r, and x (after calling refIncrement(r)): " << i << " " << r << " " << x << endl;
refIncrement(x);
cout << "Values of i, r, and x (after calling refIncrement(x)): " << i << " " << r << " " << x << endl;
cout << " Reference as lvalue:" << endl;
value("aa")++;
value("bb")++;
value("bb")++;
value("aa")++;
value("aa")++;
value("bb")++;
value("aa")++;
value("aa")++;
for (int i=0; i<pairs.size(); i++) {
cout << pairs[i].name << ": " << pairs[i].val << endl;
}
return 0;
}
Explanation / Answer
2 Answer)
int& xr; <-- this line of code means that you're declaring a referencing variable but not initializing it. That's why you may be getting the error as " xr is declared as reference but not initialized ". So what does it mean???
You've to intialize the reference variable while you declaring it itself.
So change the 9th line of code to "" int& xr=x "".
3 Answer)
onePlus() function is receiving only the value of the arguemnt passed to it. Arguement is just value of the variable. The received value will be saved in the parameter variable(or local variable) of the function. Arguemnt is different from parameter.
Arguement:- variable passed from calling function. Here main() function is the calling function.
Parameter:- varaible listed in the function definition.
So when you modify local variables of the onePlus(), changes won't reflect in the main().
This type of calling a function is also called as "" Call By Value "".
Call By Value:The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument. By default cpp uses "Call By value" to pass arguemnts.
4 Answer)
increment() function is receiving address of the arguement.
Here, arguement is the address of the variable. So in the increment() function we're directly accessing the variables in the main() with the address of the varaible.That's why contents of the vaiable in the main() are changed.
This type of calling a function is called as "" Call By Reference "".
Call By Reference: - The call by reference method of passing arguments to a function copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. It means the changes made to the parameter affect the passed argument.
To pass a value by reference, argument pointers are passed to the functions just like any other value.
5)
In the given code a "Pair" is a structure containing two variable, one string variable and one int variable.
Pairs is a vector.
Vector: Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container. Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators.
vector<Pair> pairs; <-- means declaring a vector with each element as type "Pair".
6)
Value() is receiving an address of a string variable. Checking whether the string pointed by the reference is present in "pairs" or not. If the string is present then value() will return integer value associated with it.
If it's not present then value() function appends the string and an integer '0' to the "pairs" at the end.
The const keyword can be used in pointer declarations. A pointer to a variable declared as const can be assigned only to a pointer that is also declared as const. You can use pointers to constant data as function parameters to prevent the function from modifying a parameter passed through a pointer.
So we don't want contents of the string to modified by the value() function so we are receiving the address into the const string variable in the function definiton.
6 Answer)
value() returns the address of the integer varaible.
The integer variable is which is associated with the string sent to it .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.