0% found this document useful (0 votes)
25 views5 pages

Chapter3 Exercises&Solutions

Uploaded by

m25p8rghmp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

Chapter3 Exercises&Solutions

Uploaded by

m25p8rghmp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

EXERCISES, CHAPTER 3

1. Mark the following statements as true or false.

a. In C++, pointer is a reserved word.


b. In C++, pointer variables are declared using the reserved word pointer.
c. The statement delete p; deallocates the variable pointer p.
d. The statement delete p; deallocates the dynamic variable to which p points.
e. Given the declaration

intlist[10];
int *p;

the statement

p = list;

is valid in C++.

f. Given the declaration

int *p;

the statement

p = new int[50];

dynamically allocates an array of 50 components of type int, and p contains the base address of the

array.

g. The address of operator returns the address and value of its operand.

h. If p is a pointer variable, the statement p = p * 2; is valid in C++.

2.What is the output of the following C++ code?

int x;
int y;
int *p = &x;
int *q = &y;
*p = 35;
*q = 98;
*p = *q;
cout << x << " " << y << endl;
cout << *p << " " << *q << endl;

3. Given the declaration

int num = 6;

int *p = &num;

which of the following statement(s) increment the value of num?

a. p++;

b. (*p)++;

c. num++

d. (*num)++;
4. What is the output of the following code?

int *p;

int *q;

p = new int;

*p = 43;

q = p;

*q = 52;

p = new int;

*p = 78;

q = new int;

*q = *p;

cout << *p << " " << *q << endl;

5. What is the output of the following code?

int x;

int *p;

int *q;

p = new int[10] ; q = p;

*p = 4;

for(int j = 0; j < 10; j++)


{
x = *p;
p++;
*p = x + j;
}

for (int k = 0; k < 10; k++)


{
cout << *q << " ";
q++;
}
cout << endl;

6. Explain the difference between a shallow copy and a deep copy of data.
7. What is the output of the following code?

int *p;

int *q;

p = new int [5];

p[0] = 5;

for(int i = 1; i < 5; i++){

p[i] = p[i - 1] + 2 * i;

cout << "Array p: ";

for(int i = 0; i < 5; i++){

cout << p[i] << " ";


}

cout << endl;

q = new int[5];

for(int i = 0; i < 5; i++){

q[i] = p[4 - i];

cout << "Array q: ";

for(int i = 0; i < 5; i++){

cout << q[i] << " ";

cout << endl;

8. What is the purpose of the copy constructor?

9. Name three situations when a copy constructor executes.

10. Suppose that you have the following definition of a class.

class dummyClass{
public:
void print();
...

...

private:
int listLength;
int *list;
double salary;
string name;
}

a. Write the function prototype to overload the assignment operator for the class
dummyClass.
b. Write the definition of the function to overload the assignment operator for the class
dummyClass.

c. Write the function prototype to include the copy constructor for the class dummyClass.

d. Write the definition of the copy constructor for the class dummyClass.
Solutions :

1. a. false; b. false; c. false; d. true; e. true; f. true; g. false; h. false

2. 98 98

98 98

3. b and c

4. 78 78

5. 4 4 5 7 10 14 19 25 32 40

6. In a shallow copy of data, two or more pointers points to the same memory space. In a
deep copy of data, each pointer has its own copy of the data.

7. Array p: 5 7 11 17 25
Array q: 25 17 11 7 5

8. The copy constructor makes a copy of the actual parameter data.

9. The copy constructor executes when a variable is passed by value, when a variable is
declared and initialized using another variable, and when the return value of a function is an
object.

10. a. const dummyClass& operator=(const dummyClass&);

b. const dummyClass & dummyClass::operator=(const dummyClass& rightObject)


{
if(this != &rightObject)//avoids self-assignment
{
listLength = rightObject.listLength;
salary = rightObject.salary;
name = rightObject.name;

if(list !- NULL){

delete list;
}

list = new int[listLength];

for (int j = 0; j < listLength; j++){

list[j] = rightObject.list[j];
}
}

return *this;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy