Question
The Bag ADT C++ Help! I'm trying to make a bag class that has an array of up to 100 string items, with functions that
The Bag ADT C++ Help! I'm trying to make a bag class that has an array of up to 100 string items, with functions that can add to the bag, remove from the bag, and display the contents of the bag.
Here is what I have so far:
bag.h Header
------------------------------
#include
Bag();//default ctor bool add(string item);//add item bool remove(string);//remove item void display(string);//display contents };
//Funtion defs Bag::Bag(){count = 0;}//default ctor
bool Bag::add(string)//add to bag { bool reply; if(count < 100){ a[count] = item; reply = true; } else{ reply = false; } return reply; }
bool Bag::remove(string item)//remove item { bool reply = false; int index; if(howmany(item)== 0) return reply; reply = true; index = 0; while(a[index] != item)index++; for(;index < count; index++) a[index] = a[index+1]; count --; }
void Bag::display(){
for(int i = 0; i <=100; i++)
cout << a[i];
}
-----------------------------------
Main Program
---------------------------------
#include
int main(){ Bag grabBag; string item;
grabBag.add(apple); grabBag.add(carrot); grabBag.add(pen); grabBag.display(); grabBag.remove(pen); grabBag.display(); }
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