Question: I'm having trouble with this lab, mostly just checking to see if a cake has cream cheese frosting on it and I'm getting errors about
I'm having trouble with this lab, mostly just checking to see if a cake has cream cheese frosting on it and I'm getting errors about there being no default constructor for BakedGood and Cake. I've included the insturcution followed by my own work which I would like to not change too much, only fix the errors.
Instructions:
10.9 Main Lab 10 - Bread and Cakes
Background
This lab helps you learn inheritance and polymorphism. The classes and functionality are minimal, but they make you exercise the main features of inheritance and polymorphism. To make sure you do the critical themes, there are places where you must implement it a certain way. The TAs will review your code to make sure you implement it as requested (see deductions below).
Specification
You have started a small bakery business called Bread and Cakes. This piece of software allows customers to make an on-line order. The customer first enters the specific baked goods to order (with a quantity) and you create an object for each individual good that they have ordered. You then confirm by printing the created objects. Lastly, you print an invoice of the order with final pricing, including discounts.
Your bakery offers loaves of bread, layer-cakes, and cupcakes. You need to create an inheritance hierarchy for these. You will create 5 classes named as follows. Start with a base class called BakedGood which must be an abstract class (i.e. it contains at least one pure virtual function). There are two classes which derive from BakedGood. One is the Bread class and the other is another abstact class called Cake, from which LayerCake and CupCake are derived. Shared members should be declared in the most base class possible. Your base classes may have their data members as protected rather than private if you prefer, allowing the derived classes to directly access the data members of base classes. Your three derived classes Bread, LayerCake and CupCake should at least each have the following virtual member functions (you can name them what you want):
ToString: Returns a string containing the basic description and base price of the object. While there are only three broad classes of objects (Bread, LayerCake, CupCake), there are lots of different variations of baked goods (white bread, wheat bread, 3-layer spice cupcake with vanilla frosting, Chocolate cupcake with cream-cheese frosting and red sprinkles, etc.) You do NOT have a class for each baked good, just for the three broad classes.
DiscountedPrice: Takes as parameter the total number of a particular baked good and returns the discounted total price for that baked good.
Requirements
This lab can be done individually or as pair programming.
Part 1 - Enter the bakery order and create class objects
Prompt the user to enter sub-orders. Enter "done" to finish. Each order specifies a baked good and begins with either "Bread", "Layer-cake", or "Cupcake", followed by options and quantity (see sample I/O for details). You may assume that the user inputs correct values. You are not required to error check.
Your breads can be any variety (white, wheat, rye. etc) as the bread variety is just a string in the object.
LayerCakes and CupCakes have a flavor (e.g. chocolate, yellow, spice) and a frosting (e.g. chocolate, vanilla, cream-cheese). Cake flavor and bread variety should be different variables.
LayerCakes have a number of layers
CupCakes have a sprinkles color
Store each object as a pointer in a one polymorphic vector of pointers. Keep them in the same order as entered. Later orders are added to the end of the current list.
The object is not a sub-order but rather a single baked good. Thus, if a person orders 4 loaves of Wheat bread, as in the example below, you create four Bread objects of variety "Wheat", pushing each pointer separately into your vector.
Each object must have a base price computed in its constructor. The following magic numbers should be declared as constants in the corresponding constructors. Base pricing is as follows:
| Class | Price | Cream-Cheese Frosting | # Layers |
| Bread | $4.50 | ||
| CupCake | $1.95 | add $.20 | |
| LayerCake | $9.00 | add $1.00 per layer | $3.00 per layer beyond 1 |
Part 2 - Confirm proper class creation by printing your vector with ToString (65 points)
Loop through the object pointer vector and print each description using the virtual ToString member function for each object. The ToString function prints a description of the object followed by its non-discounted base price (see sample I/O). The base price part of the returned ToString string is the same for the classes Bread, CupCake and LayerCake, so that part must be done in the BakedGood class. The rest of the string is generated by the ToString function of the current object class. Thus, for a wheat bread description, the "Wheat Bread" part of the string is generated by the ToString virtual function in Bread which calls the ToString virtual function in BakedGood to get the cost part " ($4.50)". TAs will check your code to make sure you have properly created classes and used polymorphism as described in the specification.
Part 3 - Print an Invoice (35 points)
Print an invoice of the order listing each unique baked good with description (includes base price), quantity, and total discounted price on a row. Summed totals appear on the last row. White space checking is off, but for your information our setwidth values are 75 for the baked good column and 9 for the other columns. Each baked good's discounted price must be calculated by a virtual function implemented in the corresponding class. The magic numbers from the following table must only be found in these function implementations and declared as constants.
| Class | Discount |
| Bread | For every 3 loaves of the same variety of bread you buy, one of them is free |
| CupCake | Buy 2 or more of the same cupcake, get $.30 off each, 4 or more get $.40 off each |
| LayerCake | Buy 3 or more of the same layer-cake, get $2.00 off each |
Thus, if one ordered 2 loaves of the same variety bread the cost is $9.00. 3 loaves would still be $9.00, and 4 would be $13.50. If one ordered 4 2-layer chocolate cakes with vanilla frosting, they would cost $10.00 each. If one ordered 2 3-layer spice cakes with cream-cheese frosting there would be no discount.
Note that there is a little bit of algorithm to write for the invoice. Your vector just contains a long list of baked goods, some of them being repeats. For example "Wheat bread" occurs 4 times in the example below and "White Bread" occurs twice. Note that the White Bread orders occur at different places in the overall order, as a customer may decide to order more later in the order. When you print your invoice you should just print each unique baked good once on its own line with a count of how many times it occurs. That same count is passed to the DiscountedPrice virtual function which returns the discounted total price for that particular baked good. Note that two baked goods are the same if their ToString values are the same. You can use that to go through the vector and get a count for each type of baked good. The auto-grader assumes baked goods are ordered by when the baked good first appeared in the order. You may use a temporary vector if you wish to help implement this.
Sample Input/Output
**Bread and Cakes Bakery** Enter sub-order (enter "done" to finish) Sub-order: Bread Wheat 4 Sub-order: Cupcake Funfetti lemon pink 1 Sub-order: Bread White 1 Sub-order: Layer-cake Chocolate vanilla 2 3 Sub-order: Layer-cake Spice cream-cheese 3 2 Sub-order: Cupcake Yellow cream-cheese red 3 Sub-order: Cupcake Chocolate vanilla blue 4 Sub-order: Bread White 1 Sub-order: Cupcake Chocolate vanilla blue 1 Sub-order: done Order Confirmations: Wheat bread ($4.50) Wheat bread ($4.50) Wheat bread ($4.50) Wheat bread ($4.50) Funfetti cupcake with lemon frosting and pink sprinkles ($1.95) White bread ($4.50) 2-layer Chocolate cake with vanilla frosting ($12.00) 2-layer Chocolate cake with vanilla frosting ($12.00) 2-layer Chocolate cake with vanilla frosting ($12.00) 3-layer Spice cake with cream-cheese frosting ($18.00) 3-layer Spice cake with cream-cheese frosting ($18.00) Yellow cupcake with cream-cheese frosting and red sprinkles ($2.15) Yellow cupcake with cream-cheese frosting and red sprinkles ($2.15) Yellow cupcake with cream-cheese frosting and red sprinkles ($2.15) Chocolate cupcake with vanilla frosting and blue sprinkles ($1.95) Chocolate cupcake with vanilla frosting and blue sprinkles ($1.95) Chocolate cupcake with vanilla frosting and blue sprinkles ($1.95) Chocolate cupcake with vanilla frosting and blue sprinkles ($1.95) White bread ($4.50) Chocolate cupcake with vanilla frosting and blue sprinkles ($1.95) Invoice: Baked Good Quantity Total Wheat bread ($4.50) 4 13.50 Funfetti cupcake with lemon frosting and pink sprinkles ($1.95) 1 1.95 White bread ($4.50) 2 9.00 2-layer Chocolate cake with vanilla frosting ($12.00) 3 30.00 3-layer Spice cake with cream-cheese frosting ($18.00) 2 36.00 Yellow cupcake with cream-cheese frosting and red sprinkles ($2.15) 3 5.55 Chocolate cupcake with vanilla frosting and blue sprinkles ($1.95) 5 7.75 Totals 20 103.75 Good Bye
Deductions graded by TA's
We don't expect you to miss any points here as these just describe more carefully the specs and learning goals of the lab. Read them carefully, as you don't want to have a working program which gets 0 points!
Adherence to the style guidelines. (up to 20 points)
Use inheritance and polymorphism to solve this lab. (30 points)
Shared data members and member functions should always be declared in the most base class possible. (5 point deduction each case)
You must create the class declarations and definitions in separate files. (15 points)
The BakedGood class and the Cake class must be abstract classes. (5 points each)
Create virtual member functions to get discounted prices for appropriate classes. (5 point deduction in each case)
Create virtual ToString member functions (call them what you want) for appropriate classes. Your ToString member functions must return a string and NOT use a cout. (5 point deduction in each case)
The base price part of any string returned by a ToString function must be created by calling the ToString function of the BakedGood class. (10 points)
Use a single vector of pointers in main to store all objects. (15 points). It is all right to use some type of temporary vector for helping print the invoice if you wish.
Always create your own default constructor for each class, and create a parameterized constructor(s) as required. You may combine these into one constructor function by creating a default constructor with default parameters. For abstract base classes we recommend the same, but will not grade those. (5 points each)
Notes
Member functions can be constant which would look like: int MyFunc() const; and pure virtual which would look like: virtual int AnotherFunc() = 0;. A function that is both constant and pure virtual would look like: virtual int LastFunc() const = 0;.
BakedGood.h:
#pragma once
#include
using namespace std;
class BakedGood {
public:
virtual string ToString() const;
virtual double DiscountedPrice(int numBakedGood) = 0;
protected:
string name;
int numGoods;
double price;
string description;
};
BakedGood.cpp:
#include "BakedGood.h"
#include
#include
using namespace std;
string BakedGood::ToString() const {
const int DOLLAR_PRECISION = 2;
ostringstream stringBuilder;
stringBuilder
stringBuilder
return stringBuilder.str();
}
Bread.h
#pragma once
#include "BakedGood.h"
#include
using namespace std;
class Bread :
public BakedGood
{
public:
virtual string ToString() const;
virtual double DiscountedPrice(int numBakedGood);
Bread(string breadType);
~Bread();
protected:
string breadType;
const double BREAD_BASE_PRICE = 4.50;
};
Bread.cpp
#include "Bread.h"
#include
string Bread::ToString() const {
return description;
}
double Bread::DiscountedPrice(int numBakedGood) {
const int NUM_FOR_DISCOUNT = 3;
double discountedPrice = 0.00;
int freeLoaves = 0;
freeLoaves = numBakedGood / NUM_FOR_DISCOUNT;
discountedPrice = (numGoods - freeLoaves) * BREAD_BASE_PRICE;
return discountedPrice;
}
Bread::Bread(string breadType) {
ostringstream stringBuilder;
price = BREAD_BASE_PRICE;
stringBuilder
description = stringBuilder.str();
}
Cake.h:
#pragma once
#include "BakedGood.h"
class Cake : public BakedGood {
public:
Cake(string flavor, string frosting);
protected:
string flavor;
string frosting;
};
Cake.cpp:
#include "Cake.h"
Cake::Cake(string flavor, string frosting) {
this->flavor = flavor;
this->frosting = frosting;
}
CupCake.h:
#pragma once
#include "Cake.h"
#include
using namespace std;
class CupCake :
public Cake
{
public:
virtual string ToString() const;
virtual double DiscountedPrice(int numBakedGood);
CupCake(string flavor, string frosting, string sprinkleColor);
protected:
string sprinkleColor;
const double CUPCAKE_BASE_PRICE = 1.95;
const double CUPCAKE_CREAM_CHEESE_PRICE = 0.20;
};
CupCake.cpp:
#include "CupCake.h"
string CupCake::ToString() const {
return description;
}
double CupCake::DiscountedPrice(int numBakedGood) {
const int NUM_FOR_DISCOUNT_SMALL = 2;
const int NUM_FOR_DISCOUNT_LARGE = 4;
double discountedPrice = 0.00;
const double SMALL_DISCOUNT = 0.30;
const double LARGE_DISCOUNT = 0.40;
if (numBakedGood > NUM_FOR_DISCOUNT_LARGE) {
discountedPrice = numBakedGood * (price - LARGE_DISCOUNT);
}
else if (numBakedGood >= NUM_FOR_DISCOUNT_SMALL) {
discountedPrice = numBakedGood * (price - SMALL_DISCOUNT);
}
else if (numBakedGood
discountedPrice = numBakedGood * (price);
}
if (frosting.find("cream-cheese") != string::npos) {
discountedPrice += CUPCAKE_BASE_PRICE * numBakedGood;
}
return discountedPrice;
}
CupCake::CupCake(string flavor, string frosting, string sprinkleColor) {
flavor = flavor;
frosting = frosting;
sprinkleColor = sprinkleColor;
}
LayerCake.h:
#pragma once
#include "Cake.h"
class LayerCake :
public Cake
{
public:
virtual string ToString() const;
virtual double DiscountedPrice(int numBakedGood);
LayerCake(string flavor, string frosting, int numLayers);
protected:
int numLayers;
const double LAYER_CAKE_BASE_PRICE = 9.00;
const double LAYER_CAKE_CREAM_CHEESE_PRICE = 1.00;
const double LAYER_CAKE_LAYER_PRICE = 3.00;
};
LayerCake.cpp
#include "LayerCake.h"
string LayerCake::ToString() const {
return description;
}
double LayerCake::DiscountedPrice(int numBakedGood) {
double discountedPrice = 0.0;
const int NUM_FOR_DISCOUNT = 3;
const double DISCOUNT_AMOUNT = 2.00;
const int FIRST_LAYER = 1;
if (numBakedGood >= NUM_FOR_DISCOUNT) {
discountedPrice = numBakedGood * (price - DISCOUNT_AMOUNT);
}
else {
discountedPrice = numBakedGood * price;
}
if (frosting.find("cream-cheese") != string::npos) {
discountedPrice += (LAYER_CAKE_CREAM_CHEESE_PRICE * numLayers) * numBakedGood;
}
if (numLayers > FIRST_LAYER) {
discountedPrice += ((numLayers - FIRST_LAYER) * LAYER_CAKE_LAYER_PRICE) * numBakedGood;
}
return discountedPrice;
}
LayerCake::LayerCake(string flavor, string frosting, int numLayers) {
flavor = flavor;
frosting = frosting;
numLayers = numLayers;
}
main.cpp:
#include
#include
#include
using namespace std;
#include "BakedGood.h"
#include "Bread.h"
#include "Cake.h"
#include "CupCake.h"
#include "LayerCake.h"
int main() {
string item;
vector
vector
string breadType;
string flavor;
string frosting;
int numLayers;
string sprinkleColor;
int number;
int i = 0;
int numTimesOrdered = 0;
int totalGoods = 0;
double total = 0.0;
cout
cout
while (item != "done") {
cout
cin >> item;
cout
if (item == "Bread") {
cin >> breadType;
cin >> number;
for (i = 0; i
Bread* myBread = new Bread(breadType);
myVector.push_back(myBread);
}
}
else if (item == "Layer-cake") {
cin >> flavor;
cin >> frosting;
cin >> numLayers;
cin >> number;
for (i = 0; i
LayerCake* myLayerCake = new LayerCake(flavor, frosting, numLayers);
myVector.push_back(reinterpret_cast
}
}
else if (item == "Cupcake") {
cin >> flavor;
cin >> frosting;
cin >> sprinkleColor;
CupCake* myCupCake = new CupCake(flavor, frosting, sprinkleColor);
myVector.push_back(reinterpret_cast
}
}
cout
cout
cout
tempVector = myVector;
for (int i = 0; i
for (int j = i + 1; j
if (tempVector.at(i)->ToString() == tempVector.at(j)->ToString()) {
++numTimesOrdered;
tempVector.erase(tempVector.begin() + j);
--j;
}
}
cout ToString() DiscountedPrice(numTimesOrdered)
totalGoods = totalGoods + numTimesOrdered;
total = total + tempVector.at(i)->DiscountedPrice(numTimesOrdered);
numTimesOrdered = 1;
}
cout
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
