1. [15 points] Write a complete header file for the String class based on the example used in Chapter 8 of the textbook. Your class declaration is to include all data members, but only the following function declarations. Do not write any function definitions yet!

  1. A default constructor that takes no arguments.
  2. A constructor that takes a pointer to a C string as an argument.
  3. The assignment operator (=).
  4. A string concatenation operator (+=) that has a String object as its right operand.
  5. Another string concatenation operator (+=) that has a C string as its right operand.
  6. A stream insertion operator. (Not a stream extraction operator!)

2. [5 points] What must you do immediately after you invoke the new operator in a C++ program, and why?

3. [10 points] Write the C++ code to do the following:

  1. Allocate enough memory to hold a new C string, assuming the variable some_string is a pointer to an existing C string that will later be copied into the new string. The newly-allocated memory is to be accessible using the variable another_string.
  2. Copy the string pointed to by some_string into the memory allocated for another_string.
  3. Free the memory that was allocated in answer 3a.

4. [5 points] Explain how the C++ operator that frees memory for an array (as in 3c) can determine how much memory is to be freed.

5. [10 points] Write the function definition for answer 1d (the String concatenation operator with a String object as right operand).

6. [10 points] Assume the class Complex has been defined as in the exercises for Chapter 8. Write a complete main() function that prompts the user to enter a Complex number (indicate the proper format for the number in the prompt), input the Complex number, multiply it by the complex number (3.4 + 4.5i), and display the result.

Your main() function must include all variable declarations, and must use overloaded operators for input, multiplication, and output.

7. [15 points] Assume Base is a base class, and Derived is derived from Base. Given the following code:

Base baseObj, *basePtr;

Derived derivedObj, *derivedPtr;

  1. [4 points] Insert the proper cast so that both the following statements will compile without any warnings:
    basePtr = &derivedObj;
    derivedPtr = &baseObj;
  2. [7 points] Explain your answer to part 7a. Include a brief definition of base and derived classes in this answer.
  3. [4 points] What is the effect of the cast in part 7a on the binary representation of the address that is assigned to the pointer variable?