Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In project2, you defined the following two classe // ShoppingCart.h template class ShoppingCart:public Bag { private: double totalPrice; public: ShoppingCart(); double getTotalPrice()const; bool remove(const ItemType&

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

In project2, you defined the following two classe // ShoppingCart.h template class ShoppingCart:public Bag \{ private: double totalPrice; public: ShoppingCart(); double getTotalPrice()const; bool remove(const ItemType\& anEntry); bool add(const ItemType\& newEntry); \}; // Item.h class Item \{ private: std::string name; // name of the item int quantity; // quantity of the item private: std::string name; // name of the item int quantity; // quantity of the item float price; // price of the item public: Item(); Item(std::string name, float price, int qtty); void setName(std::string name); void setQuatity(int qtty); void setPrice(float price); std::string getName() const; int getQuantity() const; float getPrice() const; friend std::istream \&operator>>(std::istream \&ins, Item \&target); \}; std::ostream \&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 "T-shirt $19.993 ", the shopping cart should look like this: 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: 1. if this newEntry is not a duplicated item, simply add it to the cart. Then update the totalPrice; 2. 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 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: 1. If this newEntry is not a duplicated item, simply add it to the cart. Then update the totalPrice; 2. 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 new Entry to the cart; and update the totalPrice. Answer the following two questions about redefining the add function. Question 6 (5 points) For step 1), which of the following implementations is correct? a) if(!contains(newEntry) {boolflag=add(newEntry);if(flag) totalPrice+=newEntry.getQy(nntity()*newEntry.getPrice); b) if(!contains(newEntry)) bool flag = Bag::add(newEntry); if(flag) totalPrice = 2 total Price; \} c) If(!contains(newEntry)) { bool flag = add(newEntry); if(flag) totalPrice+= newEntry.getPrice(); d) If(!contains(newEntry)) \{ bool flag = Bag :: add(newEntry); if(flag) bool flag = Bag>::add( new Entry); if(flag) totalPrice += newEntry.getQuantity()**newEntry.getPrice(); \} Question 7 (5 points) For step 2), which of the following implementations is correct? a) int numltems = getCurrentSize(); int q_current; int q_new = newEntry.getQuantity(); for(int i=0;i::add( new Entry); totalPrice += q_new new Entry.getPrice(); b) vector> items = toVector(); int numltems = items.size(); int q_current; int q_new = newEntry.quantity; for(int i=0;i items = toVector(); int numltems = items.size(); int q_current; int q_new = newEntry.quantity; for(int i=0;i items = toVector(); int numltems = items.size(); int q_current; int q_new = newEntry.getQuantity(); for(int i=0; i items = toVector(); int numltems = items.size(); int q_current; int q_new = newEntry.getQuantity(); for(int i=0;i \& cart) \{ int numltem = cart.getCurrentSize(); for(int i=0; i[i] \& cart) \{ int numltem = cart.getCurrentSize(): b) template> \& cart) \{ int numltem = cart. getCurrentSize(); for(int i=0;i& cart) vector items = cart.toVector(); int numltem = items.size(); for(int i=0;i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions