Question
One of the most important skills in our craft is interpreting error messages. Remember the ones you receive when you attempt to compile the unmodified
One of the most important skills in our craft is interpreting error messages. Remember the ones you receive when you attempt to compile the unmodified code.
The key abstractions employed in this program are Item, ItemStack, and Inventory. Complete ADT implementations have been provided for the former two. A partial implementation has been provided for the Inventory. Your task is to finish the Inventory.
You can implement the Copy Constructor, findMatchingItemStack, and display with loops.
You can, alternatively, use the built-in C++ functions discussed in Review 03 Example 5 (if you like a challenge).
You must implement:
1. Copy Constructor
2. Assignment Operator
(Note this is already provided and complete. Refer to our discussions of the copy-and-swap method.
Once you have completed the Copy Constructor, you are done with the Big-3.)
3. Inventory::isFull - refer to documentation in Inventory.h.
4. Inventory::findMatchingItemStackIterator - refer to documentation in Inventory.h.
5. Inventory::addItemStackNoCheck - refer to documentation in Inventory.h.
6. Inventory::mergeStacks - refer to documentation in ItemStack.h.
7. Inventory::display. This must generate the Inventory summary.
Note there is no formatting applied with setw. All spacing consists of hard-coded spaces.
Employ your Head-to-Head Testing Skills from CS 250.
This is the code, on top of the instructions provided ,some of the comments in the code have instructions, please follow them as well.
#include#include #include "Inventory.h" // Allow the compiler to define the remaining // comparison operators using namespace std::rel_ops; //------------------------------------------------------------------------------ Inventory::Inventory() :Inventory(10) { } //------------------------------------------------------------------------------ Inventory::Inventory(int n) { this->slots = n; // this->allItemStacks.reserve(n); // only works for std::vector } //------------------------------------------------------------------------------ Inventory::Inventory(const Inventory& src) { // @todo - implement this function } //------------------------------------------------------------------------------ Inventory::~Inventory() { // Done! Be able to explain why. } //------------------------------------------------------------------------------ int Inventory::utilizedSlots() const { return allItemStacks.size(); } //------------------------------------------------------------------------------ int Inventory::emptySlots() const { return slots - utilizedSlots(); } //------------------------------------------------------------------------------ int Inventory::totalSlots() const { return slots; } //------------------------------------------------------------------------------ bool Inventory::isFull() const { // @todo - implement this function return false; // replace this line } //------------------------------------------------------------------------------ Inventory::iterator Inventory::begin() { return allItemStacks.begin(); } //------------------------------------------------------------------------------ Inventory::iterator Inventory::end() { return allItemStacks.end(); } //------------------------------------------------------------------------------ Inventory::const_iterator Inventory::begin() const { return allItemStacks.begin(); } //------------------------------------------------------------------------------ Inventory::const_iterator Inventory::end() const { return allItemStacks.end(); } //------------------------------------------------------------------------------ void Inventory::display(std::ostream &outs) const { outs << " -Used " << utilizedSlots() << " of " << slots << " slots" << " "; // @todo - implement the rest of function // // 2 spaces " " before each ItemStack line } //------------------------------------------------------------------------------ Inventory::iterator Inventory::findMatchingItemStackIterator(const ItemStack& itemStack) { // @todo - implement this function return allItemStacks.end(); } //------------------------------------------------------------------------------ void Inventory::addItemStackNoCheck(ItemStack itemStack) { // @todo - implement this function. This should be one push_back-y line... } //------------------------------------------------------------------------------ Inventory& Inventory::operator=(Inventory rhs) { std::swap(*this, rhs); return *this; } //------------------------------------------------------------------------------ void swap(Inventory& lhs, Inventory& rhs) { using std::swap; swap(lhs.allItemStacks, rhs.allItemStacks); swap(lhs.slots, rhs.slots); } //------------------------------------------------------------------------------ bool operator==(const Inventory& lhs, const Inventory& rhs) { if (lhs.utilizedSlots() != rhs.utilizedSlots()) { return false; } if (lhs.emptySlots() != rhs.emptySlots()) { return false; } // The two Inventory objects have the same number of used & unused slots using const_iterator = Inventory::const_iterator; const_iterator lhsIt = lhs.begin(); const_iterator rhsIt = rhs.begin(); while (lhsIt != lhs.end() && rhsIt != rhs.end()) { if (*lhsIt != *rhsIt) { return false; } lhsIt++; rhsIt++; } // If the two Inventory objects are identical, both iterators // will have reached end positions. return lhsIt == lhs.end() && rhsIt == rhs.end(); } //------------------------------------------------------------------------------ void Inventory::mergeStacks(ItemStack& lhs, const ItemStack& rhs) { // @todo - implement this function. There is no trick here (beyond // reviewing Assignment 1). }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started