Programming in C++

Exercise Sheet 6
  1. Copy the file pets.cc containing the pet classes from the lectures (with additions) into a new directory. Add a small main function and compile the program.

  2. Move the definition of the speak method from class Dog outside of the class, and get the result to compile.

  3. Try declaring a variable of type Pet. As described in the lectures, this is not allowed: see if you can make sense of the error message the compiler gives.

  4. Try redefining the speak method in class Dalmatian, and verify that your new version works.

  5. If you copy a Dalmatian object into a Dog object, what version of speak does the resulting object have?

  6. Declare a vector of pointers to Pets and add some pointers to it (using either & or new). Loop over the vector calling speak on each pet.

  7. Repeat the previous question, this time using a list instead of a vector, and using an iterator to traverse the list.