6.8. Exercises: Inheritance and Polymorphism

  1. Figure 6.5.  Film classes

    Film classes

    The classes in Figure 6.5 are intended to help organize the film collection in the college library.

    1. Implement the Film classes. Make sure that the constructors have sufficient parameters to initialize all data members. We have suggested enum types FilmTypes (Action, Comedy, SciFi, ...) and MPAARatings (G, PG, PG-13, ...) for use in the Entertainment class.

    2. Implement the FilmList class as a container of Film pointers. Make sure that the addFilm() function does not permit the same Film to be added more than once.

      Is it possible to use base class functions such as contains() or indexOf() here?

    3. Write client code to test these classes. Put a mixture of Entertainment and Educational films into the FilmList and exercise all the member functions.

    Having a container of pointers raises the question of how to destroy it. If we simply “do the right thing” and define a FilmList destructor that visits each of its pointers and deletes it, we must then worry about client code that contains a function with a FilmList value parameter. Recall that each value parameter causes a copy of the corresponding argument to be made. That copy is destroyed when the function returns. How should we deal with copying and destroying FilmList objects?

    Our FilmList class allows us to exploit polymorphism but exposes the Film pointers to client code. So far, this design violates our earlier warning regarding the use of pointers. In general, pointers must be hidden from client code. We will discuss an approach to this problem in Section 17.3.

  2. Revisit the birthday reminder exercise (Section 3.2.2) and use an ArgumentList object to process the command-line arguments.

[ fromfile: inheritance-intro.xml id: ex-inheritance ]