Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

This assignment will help you get practice with: Abstract Data Types Inheritance…

  1. User

    This assignment will help you get practice with:

    Abstract Data Types Inheritance Polymorphism C++ pointers C++ Classes C++ virtual and pure virtual functions C++ Class Templates C++ Arrays Task:

    A container class is a data type that is capable of holding a collection of items and provides functions to access them. Bag is an example of a container class. It is an unordered collection of items that may have duplicates. In this assignment, you are asked to design and develop an abstract Bag class, called BagInterface, with the following fixed collection of operations: Insert an item of any type into a bag Query the bag contents: two queries Is an item in the bag? How many copies of an item is in the bag? Remove an item from the bag clear the bag Get the size of the bag How many items are there in the bag? Check if the bag is empty Check if the bag is full Assume the bag capacity is 20 In addition to BagInterface, you are asked to implement two classes of bag implementations: PlainBag and MagicChangeBag.

    PlainBag is a simple container that holds items of any type using an array implementation.

    MagicChangeBag is also very similar to PlainBag, with two exceptions. When an item is inserted into a magic change bag, it’ll magically disappear, and bag looks empty. Whenever a remove operation is invoked, all the items, except the one is removed will appear. do this question in C++ and in 3 different files. BagInterface.h PlainBag.h and PlaingBag.cpp MagicChangeBag.h and MagicChangeBag.cpp

  2. Assistant

    BagInterface.h:

    #ifndef BAGINTERFACE_H #define BAGINTERFACE_H

    template class BagInterface { public: virtual bool insert(const ItemType& item) = 0; virtual bool contains(const ItemType& item) const = 0; virtual int getFrequencyOf(const ItemType& item) const = 0; virtual bool remove(const ItemType& item) = 0; virtual void clear() = 0; virtual int getCurrentSize() const = 0; virtual bool isEmpty() const = 0; virtual bool isFull() const = 0; };

    #endif

    PlainBag.h:

    #ifndef PLAINBAG_H #define PLAINBAG_H

    #include "BagInterface.h"

    template class PlainBag : public BagInterface { private: static const int DEFAULT_CAPACITY = 20; ItemType items[DEFAULT_CAPACITY]; int itemCount; int maxItems;

    public: PlainBag(); bool insert(const ItemType& item); bool contains(const ItemType& item) const; int getFrequencyOf(const ItemType& item) const; bool remove(const ItemType& item); void clear(); int getCurrentSize() const; bool isEmpty() const; bool isFull() const; };

    #include "PlainBag.cpp"

    #endif

    PlainBag.cpp:

    #include "PlainBag.h"

    template PlainBag::PlainBag() : itemCount(0), maxItems(DEFAULT_CAPACITY) {}

    template bool PlainBag::insert(const ItemType& item) { if (itemCount < maxItems) { items[itemCount] = item; itemCount++; return true; } return false; }

    template bool PlainBag::contains(const ItemType& item) const { for (int i = 0; i < itemCount; i++) { if (items[i] == item) { return true; } } return false; }

    template int PlainBag::getFrequencyOf(const ItemType& item) const { int frequency = 0; for (int i = 0; i < itemCount; i++) { if (items[i] == item) { frequency++; } } return frequency; }

    template bool PlainBag::remove(const ItemType& item) { for (int i = 0; i < itemCount; i++) { if (items[i] == item) { items[i] = items[itemCount - 1]; itemCount--; return true; } } return false; }

    template void PlainBag::clear() { itemCount = 0; }

    template int PlainBag::getCurrentSize() const { return itemCount; }

    template bool PlainBag::isEmpty() const { return itemCount == 0; }

    template bool PlainBag::isFull() const { return itemCount == maxItems; }

    MagicChangeBag.h:

    #ifndef MAGICCHANGEBAG_H #define MAGICCHANGEBAG_H

    #include "BagInterface.h"

    template class MagicChangeBag : public BagInterface { private: static const int DEFAULT_CAPACITY = 20; ItemType items[DEFAULT_CAPACITY]; int itemCount; int maxItems;

    public: MagicChangeBag(); bool insert(const ItemType& item); bool contains(const ItemType& item) const; int getFrequencyOf(const ItemType& item) const; bool remove(const ItemType& item); void clear(); int getCurrentSize() const; bool isEmpty() const; bool isFull() const; };

    #include "MagicChangeBag.cpp"

    #endif

    MagicChangeBag.cpp:

    #include "MagicChangeBag.h"

    template MagicChangeBag::MagicChangeBag() : itemCount(0), maxItems(DEFAULT_CAPACITY) {}

    template bool MagicChangeBag::insert(const ItemType& item) { itemCount = 0; return true; }

    template bool MagicChangeBag::contains(const ItemType& item) const { for (int i = 0; i < itemCount; i++) { if (items[i] == item) { return true; } } return false; }

    template int MagicChangeBag::getFrequencyOf(const ItemType& item) const { if (itemCount == 0) { return 0; } return itemCount - 1; }

    template bool MagicChangeBag::remove(const ItemType& item) { for (int i = 0; i < itemCount; i++) { if (items[i] == item) { itemCount = 0; return true; } } return false; }

    template void MagicChangeBag::clear() { itemCount = 0; }

    template int MagicChangeBag::getCurrentSize() const { return itemCount; }

    template bool MagicChangeBag::isEmpty() const { return itemCount == 0; }

    template bool MagicChangeBag::isFull() const { return itemCount == maxItems; }

  3. User

    can you add comment

  4. Assistant

    BagInterface.h:

    #ifndef BAGINTERFACE_H #define BAGINTERFACE_H

    template class BagInterface { public: virtual bool insert(const ItemType& item) = 0; virtual bool contains(const ItemType& item) const = 0; virtual int getFrequencyOf(const ItemType& item) const = 0; virtual bool remove(const ItemType& item) = 0; virtual void clear() = 0; virtual int getCurrentSize() const = 0; virtual bool isEmpty() const = 0; virtual bool isFull() const = 0; };

    #endif

    PlainBag.h:

    #ifndef PLAINBAG_H #define PLAINBAG_H

    #include "BagInterface.h"

    template class PlainBag : public BagInterface { private: static const int DEFAULT_CAPACITY = 20; ItemType items[DEFAULT_CAPACITY]; int itemCount; int maxItems;

    public: PlainBag(); // Constructor bool insert(const ItemType& item); bool contains(const ItemType& item) const; int getFrequencyOf(const ItemType& item) const; bool remove(const ItemType& item); void clear(); int getCurrentSize() const; bool isEmpty() const; bool isFull() const; };

    #include "PlainBag.cpp"

    #endif

    PlainBag.cpp:

    #include "PlainBag.h"

    template PlainBag::PlainBag() : itemCount(0), maxItems(DEFAULT_CAPACITY) {}

    template bool PlainBag::insert(const ItemType& item) { if (itemCount < maxItems) { items[itemCount] = item; itemCount++; return true; } return false; }

    template bool PlainBag::contains(const ItemType& item) const { for (int i = 0; i < itemCount; i++) { if (items[i] == item) { return true; } } return false; }

    template int PlainBag::getFrequencyOf(const ItemType& item) const { int frequency = 0; for (int i = 0; i < itemCount; i++) { if (items[i] == item) { frequency++; } } return frequency; }

    template bool PlainBag::remove(const ItemType& item) { for (int i = 0; i < itemCount; i++) { if (items[i] == item) { items[i] = items[itemCount - 1]; itemCount--; return true; } } return false; }

    template void PlainBag::clear() { itemCount = 0; }

    template int PlainBag::getCurrentSize() const { return itemCount; }

    template bool PlainBag::isEmpty() const { return itemCount == 0; }

    template bool PlainBag::isFull() const { return itemCount == maxItems; }

    MagicChangeBag.h:

    #ifndef MAGICCHANGEBAG_H #define MAGICCHANGEBAG_H

    #include "BagInterface.h"

    template class MagicChangeBag : public BagInterface { private: static const int DEFAULT_CAPACITY = 20; ItemType items[DEFAULT_CAPACITY]; int itemCount; int maxItems;

    public: MagicChangeBag(); // Constructor bool insert(const ItemType& item); bool contains(const ItemType& item) const; int getFrequencyOf(const ItemType& item) const; bool remove(const ItemType& item); void clear(); int getCurrentSize() const; bool isEmpty() const; bool isFull() const; };

    #include "MagicChangeBag.cpp"

    #endif

    MagicChangeBag.cpp:

    #include "MagicChangeBag.h"

    template MagicChangeBag::MagicChangeBag() : itemCount(0), maxItems(DEFAULT_CAPACITY) {}

    template bool MagicChangeBag::insert(const ItemType& item) { itemCount = 0; return true; }

    template bool MagicChangeBag::contains(const ItemType& item) const { for (int i = 0; i < itemCount; i++) { if (items[i] == item) { return true; } } return false; }

    template int MagicChangeBag::getFrequencyOf(const ItemType& item) const { if (itemCount == 0) { return 0; } return itemCount - 1; }

    template bool MagicChangeBag::remove(const ItemType& item) { for (int i = 0; i < itemCount; i++) { if (items[i] == item) { itemCount = 0; return true; } } return false; }

    template void MagicChangeBag::clear() { itemCount = 0; }

    template int MagicChangeBag::getCurrentSize() const { return itemCount; }

    template bool MagicChangeBag::isEmpty() const { return itemCount == 0; }

    template bool MagicChangeBag::isFull() const { return itemCount == maxItems; }

  5. User

    can you add comment for the code

  6. Assistant

    Certainly! Here's the updated code with comments added for clarity:

    BagInterface.h:

    #ifndef BAGINTERFACE_H #define BAGINTERFACE_H

    template class BagInterface { public: // Pure virtual functions for basic bag operations virtual bool insert(const ItemType& item) = 0; virtual bool contains(const ItemType& item) const = 0; virtual int getFrequencyOf(const ItemType& item) const = 0; virtual bool remove(const ItemType& item) = 0; virtual void clear() = 0; virtual int getCurrentSize() const = 0; virtual bool isEmpty() const = 0; virtual bool isFull() const = 0; };

    #endif

    PlainBag.h:

    #ifndef PLAINBAG_H #define PLAINBAG_H

    #include "BagInterface.h"

    template class PlainBag : public BagInterface { private: static const int DEFAULT_CAPACITY = 20; ItemType items[DEFAULT_CAPACITY]; int itemCount; int maxItems;

    public: PlainBag(); // Constructor bool insert(const ItemType& item); bool contains(const ItemType& item) const; int getFrequencyOf(const ItemType& item) const; bool remove(const ItemType& item); void clear(); int getCurrentSize() const; bool isEmpty() const; bool isFull() const; };

    #include "PlainBag.cpp"

    #endif

    PlainBag.cpp:

    #include "PlainBag.h"

    template PlainBag::PlainBag() : itemCount(0), maxItems(DEFAULT_CAPACITY) {}

    template bool PlainBag::insert(const ItemType& item) { if (itemCount < maxItems) { items[itemCount] = item; itemCount++; return true; } return false; }

    template bool PlainBag::contains(const ItemType& item) const { for (int i = 0; i < itemCount; i++) { if (items[i] == item) { return true; } } return false; }

    template int PlainBag::getFrequencyOf(const ItemType& item) const { int frequency = 0; for (int i = 0; i < itemCount; i++) { if (items[i] == item) { frequency++; } } return frequency; }

    template bool PlainBag::remove(const ItemType& item) { for (int i = 0; i < itemCount; i++) { if (items[i] == item) { items[i] = items[itemCount - 1]; itemCount--; return true; } } return false; }

    template void PlainBag::clear() { itemCount = 0; }

    template int PlainBag::getCurrentSize() const { return itemCount; }

    template bool PlainBag::isEmpty() const { return itemCount == 0; }

    template bool PlainBag::isFull() const { return itemCount == maxItems; }

    MagicChangeBag.h:

    #ifndef MAGICCHANGEBAG_H #define MAGICCHANGEBAG_H

    #include "BagInterface.h"

    template class MagicChangeBag : public BagInterface { private: static const int DEFAULT_CAPACITY = 20; ItemType items[DEFAULT_CAPACITY]; int itemCount; int maxItems;

    public: MagicChangeBag(); // Constructor bool insert(const ItemType& item); bool contains(const ItemType& item) const; int getFrequencyOf(const ItemType& item) const; bool remove(const ItemType& item); void clear(); int getCurrentSize() const; bool isEmpty() const; bool isFull() const; };

    #include "MagicChangeBag.cpp"

    #endif

    MagicChangeBag.cpp:

    #include "MagicChangeBag.h"

    template MagicChangeBag::MagicChangeBag() : itemCount(0), maxItems(DEFAULT_CAPACITY) {}

    template bool MagicChangeBag::insert(const ItemType& item) { itemCount = 0; // Item disappears when inserted return true; }

    template bool MagicChangeBag::contains(const ItemType& item) const { for (int i = 0; i < itemCount; i++) { if (items[i] == item) { return true; } } return false; }

    template int MagicChangeBag::getFrequencyOf(const ItemType& item) const { if (itemCount == 0) { return 0; // No items in the bag } return itemCount - 1; // One less than the current item count to account for the disappearing item }

    template bool MagicChangeBag::remove(const ItemType& item) { for (int i = 0; i < itemCount; i++) { if (items[i] == item) { itemCount = 0; // All items except the removed item disappear return true; } } return false; }

    template void MagicChangeBag::clear() { itemCount = 0; // All items disappear }

    template int MagicChangeBag::getCurrentSize() const { return itemCount; }

    template bool MagicChangeBag::isEmpty() const { return itemCount == 0; }

    template bool MagicChangeBag::isFull() const { return itemCount == maxItems; }

  7. User

    can you show me the UML class diagram base on the solution

  8. Assistant

    Sure! Here's a UML class diagram representing the solution:

    ----------------------------------------
    |             BagInterface             |
    ----------------------------------------
    | + insert(item: ItemType): bool       |
    | + contains(item: ItemType): bool     |
    | + getFrequencyOf(item: ItemType): int|
    | + remove(item: ItemType): bool       |
    | + clear(): void                      |
    | + getCurrentSize(): int              |
    | + isEmpty(): bool                    |
    | + isFull(): bool                     |
    ----------------------------------------
                      ^
                      |
                      |
                      |
       ---------------------------------------
       |              PlainBag                |
       ---------------------------------------
       | - items: ItemType[DEFAULT_CAPACITY] |
       | - itemCount: int                    |
       | - maxItems: int                      |
       ---------------------------------------
       | + PlainBag()                         |
       | + insert(item: ItemType): bool       |
       | + contains(item: ItemType): bool     |
       | + getFrequencyOf(item: ItemType): int|
       | + remove(item: ItemType): bool       |
       | + clear(): void                      |
       | + getCurrentSize(): int              |
       | + isEmpty(): bool                    |
       | + isFull(): bool                     |
       ---------------------------------------
      
                  
       ---------------------------------------
       |          MagicChangeBag              |
       ---------------------------------------
       | - items: ItemType[DEFAULT_CAPACITY] |
       | - itemCount: int                    |
       | - maxItems: int                      |
       ---------------------------------------
       | + MagicChangeBag()                   |
       | + insert(item: ItemType): bool       |
       | + contains(item: ItemType): bool