Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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 class Bag{ public: int count; std::string a[100];

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 #include #include "bag.h" using namespace std;

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

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Murach's SQL Server 2012 For Developers

Authors: Bryan Syverson, Joel Murach, Mike Murach

1st Edition

1890774693, 9781890774691

More Books

Students also viewed these Databases questions

Question

How do Dimensional Database Models differ from Relational Models?

Answered: 1 week ago

Question

What type of processing do Relational Databases support?

Answered: 1 week ago

Question

Describe several aggregation operators.

Answered: 1 week ago