Question
=== After copying the program included below, WRITE and TEST the following Functions: void most_expensive ( ) (shows the most expensive item) void show_reverse (
=== After copying the program included below, WRITE and TEST the following Functions:
void most_expensive ( ) (shows the most expensive item)
void show_reverse ( ) (shows everything in the bag in reverse order of showItems() )
void get_frequency (Item item) (show how many times an Item (the name) appears in the bag)
bool delete_item (Item item) (delete 1 item (based on the name), shift array elements to the left, return true if successful
int get_index_of (Item item) (returns the index of an item (based on the name) )
double sum_of_all ( ) (returns the sum of all Items)
#includeusing namespace std; class Item { private: string name; double cost; public: Item() { name = ""; cost = 0.0; } Item(string newName, double newCost) { name = newName; cost = newCost; } void show() { cout << "- " << name << " $" << cost << endl; } string get_name() { return name; } double get_cost() { return cost; } void set_item(string newName, double newCost) { name = newName; cost = newCost; } void setItem(Item item) { name = item.get_name(); cost = item.get_cost(); } }; class Bag { private: Item *items; int number_items; int max; public: Bag(int Max) { items = new Item[Max]; // an array of items number_items = 0; // this is the index of current item max = Max; // size of the bag/array } void insert_item(string name, double cost) { items[number_items].set_item(name, cost); number_items++; } void insertItem(Item item) { // items[number_items].set_item(item.get_name(), item.get_cost()); items[number_items].setItem(item); number_items++; } int size() { return max; } void showItems() { for(int i = 0; i < number_items; i++) items[i].show(); } void delete_first() { // shift all the elements one to the left for(int i = 0; i < max - 1; i++) items[i] = items[i+1]; number_items --; // resize bag } bool contains(string name_to_find) { for(int i = 0; i < number_items; i++) if(name_to_find == items[i].get_name()) return true; return false; } void sort_by_cost_acending() // bubble sort { for(int i = 0; i < number_items - 1; i++) for(int j = 0; j < number_items - 1; j++) if(items[j].get_cost() > items[j+1].get_cost()) swap(j, j+1); } void swap(int index1, int index2) { Item temp = items[index1]; items[index1] = items[index2]; items[index2] = temp; } }; int main() { /// Test to see if Item works cout << "Testing Item: "; Item anItem("Fuji Apple", 4.99); anItem.show(); }
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