Question: hi.we are using c++, put comments to help me better understand Use of the C++ Template Classes list and vector The goal is to create

hi.we are using c++, put comments to help me better understand

Use of the C++ Template Classes list and vector

The goal is to create a Class named Bag used to contain items of template type Type, in a fixed size, primitive array. The items are added to a Bag object in random order. The class is to be a template class.

The union of two bags is a new bag containing the combined contents of the original two bags. Design and specify a method union for the ADT bag that returns as a new bag the union of the bag receiving the call to the method and the bag that is the methods one argument. Include sufficient comments to fully specify the method.

Note that the union of two bags might contain duplicate items. For example, if object x occurs five times in one bag and twice in another, the union of these bags contains x seven times. Specifically, suppose that bag1 and bag2 are bags; bag1 contains the strings a, b, and c; and bag2 contains the strings b, b, d, and e. The expression bag1.union(bag2) returns a bag containing the strings a, b, b, b, c, d, and e. Note that union does not affect the contents of bag1 and bag2.

1. Client-test.cpp that has just the method main and any other required support methods

2) Bag.h that declares the Class Bag

3) Bag.cpp that defines all of the class methods listed in the following UML table.

sample output

Example

Given: Bag bag1; Bag bag2;

Bag unionBag; "bag1": [1, 2, 3, 4, 2] "bag2": [5, 3, 7, 1, 9, 5] unionBag = bag1.bagsUnion(bag2); // "bag1" invokes the method Result: Union: [1, 1, 2, 2, 3, 3, 4, 5, 5, 7, 9]

Use the UML diagram to implement the class

-BAG_SIZE: static const int -items: Type[SIZE] -elementsUsed: int +Bag() +Bag(const &Type) +Bag(const Bag&) +~Bag() // Mutator Methods +operator=(const Bag&): const Bag& +addItem(const &Type): bool +removeItem(const Type&): bool +temoveAllItems(const Type&: int +empty(): void // Observer Methods +nmbrItems(): int +capacity(): int +isEmpty(): bool +isFull(): bool +contains(const &Type): bool +itemOccurrences(const Type&: int +bagsUnion(const Nag&): Bag +toString(): string

#This is a client test program

template void fillBag(Bag &bag, const Type values[], int size); template string removeItems(Bag &bag, const Type values[], int size, const string &name);

int main() { // Constant "const" Value Declarations const int NO_ERRORS = 0; // Program Execution Status: No errors const string BAG1_NAME("bag1"); const int BAG1_VALUES1[] = { 1, 2, 3, 4, 2 }; const int SIZE_BAG1_VALUES1 = sizeof(BAG1_VALUES1) / sizeof(BAG1_VALUES1[0]); const int BAG1_VALUES2[] = { 1, 2, 3, 4, 5}; const int SIZE_BAG1_VALUES2 = sizeof(BAG1_VALUES2) / sizeof(BAG1_VALUES2[0]); const int BAG2_VALUES1[] = { 5, 3, 7, 5, 1, 9, 5 }; const int SIZE_BAG2_VALUES1 = sizeof(BAG2_VALUES1) / sizeof(BAG2_VALUES1[0]); const int OCCURRENCES_VALUE = 5;

int main() { const int NO_ERRORS = 0; const string BAG1_NAME("bag1"); const int BAG1_VALUES1[] = { 1, 2, 3, 4, 2 }; const int SIZE_BAG1_VALUES1 = sizeof(BAG1_VALUES1) / sizeof(BAG1_VALUES1[0]); const int BAG1_VALUES2[] = { 1, 2, 3, 4, 5}; const int SIZE_BAG1_VALUES2 = sizeof(BAG1_VALUES2) / sizeof(BAG1_VALUES2[0]); const int BAG2_VALUES1[] = { 5, 3, 7, 5, 1, 9, 5 }; const int SIZE_BAG2_VALUES1 = sizeof(BAG2_VALUES1) / sizeof(BAG2_VALUES1[0]); const int OCCURRENCES_VALUE = 5; const int BAG2_VALUES2[] = { 6, 7, 8, 9, 10 }; const int SIZE_BAG2_VALUES2 = sizeof(BAG2_VALUES2) / sizeof(BAG2_VALUES2[0]); const int REMOVE_VALUES[] = { 3, 1, 5, 6, 4, 2, 3 }; const int SIZE_REMOVE_VALUES = sizeof(REMOVE_VALUES) / sizeof(REMOVE_VALUES[0]); const int BAG2_VALUES2[] = { 6, 7, 8, 9, 10 }; const int SIZE_BAG2_VALUES2 = sizeof(BAG2_VALUES2) / sizeof(BAG2_VALUES2[0]); const int REMOVE_VALUES[] = { 3, 1, 5, 6, 4, 2, 3 }; const int SIZE_REMOVE_VALUES = sizeof(REMOVE_VALUES) / sizeof(REMOVE_VALUES[0]);

Bag bag1;

Bag unionBag;

fillBag(bag1, BAG1_VALUES1, SIZE_BAG1_VALUES1); cout << " Example #1: Test \"addItem\", \"toString\", and Copy Constructor" << " Given:" << " Bag bag1;" << " fillBag(bag1, BAG1_VALUES1, SIZE_BAG1_VALUES1);" << " cout << bag1.toString();" << " Result:" << endl; cout << " \"bag1\": " << bag1.toString(); cout << " Given:" << " Bag bag2(bag1);" << " cout << bag2.toString();" << " Result:" << endl;

Bag bag2(bag1);

cout << " \"bag2\": " << bag2.toString() << endl; fillBag(bag1, BAG1_VALUES1, SIZE_BAG1_VALUES1);

cout << " \"bag1\": " << bag1.toString();

Bag bag2(bag1);

cout << " \"bag2\": " << bag2.toString() << endl;

fillBag(bag1, BAG1_VALUES2, SIZE_BAG1_VALUES2);

cout << " \"bag2\": " << bag2.toString() << endl; cout << " \"bag1\": " << bag1.toString() << endl;

template void fillBag(Bag &bag, const Type values[], int size) { bag.empty(); // Make sure the caller's "Bag" object is empty for (int index = 0; index < size; ++index) { Type nextItem = values[index]; if (!bag.addItem(nextItem)) cout << " Error: Unable to add item \"" << nextItem << "\" to the Bag" << endl; }

}

template string removeItems(Bag &bag, const Type values[], int size, const string &bagName) { ostringstream results; for (int index = 0; index < size; ++index) { Type item = values[index]; bool status; results << "Given: " << " " << bagName << ".remove(" << item << ");" << " Result:" << endl; status = bag.removeItem(item); if (!status) { if (bag.isEmpty()) results << " Error: \"" << bagName << "\" is empty!" << endl; else results << " Error: \"" << bagName << "\" does not contain \"" << item << "\"!" << endl; } results << " \"" << bagName << "\": " << bag.toString(); if (!status) results << " (unchanged)"; if (index < size - 1) results << endl; } return results.str(); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!