Solutions to Selected Midterm Questions

Answer #5 Answer #8
legal
illegal - can't dereference an int *y
legal
illegal - can't dereference an int *y
legal
legal
legal
illegal - *x is an int and array is an address
illegal - *x is an int and &array[0] is an address

Answer #9

class Person
{
  public:
    Person(void);
    ~Person(void);
    void Set(const char* first, const char* last, int age);
    void Display(void) const;

  private:
    char* first;
    char* last;
    int age;
};
Answer #10
Person::Person(void)
{
  delete [] first;
  delete [] last;
}
Answer #11
Person(const Person& otherPerson);
Answer #12
Person person[50];
Answer to Extra Credit
Person::Person(const Person& otherPerson)
{
  age = otherPerson.age;

  first = new char[strlen(otherPerson.first) + 1];
  assert(first);
  strcpy(first, otherPerson.first);

  last = new char[strlen(otherPerson.last) + 1];
  assert(last);
  strcpy(last, otherPerson.last);
}

Back to Outline