Study Guide 4 Answers

Perspective

In the workplace, most of you will work on large projects as a member of a team. Each team member will have an assigned piece of the overall project. In the end, all of the pieces must fit together like the pieces of a jigsaw puzzle. Everyone will work from a common set of requirements, ensuring the pieces fit together. If one team member goes "cowboy" and makes changes contrary to the requirements, the pieces won't fit together. When this happens, the other team members will meet the cowboy in the parking lot and "throw him or her a party."

The questions on the worksheets are like tiny little requirements. You are not at liberty to make changes like changing the name of a variable or a structure. Please read the questions carefully. Also, remember that C++ is case sensitive: bar is not the same as Bar.

Terminology

It's very rare for anyone to learn everything about a concept with just one learning experience. You must be willing to return to previously presented concepts and review them as needed. For example, the following answers are more compact and meaningful if you know what is meant by:

Examples


Answers

  1. c
  2. a
  3. c
  4. a
  5. b
  6. b, c, and d
  7. b, c, and d
  8. d
  9. a
  10. c
  11. a
  12. c
  13. b
  14. a
  15. b
  16. c
  17. Write an expression to calculate the address of the variable testvar.
    &testvar
    Compare this question to Q3 and Q14.
  18. Define a variable named char_pointer whose data type is a pointer to a char.
    char* char_pointer
    Compare this question to Q4 and Q15.
  19. Define a variable named double_pointer whose type is a  pointer to a double.
    double* double_pointer
    Compare this question to Q4 and Q15.
  20. If a pointer named testptr points to a variable testvar, write an expression that represents the contents of testvar but that does not use its name.
    *testptr
    Compare this question to Q5 and Q16.
  21. C
  22. B
  23. A
  24. B
  25. C
  26. Generally b or c. The sizeof operator returns the size of a variable or data type measured in bytes (section 2.8). In Visual Studio, the default size of a pointer is 4 bytes (even on a 64-bit computer), but VS is configurable so that the size of a pointer is 8 bytes (which is more efficient on a 64-bit computer). Other compilers, specifically those running on other operating systems, will produce 4-byte pointers on 32-bit computers and 8-byte pointers on 64-bit computers.

    The size of a pointer is fixed and is not affected by initializing or not initializing the contents of the pointer.

  27. Assume that bar is the name of a class. Write a statement that defines a variable named bar_ptr that can point to a bar object. What it means to define a variable was introduced in section 1.6 of the text.
    bar* bar_ptr;
  28. Assume that bar is the name of a class. Write C++ code that creates an instance of bar on the heap. You may choose any variable name for the object.
    bar* bar_ptr;
    bar_ptr = new bar;
    bar* bar_ptr;
    bar_ptr = new bar();
    bar* bar_ptr = new bar;
    
    bar* bar_ptr = new bar();

    Until very recently, parentheses were not only not needed when calling a default constructor, but using them was an error. The most recent version of C++ allows both versions listed here as correct. Defining and initializing a variable are two distinct operations that can be implemented with two statements or combined into a single statement.