Jellybag ———————-

This code will build & run, but the last part of the main file, which executes a function, does not correctly use polymorphism.Your task is to update bag/set/magicbag/bean .h and .cpp files so that polymorphism works, and the main file gives the desired output.Requirements:JellyBag should be the parent class. The others are child classes of JellyBag. A JellyBag just holds JellyItems in no particular order. Repeats are allowed.JellySet eliminates duplicate values.JellyMagicBag is the same as JellyBag except when iterating through values (using next()), the order is random.JellyBean can only hold zero or one JellyItems, so adding a second item removes the first.When you rewrite the classes, you must maximize the benefit of inheritance: do not write any extra code in the child classes that is not absolutely required. Call on your parent methods. Note, sometimes your child class will define a method like add() that must call the JellyBag version of add(). To do so, you must write JellyBag::add() in the child method because if you just write add() then the child method will call itself infinitely.Makefile:
SRC_FILES := $(wildcard *.cpp)
OBJ_FILES := $(patsubst %.cpp,%.o,$(SRC_FILES))
CXXFLAGS = -Wall -MMD -g
all: main
main: $(OBJ_FILES)
g++ -o $@ $^
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
g++ $(CXXFLAGS) -c -o $@ $< -include $(OBJ_FILES:.o=.d) .PHONY: clean clean: rm -f main *.o *.d ----------------------------------------------------------------bag.h: #ifndef BAG_H #define BAG_H #include "item.h" class JellyBag { public: JellyBag(); bool add(const JellyItem &item); bool contains(const JellyItem &item) const; bool remove(const JellyItem &item); // removes single occurrence int size() const; void reset(); bool has_next() const; JellyItem& next(); ~JellyBag(); }; private: JellyItem *items; int s; int pos; #endif ---------------------------------------------------------------bean.h: #ifndef BEAN_H #define BEAN_H #include "item.h" #include "bag.h" class JellyBean { public: JellyBean(); bool add(const JellyItem &item); // replaces item there is one bool contains(const JellyItem &item) const; bool remove(const JellyItem &item); // removes single item int size() const; void reset(); bool has_next() const; JellyItem& next(); }; private: JellyBag bag; #endif ---------------------------------------------------------------magicbag.cpp: #include #include "magicbag.h" using namespace std; JellyMagicBag::JellyMagicBag() { } bool JellyMagicBag::add(const JellyItem &item) { return bag.add(item); } bool JellyMagicBag::contains(const JellyItem &item) const { return bag.contains(item); } bool JellyMagicBag::remove(const JellyItem &item) { return bag.remove(item); } int JellyMagicBag::size() const { return bag.size(); } void JellyMagicBag::reset() { // no effect } bool JellyMagicBag::has_next() const { // if we have anything at all, we have a next return bag.size() > 0;
}
JellyItem& JellyMagicBag::next() {
// start over every time, move forward random count
bag.reset();
for(int i = 0; i < rand() % bag.size(); i++) { bag.next(); } return bag.next(); } -------------------------------------------------------------main.cpp: #include #include #include "bag.h" #include "set.h" #include "bean.h" #include "magicbag.h" #include "item.h" using namespace std; void do_polymorphism(JellyBag *polybag) { cout add(JellyItem(55)); cout Purchase answer to see full attachment

Leave a Comment