Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

/* * ReferenceDemo.cpp * * Modified by Yang Peng on 12/31/17 */ #include #include #include using namespace std; void refIncrement(int& a) { a++; } void

/*

* ReferenceDemo.cpp

*

* Modified by Yang Peng on 12/31/17

*/

#include

#include

#include

using namespace std;

void refIncrement(int& a)

{

a++;

}

void onePlus(int a)

{

a++;

}

struct Pair

{

string name;

int val;

};

vector pairs;

int& value(const string& s)

{

for (int i=0; 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

cout

r = 2;

cout

int& xr = x;

cout

cout

onePlus(i);

cout

onePlus(r);

cout

onePlus(x);

cout

cout

refIncrement(i);

cout

refIncrement(r);

cout

refIncrement(x);

cout

cout

value("aa")++;

value("bb")++;

value("bb")++;

value("aa")++;

value("aa")++;

value("bb")++;

value("aa")++;

value("aa")++;

for (int i=0; i

cout

}

return 0;

}

Output:

image text in transcribedimage text in transcribed
2. Download and try to compile the le. Why doesn't it compile? I'm not just looking for which line(s) have the problem, but rather what is wrong with those linels). 3. What is happening when onePlusO 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 reflncrementO. 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? Basic reference manipulations: Values of i, r, and x (original) : 1 1 1 Values of i, r, and x (after updating r) : 2 2 1 Addresses of i, r, and x: 0x7ffee5980lac 0x7ffee5980lac 0x7ffee59801a8 Pass by value: Values of i, r, and x (after calling onePlus (i) ) : 2 2 1 Values of i, r, and x (after calling onePlus (r) ) : 2 2 1 Values of i, r, and x (after calling onePlus (x) ) : 2 2 1 Pass by reference: Values of i, r, and x (after calling refIncrement (i) ) : 3 3 1 Values of i, r, and x (after calling refIncrement (r) ) : 4 4 1 Values of i, r, and x (after calling refIncrement (x) ) : 4 4 2 Reference as lvalue: aa: 5 bb: 3

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions