Part E - Polymorphism

Virtual Functions

Workshop 9


In this workshop, you design a class hierarchy that implements an abstract base class.


Learning Outcomes

Upon successful completion of this workshop, you will have demonstrated the abilities to

  • define an abstract base class
  • derive a concrete class that implements an abstract base class
  • derive a class that upgrades a concrete class

Interface - All Groups

Define an interface (an abstract base class without data members) named iFood that exposes the following member functions in the iFood class hierarchy:

  • int portions() const - a query that returns the number of portions
  • void consume() - a modifier that consumes a single portion
  • int remaining() const - a query that returns the number of slices remaining
  • void display() const - a query that displays current information about the food item

Place your interface definition in a header file named iFood.h


Pizzas - Group X

Implement your iFood interface through two separate classes:

  • Pizza
  • DeluxePizza

Pizza Class

Design a class named Pizza that holds information about a pizza and tracks its consumption.  Upon instantiation, a Pizza object receives a double specifying the pizza's diameter and an int specifying the total number of slices.  Initially, a Pizza object is unconsumed.

Implement the pure virtual functions listed in the interface's definition.  Your display() function should insert information in the following format - diameter, total number of slices, "slices", number of slices remaining, "remaining"

 14" 8 slices 5 remaining

Place your class definition in a header file named Pizza.h and your function definitions in an implementation file named Pizza.cpp

Client Module

The main program that uses your implementation is listed on the left, while the results are shown on the right:

 // Workshop 9 - Pizza
 // w9.cpp

 #include <iostream>
 #include "Pizza.h"

 int main() {
     iFood* food;

     food = new Pizza(14.0, 8);

     for (int i = 0; i < 3; i++)
         food->consume();
     food->display();
     std::cout << std::endl;

     delete p;
 }













 14" 8 slices 5 remaining




DeluxePizza Class

Derive a class named DeluxePizza from your Pizza class.  Your derived class holds information about the pizza's toppings.  Upon instantiation, a DeluxePizza object receives a double specifying the pizza's diameter, an int specifying the total number of slices and the address of a C-style null-terminated string describing the toppings.  Your class can hold up to 40 characters in this toppings' string.  Initially, a DeluxePizza object is unconsumed.

Your display() function should insert information in the following format - diameter, total number of slices, "slices", number of slices remaining, "remaining", newline, "- ", toppings

 12" 6 slices 4 remaining
 - mushrooms, peppers

Add your class definition to the header file named Pizza.h and your function definition to the implementation file named Pizza.cpp

Client Module

The main program that uses your implementation is listed on the left, while the results are shown on the right:

 // Workshop 9 - Pizza
 // w9.cpp

 #include <iostream>
 #include "Pizza.h"

 int main() {
     iFood* food;

     food = new Pizza(14.0, 8);

     for (int i = 0; i < 3; i++)
         food->consume();
     food->display();
     std::cout << std::endl;

     delete food;

     food = new DeluxePizza(12.0, 6,
      "mushrooms, peppers");

     for (int i = 0; i < 2; i++)
         food->consume();
     food->display();
     std::cout << std::endl;

     delete food;
 }













 14" 8 slices 5 remaining









 12" 6 slices 4 remaining
 - mushrooms, peppers



Typescript

Create a typescript of your complete solution using the following commands: 

 + At the prompt, type: script w9.txt
 + At the prompt, type: whoami
 + At the prompt, type: cat iFood.h Pizza.h Pizza.cpp 
 + At the prompt, type: g++ -o w9 w9.cpp Pizza.cpp
 + At the prompt, type: w9
 + At the prompt type: exit

These commands will produce a file named w9.txt.  Download this file to your local computer. 


Chocolates - Group Y

Unit Tests

Design a global function named unitTests() that conducts a set of unit tests on implementations of the iFood interface.  Your function reports the success or failure of the tests on standard output.  Your function's prototype is

 void unitTest(iFood*);

Place the prototype in a header file named iFoodTests.h and your function definition in an implementation file named iFoodTests.cpp

Implementations

Implement your iFood interface through two separate classes:

  • Chocolate
  • DeluxeChocolate

Chocolate Class

Design a class named Chocolate that holds information about a chocolate bar and tracks its consumption.  Your class implements the iFood interface.  Upon instantiation, a Chocolate object receives an int specifying the number of squares.  Initially, a Chocolate object is unconsumed.

Your display() function should insert information in the following format - number of squares, "squares", number of squares remaining, "remaining"

 8 squares 4 remaining

Place your class definition in a header file named Chocolate.h and your function definitions to the implementation file named Chocolate.cpp

DeluxeChocolate Class

Derive a class named DeluxeChocolate from your Chocolate class.  Upon instantiation, a DeluxeChocolate object receives an int specifying the number of squares and the address of a C-style null-terminated string describing the filling.  Your class does not limit the number of characters in this string.  Initially, a DeluxeChocolate object is unconsumed.

Your display() function should insert information in the following format - number of squares, "squares", number of squares remaining, "remaining", newline, dash, filling

 8 squares 4 remaining
 - marzipan

In order for your class to copy objects properly, you need to include a copy constructor, an assignment operator and a destructor. 

Add your class definition to the header file named Chocolate.h and your function definitions to the implementation file named Chocolate.cpp

Test Main

The main program that tests your implementation is listed on the left, while the results are shown on the right:

 // Workshop 9 - Chocolate Tests
 // w9.tests.cpp

 #include "Chocolate.h"
 #include "iFoodTests.h"

 int main() {
     iFood* food;

     food = new Chocolate(10);
     iFoodTests(food);
     delete food;

     food = new DeluxeChocolate(
      "marzipan", 12);
     iFoodTests(food);
     iFood*
     delete food;
 }










 passed all tests




 passed all tests



Client Module

The main program that uses your implementation is listed on the left, while the results are shown on the right:

 // Workshop 9 - Chocolate
 // w9.cpp

 #include <iostream>
 #include "Chocolate.h"

 int main() {
     iFood* food;

     food = new Chocolate(10);

     for (int i = 0; i < 4; i++)
         food->consume();
     food->display();
     std::cout << std::endl;

     delete food;

     food = new DeluxeChocolate(
      "marzipan", 12);

     for (int i = 0; i < 3; i++)
         food->consume();
     food->display();
     std::cout << std::endl;

     delete food;
 }













 10 squares 6 remaining 









 12 squares 9 remaining
 - marzipan



Typescript

Create a typescript of your complete solution using the following commands: 

 + At the prompt, type: script w9.txt
 + At the prompt, type: whoami
 + At the prompt, type: cat iFoodTests.h iFoodTests.cpp
 + At the prompt, type: cat iFood.h Chocolate.h Chocolate.cpp 
 + At the prompt, type: g++ -o w9tests w9.cpp Chocolate.cpp iFoodTests.cpp 
 + At the prompt, type: w9tests
 + At the prompt, type: g++ -o w9 w9.cpp Chocolate.cpp 
 + At the prompt, type: w9
 + At the prompt type: exit

These commands will produce a file named w9.txt.  Download this file to your local computer. 


SUBMISSION

Moodle

Upload your typescript file to Moodle: 

  • Login to Moodle
  • Select your course code
  • Select W9 under Assignments and Tasks
  • Upload w9.txt
  • Write a short note to your instructor
    • press "Edit"
    • enter your conclusions about this workshop in the notes textbox
    • press "Save Changes"
  • When ready to submit, press "Send for Marking"

Other Submissions

Submit your typescript file as your instructor has specified.







  Designed by Chris Szalwinski   Copying From This Site   
Logo
Creative Commons License