Question: HOMEWORK C++ For each problem, write out the problem statement and then give your answer. If it is possible to write programs to solve the

HOMEWORK C++

For each problem, write out the problem statement and then give your answer. If it is possible to write programs to solve the problems, please do so.

Exercise R7.2. A pointer variable can contain a pointer to a valid object, a pointer to a deleted object, NULL, or a random value. Write code that creates and sets four pointer variables a, b, c, and d to show each of these possibilities.

Exercise R7.3. What happens when you dereference each of the four pointers that

you created in Exercise R7.2? Write a test program if you are not sure.

Exercise R7.5. What does the following code print?

Employee harry = Employee("Hacker, Harry", 35000);

Employee boss = harry;

Employee* pharry = new Employee("Hacker, Harry", 35000);

Employee* pboss = pharry;

boss.set_salary(45000);

(*pboss).set_salary(45000);

cout << harry.get_salary() << " ";

cout << boss.get_salary() << " ";

cout << pharry->get_salary() << " ";

cout << pboss->get_salary() << " ";

Exercise R7.6. Pointers are addresses and have a numerical value. You can print out

the value of a pointer as cout << (unsigned long)(p). Write a program to compare p,

p + 1, q, and q + 1, where p is an int* and q is a double*. Explain the results.

Exercise R7.8. Which of the following assignments are legal in C++?

void f(int p[])

{

int* q;

const int* r;

int s[10];

p = q;

p = r;

p = s;

q = p;

q = r;

q = s;

r = p;

r = q;

r = s;

s = p;

s = q;

s = r;

}

Exercise R7.9. Given the definitions

double values[] = { 2, 3, 5, 7, 11, 13 };

double* p = values + 3;

explain the meanings of the following expressions:

a. values[1]

b. values + 1

c. *(values + 1)

d. p[1]

e. p + 1

f. p values

Exercise R7.12. What is the difference between the following two variable

definitions?

a. char a[] = "Hello";

b. char* b = "Hello";

Exercise R7.13. What is the difference between the following three variable

definitions?

a. char* p = NULL;

b. char* q = "";

c. char r[] = { '\0' };

Exercise R7.14. Consider this program segment:

char a[] = "Mary had a little lamb";

char* p = a;

int count = 0;

while (*p != '\0')

{

count++;

while (*p != ' ' && *p != '\0') p++;

while (*p == ' ') p++;

}

What is the value of count at the end of the outer while loop?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!