Inheritance and Polymorphism

The goal of this lab is to give you a bit more experience with inheritance and polymorphism. Complete the steps below:
  1. Create a new Project and add the following pieces of code to the project: pet.h - dog.h - test.cpp. Note: You will need to complete steps 2 and 3 before you can compile this code.
  2. In order to use dynamic_cast you will need to enable the use of runtime type information.
    1. From the menu, select Project and then Settings.
    2. Select the C/C++ Tab.
    3. From the Category pull-down menu select C++ Language.
    4. Check Enable Run-Time Type Information (RTTI).
    5. Click OK.
  3. Fix the errors in test.cpp. Refer to your exam to determine which lines will not compile and comment out those lines.
  4. Build and run test.cpp.
  5. Remove the virtual keyword from dog.h and pet.h and build your project.
  6. Replace the virtual keyword.
  7. Add the following lines between the last cout and the return in main:
    Pet* p2_ptr = new Pet("Jake", "Spotted");
    Dog* d2_ptr = dynamic_cast<Dog*>(p2_ptr);
    cout << d2_ptr->getBreed() << endl;
  8. Build and run test.cpp.
Email me (srollins@mtholyoke.edu) answers to the following questions by Friday, 3/10/06 - 5pm. Note: I expect your answers to demonstrate a thorough understanding of each concept. You need to think carefully about why you get the output or errors that you get.
  1. Explain what happened at step 5.
  2. Explain what you think the three lines of code you added in step 7 should accomplish.
  3. Explain what happened at step 8.
  4. Give three examples of classes (other than Dog), that might inherit from Pet. For each, outline at least one additional data member for the class. This is a design question; it does not require any implementation.
  5. Do you think it would ever make sense to have a class that would inherit from Dog? If so, give an example and describe what the class would look like. If not, argue why not.

Sami Rollins