Question: C++ Programming //ArrayBag.cpp // Created by Frank M. Carrano and Timothy M. Henry. // Copyright (c) 2017 Pearson Education, Hoboken, New Jersey. template bool ArrayBag
C++ Programming

//ArrayBag.cpp
// Created by Frank M. Carrano and Timothy M. Henry. // Copyright (c) 2017 Pearson Education, Hoboken, New Jersey. templatebool ArrayBag ::add(const ItemType& newEntry) { bool hasRoomToAdd = (itemCount ArrayBag ::ArrayBag(): itemCount(0), maxItems(DEFAULT_CAPACITY) { } // end default constructor template int ArrayBag ::getCurrentSize() const { return itemCount; } // end getCurrentSize template bool ArrayBag ::isEmpty() const { return itemCount == 0; } // end isEmpty template std::vector ArrayBag ::toVector() const { std::vector bagContents; for (int i = 0; i bool ArrayBag ::contains(const ItemType& anEntry) const { bool isFound = false; int curIndex = 0; // Current array index while (!isFound && (curIndex void ArrayBag ::clear() { itemCount = 0; } // end clear template int ArrayBag ::getIndexOf(const ItemType& target) const { bool isFound = false; int result = -1; int searchIndex = 0; // If the bag is empty, itemCount is zero, so loop is skipped while (!isFound && (searchIndex bool ArrayBag ::remove(const ItemType& anEntry) { int locatedIndex = getIndexOf(anEntry); bool canRemoveItem = !isEmpty() && (locatedIndex > -1); if (canRemoveItem) { itemCount--; items[locatedIndex] = items[itemCount]; } // end if return canRemoveItem; } // end remove template int ArrayBag ::getFrequencyOf(const ItemType& anEntry) const { int frequency = 0; int curIndex = 0; // Current array index while (curIndex
//ArrayBag.h
// Created by Frank M. Carrano and Timothy M. Henry. // Copyright (c) 2017 Pearson Education, Hoboken, New Jersey. /** Header file for an array-based implementation of the ADT bag. Listing 3-1. @file ArrayBag.h */ #ifndef ARRAY_BAG_ #define ARRAY_BAG_ #include "BagInterface.h" templateclass ArrayBag : public BagInterface { private: static const int DEFAULT_CAPACITY = 6; // Small size to test for a full bag ItemType items[DEFAULT_CAPACITY]; // Array of bag items int itemCount; // Current count of bag items int maxItems; // Max capacity of the bag // Returns either the index of the element in the array items that // contains the given target or -1, if the array does not contain // the target. int getIndexOf(const ItemType& target) const; public: ArrayBag(); int getCurrentSize() const; bool isEmpty() const; bool add(const ItemType& newEntry); bool remove(const ItemType& anEntry); void clear(); bool contains(const ItemType& anEntry) const; int getFrequencyOf(const ItemType& anEntry) const; std::vector toVector() const; }; // end ArrayBag #include "ArrayBag.cpp" #endif
//BagInterface.h
// Created by Frank M. Carrano and Timothy M. Henry. // Copyright (c) 2017 Pearson Education, Hoboken, New Jersey. /** Listing 1-1. @file BagInterface.h */ #ifndef BAG_INTERFACE_ #define BAG_INTERFACE_ #includetemplate class BagInterface { public: /** Gets the current number of entries in this bag. @return The integer number of entries currently in the bag. */ virtual int getCurrentSize() const = 0; /** Sees whether this bag is empty. @return True if the bag is empty, or false if not. */ virtual bool isEmpty() const = 0; /** Adds a new entry to this bag. @post If successful, newEntry is stored in the bag and the count of items in the bag has increased by 1. @param newEntry The object to be added as a new entry. @return True if addition was successful, or false if not. */ virtual bool add(const ItemType& newEntry) = 0; /** Removes one occurrence of a given entry from this bag, if possible. @post If successful, anEntry has been removed from the bag and the count of items in the bag has decreased by 1. @param anEntry The entry to be removed. @return True if removal was successful, or false if not. */ virtual bool remove(const ItemType& anEntry) = 0; /** Removes all entries from this bag. @post Bag contains no items, and the count of items is 0. */ virtual void clear() = 0; /** Counts the number of times a given entry appears in this bag. @param anEntry The entry to be counted. @return The number of times anEntry appears in the bag. */ virtual int getFrequencyOf(const ItemType& anEntry) const = 0; /** Tests whether this bag contains a given entry. @param anEntry The entry to locate. @return True if bag contains anEntry, or false otherwise. */ virtual bool contains(const ItemType& anEntry) const = 0; /** Empties and then fills a given vector with all entries that are in this bag. @return A vector containing all the entries in the bag. */ virtual std::vector toVector() const = 0; /** Destroys this bag and frees its assigned memory. (See C++ Interlude 2.) */ virtual ~BagInterface() { } }; // end BagInterface #endif
//bagtester.cpp
// Created by Frank M. Carrano and Timothy M. Henry. // Copyright (c) 2017 Pearson Education, Hoboken, New Jersey. #include#include #include "ArrayBag.h" using std::cout; using std::endl; using std::string; void displayBag(ArrayBag<:string>& bag) { cout bagItems = bag.toVector(); int numberOfEntries = (int)bagItems.size(); for (int i = 0; i & bag) { cout bag; cout
PART 2
Assignment 4.1 [20 points] Do Programming Problem 6 at the end of chapter 3. The problem refers to Programming Problem 5 of Chapter 1, which you didn't do, but it was just writing out specifications for a "Set" ADT, which is just a slight variation on the "Bag" ADT. Your task this week is to write an "Array Set" class to implement the "Set" ADT. You don't need to write out the specifications. The vast majority of your code will be copied directly from the ArrayBag class of chapter 3. You just need to make minor adjustments to reflect the fact that duplicate elements are not allowed. (Note that you will receive 0 if your submitted code allows duplicate elements, since this is pretty much the only thing we are changing.) Also, we won't have a frequency of member function. Your Array Set class must be derived from a "SetInterface" abstract class. There's almost no work for you to do here. Just copy the "Baginterface" class from chapter 1. None of the code in that abstract class will need to be modified, except that the frequency Of() member function will be removed. If you find the part about deriving the class from "SetInterface" confusing, be sure to ask about it in the discussion! Here is the source code from the text that you are to use as a starting point: ArrayBag.cpp ArrayBag.h BagInterface.h bagtester.cpp Assignment 4.2 [25 points] Add member functions setUnion(), setIntersection(), and setDifference(). (Don't add them to the SetInterface class -- this causes too many headaches.) These operations on sets are covered in Math 4. If you don't know what they mean, you can read about them in Exercises 6, 7, and 8 of chapter 1. The functions should all perform the operation on the Array Set calling object and the ArraySet parameter and return the resulting Array Set. The setUnion() function should have an additional parameter that is set to false if the operation fails (because the size of the result exceeds the capacity of the ArraySet). Here's a snippet of client code: bool myFlag; set1 = set2.setUnion (set3, myFlag); if (!myFlag) { cout & (Array Set
- by reference), rather than Array Set
- (without the &), so that we return a reference to the Array Set rather than a copy of it. We could return the ArraySet object by value, but since it's better practice to return objects by reference, we will do so. To return an object by value, the computer has to make a copy of it, which can be costly, since objects can be quite large. (This is why we pass object parameters by reference, by the way). The problem with returning an object by reference is that you have to make sure that you aren't returning a reference to a local variable that was created on the stack, because, as you know, those local variables will be destroyed when control is returned to the calling function, which will cause the reference to be a reference to deallocated memory. To resolve this, we declare a pointer to the type of the object (Array Set
- ), and use the "new" operator to allocate memory for the object on the heap (so that it is not destroyed when control is returned to the calling function). Then, at the end of the function, we use the pointer to return the object. For example: ArraySet
* resultPtr = new ArraySet ; return *resultPtr; Documentation You can copy the documentation from the BagInterface class and make edits to that documentation so that it is appropriate for your SetInterface class. No need to repeat this documentation in the Array Set class itself. You will need to write the documentation for the setUnion(), setIntersection, and setDifference() member functions. Follow the pattern used in the existing BagInterface class. Submit Your Work Name your source code files setinterface.h, arrayset.h, and arrayset.cpp. Don't submit the client program that you used for testing, or your output. Use the Assignment Submission link to submit the three files. When you submit your assignment, on the Canvas assignment submission page there will be a text field in which you can add a note to me (called a "comment", but don't confuse it with a C++ comment). In this "comments" section of the submission page let me know whether the class works as required. Assignment 4.1 [20 points] Do Programming Problem 6 at the end of chapter 3. The problem refers to Programming Problem 5 of Chapter 1, which you didn't do, but it was just writing out specifications for a "Set" ADT, which is just a slight variation on the "Bag" ADT. Your task this week is to write an "Array Set" class to implement the "Set" ADT. You don't need to write out the specifications. The vast majority of your code will be copied directly from the ArrayBag class of chapter 3. You just need to make minor adjustments to reflect the fact that duplicate elements are not allowed. (Note that you will receive 0 if your submitted code allows duplicate elements, since this is pretty much the only thing we are changing.) Also, we won't have a frequency of member function. Your Array Set class must be derived from a "SetInterface" abstract class. There's almost no work for you to do here. Just copy the "Baginterface" class from chapter 1. None of the code in that abstract class will need to be modified, except that the frequency Of() member function will be removed. If you find the part about deriving the class from "SetInterface" confusing, be sure to ask about it in the discussion! Here is the source code from the text that you are to use as a starting point: ArrayBag.cpp ArrayBag.h BagInterface.h bagtester.cpp Assignment 4.2 [25 points] Add member functions setUnion(), setIntersection(), and setDifference(). (Don't add them to the SetInterface class -- this causes too many headaches.) These operations on sets are covered in Math 4. If you don't know what they mean, you can read about them in Exercises 6, 7, and 8 of chapter 1. The functions should all perform the operation on the Array Set calling object and the ArraySet parameter and return the resulting Array Set. The setUnion() function should have an additional parameter that is set to false if the operation fails (because the size of the result exceeds the capacity of the ArraySet). Here's a snippet of client code: bool myFlag; set1 = set2.setUnion (set3, myFlag); if (!myFlag) { cout & (Array Set - by reference), rather than Array Set
- (without the &), so that we return a reference to the Array Set rather than a copy of it. We could return the ArraySet object by value, but since it's better practice to return objects by reference, we will do so. To return an object by value, the computer has to make a copy of it, which can be costly, since objects can be quite large. (This is why we pass object parameters by reference, by the way). The problem with returning an object by reference is that you have to make sure that you aren't returning a reference to a local variable that was created on the stack, because, as you know, those local variables will be destroyed when control is returned to the calling function, which will cause the reference to be a reference to deallocated memory. To resolve this, we declare a pointer to the type of the object (Array Set
- ), and use the "new" operator to allocate memory for the object on the heap (so that it is not destroyed when control is returned to the calling function). Then, at the end of the function, we use the pointer to return the object. For example: ArraySet
* resultPtr = new ArraySet ; return *resultPtr; Documentation You can copy the documentation from the BagInterface class and make edits to that documentation so that it is appropriate for your SetInterface class. No need to repeat this documentation in the Array Set class itself. You will need to write the documentation for the setUnion(), setIntersection, and setDifference() member functions. Follow the pattern used in the existing BagInterface class. Submit Your Work Name your source code files setinterface.h, arrayset.h, and arrayset.cpp. Don't submit the client program that you used for testing, or your output. Use the Assignment Submission link to submit the three files. When you submit your assignment, on the Canvas assignment submission page there will be a text field in which you can add a note to me (called a "comment", but don't confuse it with a C++ comment). In this "comments" section of the submission page let me know whether the class works as required
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock

