UMBC CMSC 202 Computer Science II

Lab9: Inheritance


In this lab you will practice working with the C++ class derivation mechanism.


Despite the recent announcement by Border's Bookstore that they will end their relationship with and create their own online shopping experience, they have in fact joined forces with Papa John's Pizza. The hope is to deliver books to their customers even faster than the FedEx overnight service. Hungry for some pizza and historical fiction? You'll have one stop shopping.

The problem is that they will need a system to order pizza and books at the same time. Fortunately, the computer section at Border's is full of books that talk about object-oriented programming and inheritance.

In a related announcement, Papa John's Pizza revealed that their drivers will no longer carry any coins. Henceforth, all prices will be rounded down to the nearest dollar.

Step 1: Get the files

Step 2: Implement the Book class

  1. Look over OrderItem.h. Note that the data members are "protected", not "private". What does this mean?

  2. Note that ItemCost() is a friend function. Implement the ItemCost() function in OrderItem.cpp. Don't forget the tax. Item cost should round down.

  3. Look over Book.h. Note that the Book class is a public derivation of OrderItem. Four data items have been added: m_title, m_author, is_rated and rating. These have the usual meanings.

  4. Add the implementations of 3 functions in Book.cpp: the alternate constructor, PrintItem() and SetRating().

    Note: that the ItemOrder alternate constructor initializes m_quantity to 1. This is what we want.

  5. Compile your program with lab9main1.cpp: g++ -ansi -Wall OrderItem.cpp Book.cpp lab9main1.cpp

  6. Check the output against lab9main1.txt.

Step 3: Write the Pizza class

  1. Edit the header file for Pizza. You will need to have the following members in Pizza:

  2. Compile your program separately to debug: g++ -c -ansi -Wall Pizza.cpp

  3. Compile your program with lab9main2.cpp: g++ -ansi -Wall OrderItem.cpp Book.cpp Pizza.cpp lab9main2.cpp

  4. Check the output against lab9main2.txt. Why does PrintItem() sometimes print out just the SKU, price and quantity and other times print out the book title and pizza toppings?

Step 4: Checking Out

Have your TA look over the output of your program.