Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. How is * used to create pointers? Give an example to justify your answer. How

ID: 3586703 • Letter: 1

Question

1.

How is * used to create pointers? Give an example to justify your answer.

How is * used to dereference pointers? Give an example to justify your answer.

2. Consider the following statement:

int* p, q;

This statement could lead to what type of misrepresentation?

3. Suppose that you have the declaration int *numPtr;. What is the difference between the expressions: *numPtr and &numPtr?

4. Suppose that numPtr is a pointer of type int and gpaPtr is a pointer of type double. Further suppose that numPtr = 1050 and gpaPtr = 2000. Also suppose that the size of the memory allocated for an int value is 4 bytes and the size of the memory allocated for a double value is 8 bytes. What are the values of numPtr and gpaPtr after the statements numPtr = numPtr + 2; and gpaPtr = gpaPtr + 3; execute?

5.

What does the operator new do?

What does the operator delete do?

6. Explain why you cannot use a range-based for loop on dynamic arrays.

7. Explain the difference between a shallow copy and a deep copy of data.

8.

What is the purpose of the copy constructor?

Name two situations in which a copy constructor executes.

9. Name three things that you should do for classes with pointer member variables.

10. What is the difference between compile-time binding and run-time binding?

11. Is it legal to have an abstract class with all member functions pure virtual?

Explanation / Answer

1) How is * used to create pointers? Give an example to justify your answer.

When we declare a variable preceded by * then it is a information to the compiler to allocate memory to a varible which can store address of another variable of same type.

int *pt;

Where pt is a pointer type variable whose size is 2 byte and can store the address of another integer type variable only.

1) How is * used to dereference pointers? Give an example to justify your answer.

int *pt;

int p = 10;

pt = &p;

cout<<"PT = "<<*pt<<" P = "<<p;

In the above examples int *pt; is a pointer declaration because it is used with the data type.

Assume p address is 100.

pt = &p; In this statement pt stores the address of p assume it is 100.

cout<<"PT = "<<*pt;

*pt This statement means value at pt, where pt is 100 because pt stores the address of p i.e., address 100.

Now value at 100 address location is equivalent to variable p contents i.e., 10.

2. Consider the following statement:

int* p, q;

This statement could lead to what type of misrepresentation?

int* p, q; Only p is the pointer variable, and variable q is not pointer type it is a normal variable to store value.

Here, q is an integer variable which can store integer type data where, as p is a integer type pointer which can store the address of pointer type variable.

To avoid confusion, attach the pointer symbol, * to the variable name q.

int *p, *q;

3. Suppose that you have the declaration int *numPtr;. What is the difference between the expressions: *numPtr and &numPtr?

*numPtr this statement refers to value at numPtr.

&numPtr this statement refers to address of numPtr.

Example:

int p = 10

int *numPtr;

numPtr = &p;

Assume address of p is 100 and address of numPtr is 500.

numPtr = &p; This statement will store the address of p i.e., 100 in numPtr.

cout<<*numPtr; à will display 10 i.,e., value stored at address location 100

cout<<&numPtr; à will display 500 i.e., address of numPtr

4. Suppose that numPtr is a pointer of type int and gpaPtr is a pointer of type double. Further suppose that numPtr = 1050 and gpaPtr = 2000. Also suppose that the size of the memory allocated for an int value is 4 bytes and the size of the memory allocated for a double value is 8 bytes. What are the values of numPtr and gpaPtr after the statements numPtr = numPtr + 2; and gpaPtr = gpaPtr + 3; execute?

numPtr = numPtr + 2;

Answer: 1058

Explanation: 1050 + 4 + 4

gpaPtr = gpaPtr + 3;

Answer: 2024

Explanation: 2000 + 8 + 8 + 8

5) What does the operator new do?

new operator is used to dynamically allocate memory.

Example:

int *ptr;

is a pointer declaration;

ptr = new int[20];

allocates 20 contiguous memory locations, each of type integer, and stores the address of the

first memory location into ptr.

5) What does the operator delete do?

delete operator is used to de-allocate memory allocated by new operator.

Example:

delete ptr;

6. Explain why you cannot use a range-based for loop on dynamic arrays.

It works with constant arrays but not with pointer based dynamic array.

It gives errors and warnings about failure of substitution.

If the type of the expression passed to the right of : is an array type, then the loop iterates from variable ptr to ptr + size (ptr pointing to the first element of the array, size being the element count of the array).

7. Explain the difference between a shallow copy and a deep copy of data.

Shallow copy: If the object we are copying has a pointer to some data, we don't make a copy of that data and both objects share that same data. So any change in the data in one object also changes it in the other, because they're both using that exact same data. It is creating a new object and then copying the non static fields of the current object to the new object. If the field is a value type, a bit by bit copy of the field is performed. If the field is a reference type, the reference is copied but the referred object is not, therefore the original object and its clone refer to the same object. A shallow copy of an object is a new object whose instance variables are identical to the old object.

Deep copy: It is creating a new object and then copying the non - static fields of the current object to the new object. If a field is a value type, a bit by bit copy of the field is performed. If a field is a reference type, a new copy of the referred object is performed. A deep copy of an object is a new object with entirely new instance variables, it does not share objects with the old. While performing Deep Copy the classes to be cloned must be flagged as [Serializable].

During object declaration, the initialization of one object using the value of another object would lead to a shallow copying of the data if the default member-wise copying of data is allowed

8. What is the purpose of the copy constructor?

Copy constructor is basically used to create a duplicate copy of the existing object.

Name two situations in which a copy constructor executes.

9. Name three things that you should do for classes with pointer member variables.

1) Destructor must be defined in the class.

2) Assignment operator must be overloaded in the class.

3) Class must contain copy constructor.

10. What is the difference between compile-time binding and run-time binding?

Compile-time binding

Run-time binding

Also known as static binding or early binding

Also known as dynamic binding or late binding

An object is assigned to a variable declared to be of a specific object type.

The functions, methods, variables and properties are detected and checked only at the run-time.

Example: function overloading and operator overloading

Example: virtual function

11. Is it legal to have an abstract class with all member functions pure virtual?

Yes.

Because it is a row class based on which new classes can be generated and they will implement the functions as per their requirement.

Compile-time binding

Run-time binding

Also known as static binding or early binding

Also known as dynamic binding or late binding

An object is assigned to a variable declared to be of a specific object type.

The functions, methods, variables and properties are detected and checked only at the run-time.

Example: function overloading and operator overloading

Example: virtual function