Question
C++ Polymorphism The purpose is to write classes and subclasses representing an ice cream item orders that can be sold in an ice cream shop.
C++ Polymorphism
The purpose is to write classes and subclasses representing an ice cream item orders that can be sold in an ice cream shop. There are many ways ice cream can be sold, and we will implement a specific way according to the description below:
---An ice cream order can consist of many ice cream items.
---An ice cream item can either be small, medium, or large. These sizes are represented as string types and used when constructing the ice cream items (see IceCreamItem.h).
---There are two types of ice cream items that may be purchased and added to an ice cream order:
CustomItem inherits from IceCreamItem
In addition to the functions inherited from IceCreamItem.h, you will need to implement the following specifically for a CustomItem:
CustomItem(std::string size);
----Constructs a custom ice cream order with a size. The only sizes an IceCreamItem may have are: small, medium, and large.
-----Each size will have a base price for the order (without the addition of toppings). A small size is $3.00, a medium size is $5.00, and a large size is $6.50.
virtual ~CustomItem();
----A virtual deconstructor in case you have to deallocate any memory on the heap.
void addTopping(std::string topping);
----A method that allows you to add a topping to the custom ice cream order.
----Toppings are not predefined and can be identified with strings. You may assume any topping added to a CustomItem has a weight of 1 oz. Each oz. increases the price of the CustomItem by $0.40 cents.
----You may add the same topping (i.e. represented as the same string) as many times you want, with each topping addition being 1 oz. Your implementation will need to keep track of the number of ounces for each topping and display the correct number of ounces when constructing a string in composeItem.
PreMadeItem inherits from IceCreamItem
-In addition to the functions inherited from IceCreamItem.h, you will need to implement the following specifically for a PreMadeItem:
PreMadeItem(std::string name, std::string size);
----Constructs a pre-made ice cream order with a size. The only sizes any IceCreamItem may have are small, medium, and large.
----Each PreMadeItem also consists of a name (represented as a string) used when composing the details for this item.
----Each size will have a base price for the order (without the addition of toppings). A small size is $4.00, a medium size is $6.00, and a large size is $7.50.
----Since a PreMadeItem already has the necessary toppings built in, there is no need to add toppings such as in the CustomItem.
virtual ~PreMadeItem();
----A virtual deconstructor in case you have to deallocate any memory on the heap.
Implement the files to pass your tests.
The provided starter code will test your implementation by constructing orders of various ice cream items. This will make sure your price calculations, toppings, names, etc. are correct. You must implement the following files according to the specifications above:
IceCreamItem.cpp, CustomItem.h, CustomItem.cpp, PreMadeItem.h, PreMadeItem.cpp
Since you will need to provide a composeItem() implementation for each subclass, there is a certain format that a CustomItem and PreMadeItem must compose the details of the item into a string that composeItem() returns. Some notes:
----Your price must be displayed in a traditional dollar amount with a precision of two decimal places.
----For a CustomItem, your toppings must be displayed in lexicographical order. How you do this is up to you, but I recommend using asince the keys are inherently sorted. The keys can be stored as strings representing the topping names, and the values are the number of ounces of that topping (i.e. number of times addTopping() was called for that specific topping).
----An example of a CustomItem that is a large size, contains one addition of an oreo topping, 2 additions of peanut topping, and a price of $7.70.
Custom Size: large Toppings: oreos: 1 oz peanuts: 2 oz Price: $7.70
An example of a PreMadeItem that is a medium size, the item name is Banana Slamma, and has a price of $6.00.
Pre-made Size: medium Pre-made Item: Banana Slamma Price: $6.00
// IceCreamItem.h #ifndef ICECREAMITEM_H #define ICECREAMITEM_H #includeclass IceCreamItem { public: // Constructor for an IceCreamItem. All ice cream items // will either be "small", "medium", or "large". IceCreamItem(std::string size); // A virtual method that composes a string with the details // of an IceCreamItem. See the lab writeup and test files // for examples of the format for composeItem. virtual std::string composeItem() = 0; // Returns the dollar amount of an ice cream item. virtual double getPrice() = 0; protected: double price; std::string size; }; #endif
// IceCreamOrder.cpp #include#include #include #include #include "IceCreamOrder.h" #include "IceCreamItem.h" using namespace std; string IceCreamOrder::printBill() const { double totalPrice = 0; string out = ""; for (size_t i = 0; i < items.size(); i++) { totalPrice += items[i]->getPrice(); out += items[i]->composeItem(); out += "----- "; } stringstream stream; stream << fixed << setprecision(2) << totalPrice; out += "Total: $" + stream.str() + " "; return out; } void IceCreamOrder::addItem(IceCreamItem* item) { items.push_back(item); }
// IceCreamOrder.h #ifndef ICECREAMORDER_H #define ICECREAMORDER_H #include#include #include "IceCreamItem.h" class IceCreamOrder { public: std::string printBill() const; void addItem(IceCreamItem* item); private: std::vector items; }; #endif
TEST CODE---------------------
// testIceCreamOrder1.cpp #include#include #include "IceCreamOrder.h" #include "CustomItem.h" #include "tddFuncs.h" using namespace std; int main() { cout << "Running tests from: " << __FILE__ << endl; IceCreamOrder order1; CustomItem* item1 = new CustomItem("small"); order1.addItem(item1); string expected = "Custom Size: small \ Toppings: \ Price: $3.00 \ ----- \ Total: $3.00 "; ASSERT_EQUALS(expected,order1.printBill()); delete item1; IceCreamOrder order2; CustomItem* item2 = new CustomItem("medium"); order2.addItem(item2); expected = "Custom Size: medium \ Toppings: \ Price: $5.00 \ ----- \ Total: $5.00 "; ASSERT_EQUALS(expected,order2.printBill()); delete item2; IceCreamOrder order3; CustomItem* item3 = new CustomItem("large"); order3.addItem(item3); expected = "Custom Size: large \ Toppings: \ Price: $6.50 \ ----- \ Total: $6.50 "; ASSERT_EQUALS(expected,order3.printBill()); delete item3; }
// testIceCreamOrder2.cpp #include#include #include "IceCreamOrder.h" #include "CustomItem.h" #include "tddFuncs.h" using namespace std; int main() { cout << "Running tests from: " << __FILE__ << endl; IceCreamOrder order1; CustomItem* item1 = new CustomItem("small"); item1->addTopping("mochi"); order1.addItem(item1); string expected = "Custom Size: small \ Toppings: \ mochi: 1 oz \ Price: $3.40 \ ----- \ Total: $3.40 "; ASSERT_EQUALS(expected,order1.printBill()); delete item1; IceCreamOrder order2; CustomItem* item2 = new CustomItem("medium"); item2->addTopping("gummi bears"); item2->addTopping("gummi bears"); order2.addItem(item2); expected = "Custom Size: medium \ Toppings: \ gummi bears: 2 oz \ Price: $5.80 \ ----- \ Total: $5.80 "; ASSERT_EQUALS(expected,order2.printBill()); delete item2; IceCreamOrder order3; CustomItem* item3 = new CustomItem("large"); item3->addTopping("oreos"); item3->addTopping("peanuts"); item3->addTopping("peanuts"); order3.addItem(item3); expected = "Custom Size: large \ Toppings: \ oreos: 1 oz \ peanuts: 2 oz \ Price: $7.70 \ ----- \ Total: $7.70 "; ASSERT_EQUALS(expected,order3.printBill()); delete item3; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started