Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In project 2 , you defined the following two classes: / / ShoppingCart.h template class ShoppingCart:public Bag { private: double totalPrice; public: ShoppingCart ( )
In project you defined the following two classes:
ShoppingCart.h
template
class ShoppingCart:public Bag
private:
double totalPrice;
public:
ShoppingCart;
double getTotalPriceconst;
bool removeconst ItemType& anEntry;
bool addconst ItemType& newEntry;
;
Item.h
class Item
private:
std::string name; name of the item
int quantity; quantity of the item
float price; price of the item
public:
Item;
Itemstd::string name, float price, int qtty;
void setNamestd::string name;
void setQuatityint qtty;
void setPricefloat price;
std::string getName const;
int getQuantity const;
float getPrice const;
friend std::istream &operatorstd::istream &ins, Item ⌖
;
std::ostream &operatorstd::ostream &out, const Item ⌖
bool operator const Item& ob const Item& ob;
In Project the add member function allows duplicated items. For example, suppose the shopping cart currently contains the following items
Name UnitPrice Quantity
Tshirt $
Sweater $
The total charge is $
If we add a new item as follows:
Tshirt $
the shopping cart becomes like this:
Name UnitPrice Quantity
Tshirt $
Sweater $
Tshirt $
The total charge is $
Two Tshirt items are considered duplicated items although their quantities are different In Project we ignore the quantity when defining operator
Let's redefine the add member function to avoid duplicated items by only changing the quantity if the new item already exists. In above example, after adding the new item Tshirt $ the shopping cart should look like this:
Name UnitPrice Quantity
Tshirt $
Sweater $
The total charge is $
To implement this new add function, first check if this newEntry is in the cart, and then perform the following two logic steps according to the checked results:
if this newEntry is not a duplicated item, simply add it to the cart. Then update the totalPrice;
otherwise, find the duplicated item from the cart and add its quantity to the new newEntry's quantity. Then remove the duplicated item from the cart; add the modified newEntry to the cart; and update the totalPrice.
Answer the following two questions about redefining the add function.
Question points
For step which of the following implementations is correct?
Question options:
a
ifcontainsnewEntry
bool flag addnewEntry;
ifflag
totalPricenewEntry.getQuantitynewEntrygetPrice;
b
IfcontainsnewEntry
bool flag addnewEntry;
ifflag
totalPrice newEntry.getPrice;
c
ifcontainsnewEntry
bool flag Bag::addnewEntry;
ifflag
totalPrice totalPrice;
d
IfcontainsnewEntry
bool flag Bag::addnewEntry;
ifflag
totalPricenewEntry.getQuantitynewEntrygetPrice;
Question points
For step which of the following implementations is correct?
Question options:
a
vector items toVector;
int numItems items.size;
int qcurrent;
int qnew newEntry.getQuantity;
forint i; i numItems; i
ifitemsinewEntry
qcurrentitemsigetQuantity;
itemsisetQuantity qcurrentqnew;
remove itemsi;
break;
bool flag addnewEntry;
ifflag
totalPriceqnew newEntry.getPrice;
b
int numItems getCurrentSize;
int qcurrent;
int qnew newEntry.getQuantity;
forint i; i numItems; i
ifitemsinewEntry
qcurrentitemsigetQuantity;
newEntry.setQuantityqnewqcurrent;
removeitemsi;
break;
bool flag Bag::addnewEntry;
if flag
totalPriceqnew newEntry.getPrice;
c
vector items toVector;
int numItems items.size;
int qcurrent;
int qnew newEntry.quantity;
forint i; i numItems; i
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