Answered Essay: Write a C++ code to implement the Bag class to store strings. The imple

Write a C++ code to implement the Bag class to store strings. The implementation should include: a header file, an implementation file, an application file. A Bag class will include the following operations create an empty bag, insert a new string into a bag, remove one matching element and return true if successful, clear all matching elements and return the count of item removed, return the total number of elements in the bag, returns the number of copies of the given item present in the bag. Each line should include an explaination. In the application file will have the menu list and ask users to choose which operations that they want, also

Expert Answer

 

HEADER FILE

#ifndef _BAG_
#define _BAG_

class Bag {

private:
int count; // number of members in bag
string data[20]; // data store up to 20 string members

public:
Bag(); // empty Bag Constructor
bool insert(string); // Put a member in the bag
bool remove(string); // remove one matching member from the bag
int size(); // number of total members in bag
int clear(string); // remove all matching members from the bag
int sameinbag(string); // how many matching member in bag.
};

#endif

The Bag implementation Code

#include<iostream>
#include<string>
using namespace std;

Bag::Bag() // Constructor
{
// set the initial state of the bag
count=0;
}

int Bag::clear(string value) // clear/remove all matching members from the bag
{
int number_of_members_removed = 0;

// no matching member in the bag
if(sameinbag(value) == 0) return 0;
int index =0;
for(;index<count;index++){
if(data[index].compare(value)==0){
remove(value);
number_of_members_removed +=1;
}
}
return number_of_members_removed;
}

bool Bag::insert(string value) // Insert/Place a value in Bag
{
bool reply;
if(count <20 ) {
data[count]=value;
reply=true;
count++;
} else {
reply=false;
}
return reply;
}

int Bag::sameinbag(string value) // how many matching member in bag
{
int matching_members = 0;
int index;
for(index=0;index<count;index++)
if(data[index].compare(value)==0){
matching_members += 1;
}
return matching_members;
}

bool Bag::remove(string value) // remove one matching member from the bag
{
bool reply=false;
int index;

// no matching member in the bag
if(sameinbag(value) == 0) return reply;
reply=true;
index=0;
while(data[index].compare(value)!=0) index++;
for(;index<count;index++)
data[index]=data[index+1];
count–;
}

int Bag::size() // How many elements in bag?
{
return count;
}

Driver to test the Bag

#include<iostream>
#include<string>
using namespace std;

int main(int argc, char *argv[])
{

// create an object of bag class
Bag b;
string value;
cout << “Enter your choice or press -1 to exit” << endl;
cout << “1. Total number of elements in the bag” <<endl;
cout << “2. Put a element in the bag” <<endl;
cout << “3. Remove one matching element from the bag” <<endl;
cout << “4. Remove all matching elements from the bag” <<endl;
cout << “5. How many matching elements in the bag” <<endl;

int option;
cin >> option;
while(option!= -1){
switch(option){

case 1 :
{
cout << b.size() << ” elements in the bag.” << endl;
break;
}

case 2 :
{
cout << “Enter string: ” << endl;
string str;
cin >> str;
b.insert(str);
break;
}

case 3 :
{
cout << “Enter string to delete: ” <<endl;
string str1;
cin >> str1;
cout << “Removed: ” << b.remove(str1);
break;
}

case 4 :
{
cout << “Enter string to delete: ” <<endl;
string str2;
cin >> str2;
cout <<“Number of elements removed: ” << b.clear(str2);
break;
}
case 5 :
{
cout << “Enter string to count: ” <<endl;
string str3;
cin >> str3;
cout <<“Number of matching elements : ” << b.sameinbag(str3);
break;
}
}
int temp_option;
cin >> temp_option;
option = temp_option;
}
return 0;
}

Buy Essay
Calculate your paper price
Pages (550 words)
Approximate price: -

Help Me Write My Essay - Reasons:

Best Online Essay Writing Service

We strive to give our customers the best online essay writing experience. We Make sure essays are submitted on time and all the instructions are followed.

Our Writers are Experienced and Professional

Our essay writing service is founded on professional writers who are on stand by to help you any time.

Free Revision Fo all Essays

Sometimes you may require our writers to add on a point to make your essay as customised as possible, we will give you unlimited times to do this. And we will do it for free.

Timely Essay(s)

We understand the frustrations that comes with late essays and our writers are extra careful to not violate this term. Our support team is always engauging our writers to help you have your essay ahead of time.

Customised Essays &100% Confidential

Our Online writing Service has zero torelance for plagiarised papers. We have plagiarism checking tool that generate plagiarism reports just to make sure you are satisfied.

24/7 Customer Support

Our agents are ready to help you around the clock. Please feel free to reach out and enquire about anything.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

HOW OUR ONLINE ESSAY WRITING SERVICE WORKS

Let us write that nagging essay.

STEP 1

Submit Your Essay/Homework Instructions

By clicking on the "PLACE ORDER" button, tell us your requires. Be precise for an accurate customised essay. You may also upload any reading materials where applicable.

STEP 2

Pick A & Writer

Our ordering form will provide you with a list of writers and their feedbacks. At step 2, its time select a writer. Our online agents are on stand by to help you just in case.

STEP 3

Editing (OUR PART)

At this stage, our editor will go through your essay and make sure your writer did meet all the instructions.

STEP 4

Receive your Paper

After Editing, your paper will be sent to you via email.