UMBC CMSC 202 Computer Science II
Lab9: Inheritance
Objective
In this lab you will practice working with the C++ class derivation mechanism.
Premise
Despite the
recent announcement by Border's Bookstore that they will end
their relationship with Amazon.com 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
- OrderItem.h:
class definition for the OrderItem base class.
You won't modify this file.
- OrderItem.cpp:
implementation for the OrderItem member functions and
the friend function ItemCost(). You will implement ItemCost().
- Book.h:
class definition of the derived class Book.
You won't modify this file.
- Book.cpp:
implementation of the Book member functions.
You will add the implementations.
- Pizza.h:
skeleton of Pizza class definition. You will
write the definition.
- Pizza.cpp:
skeleton of Pizza class implementation. You will
write most of this one.
- lab9main1.cpp:
contains first main program to test the Book class.
- lab9main1.txt:
output from lab9main1.cpp should look like this.
- lab9main2.cpp:
contains second main program to test the Book and Pizza classes.
- lab9main2.txt:
output from lab9main2.cpp should look like this.
Step 2: Implement the Book class
- Look over OrderItem.h. Note that the data members are
"protected", not "private". What does this mean?
- Note that ItemCost() is a friend function.
Implement the ItemCost() function in OrderItem.cpp.
Don't forget the tax. Item cost should round down.
- 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.
- 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.
- Compile your program with lab9main1.cpp:
g++ -ansi -Wall OrderItem.cpp Book.cpp lab9main1.cpp
- Check the output against lab9main1.txt.
Step 3: Write the Pizza class
- Edit the header file for Pizza. You will need to have
the following members in Pizza:
- A default constructor: By default you get a cheese
pizza. It costs $9 and has SKU number 8888.
- AddTopping(): Takes a string parameter (e.g.,
"pepperoni") and adds the topping to the pizza.
Each topping costs $2.
- PrintItem(): Prints out the pizza toppings
as well as the inherited members. See sample output in
lab9main2.txt.
- a vector of strings to hold all the toppings added.
- Compile your program separately to debug:
g++ -c -ansi -Wall Pizza.cpp
- Compile your program with lab9main2.cpp:
g++ -ansi -Wall OrderItem.cpp Book.cpp Pizza.cpp lab9main2.cpp
- 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.