|
|
|
|
|
|
|
3. a. recPtr = &oneRec;
b. (*recPtr).weight = 120.5;
recPtr->weight = 120.5;
4. a. double* dblPtr = new double;
*dblPtr = 98.32586728;
b. int* list = new int[50];
for (i = 0; i < 50; i++)
list[i] = 0;
5. delete dblPtr;
delete [] list; |
|
|
|
|
|
|
|
|
6. float& gamma = delta; 7. gamma = 12.9; (Remember that once a reference variable is initialized, each appearance of the variable is implicitly dereferenced.) 8. Shallow copying copies one pointer to another without copying any pointed-to data. 9. (a.) Passing a class object as a parameter using pass-by-value. (b.) Initializing a class object in its declaration. (c.) Returning a class object as a function value. 10. The class needs one or more constructors (to create the dynamic data), a destructor (to clean up the free store), a deep copy operation, and a copy-constructor (for deep copying during initializations). 11. Pointers, when used with dynamic data, improve memory efficiency because we create only as many dynamic variables as are needed. With respect to time efficiency, it is faster to move pointers than to move large data structures, as in the case of sorting large structs. |
|
|
|
|
|
|
|
|
Exam Preparation Exercises |
|
|
|
|
|
|
|
|
1. How does a variable of type float* differ from a variable of type float? |
|
|
|
|
|
|
|
|
2. Show what is output by the following C++ code. If an unknown value gets printed, write a U. |
|
|
|
 |
|
|
|
|
int main()
{
int m;
int n;
int* p = &m;
int* q;
*p = 27;
cout < *p < < *q < endl;
q = &n;
n = 54;
cout < *p < < *q < endl;
p = &n;
*p = 6;
cout < *p < < n < endl;
return 0;
} |
|
|
|
|
|
|
|
|
3. Given the declarations |
|
|
|
 |
|
|
|
|
struct PersonType
{
char lastName[31];
char firstnitial;
};
|
|
|
|
|
|