Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone help me with this code. #include #include #include Inventory.h / / Allow the compiler to define the remaining / / comparison operators

Can someone help me with this code.
#include
#include
#include "Inventory.h"
// Allow the compiler to define the remaining
// comparison operators
using namespace std::rel_ops;
//----------------------------------------------------
Inventory::Inventory() : slots(10){}
//----------------------------------------------------
Inventory::Inventory(int n) : slots(n), allItemStacks(n){}
//----------------------------------------------------
Inventory::Inventory(const Inventory &src) : slots(src.slots), allItemStacks(src.allItemStacks){}
//----------------------------------------------------
Inventory::~Inventory(){}
//----------------------------------------------------
int Inventory::utilizedSlots() const {
return allItemStacks.size();
}
//----------------------------------------------------
int Inventory::emptySlots() const {
return slots - utilizedSlots();
}
//----------------------------------------------------
int Inventory::totalSlots() const {
return slots;
}
//----------------------------------------------------
bool Inventory::isFull() const {
return utilizedSlots()>= slots;
}
//----------------------------------------------------
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
";
for (const auto &itemStack : allItemStacks){
outs <<""<< itemStack <<"
";
}
}
//----------------------------------------------------
Inventory::iterator Inventory::findMatchingItemStackIterator(const ItemStack& itemStack){
return std::find(allItemStacks.begin(), allItemStacks.end(), itemStack);
}
//----------------------------------------------------
void Inventory::addItemStackNoCheck(ItemStack itemStack){
allItemStacks.push_back(itemStack);
}
//----------------------------------------------------
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;
}
for (auto lhIt = lhs.begin(), rhIt = rhs.begin(); lhIt != lhs.end() && rhIt != rhs.end(); ++lhIt, ++rhIt){
if (*lhIt !=*rhIt){
return false;
}
}
return true;
}
//----------------------------------------------------
void Inventory::mergeStacks(ItemStack& lhs, const ItemStack& rhs){
lhs.addItems(rhs.size());
}
I get
Processing Log:
Discarded (10) HP Potion
Discarded (5) MP Potion
Discarded (2) Bow Tie
Discarded (3) Dirt
Discarded (27) Iron Ore
Discarded (44) Diamond Ore
Discarded (55) Iron Ingot
Discarded (1) Diamond
Discarded (4) Diamond Block
Discarded (3) Dirt
Discarded (5) MP Potion
Discarded (4) Diamond Block
Discarded (1) Diamond
Discarded (2) Iron Ore
Item List:
0 Air

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David M. Kroenke, David J. Auer

7th edition

133544621, 133544626, 0-13-354462-1, 978-0133544626

More Books

Students also viewed these Databases questions

Question

Is the tone of the writing appropriate for the audience?

Answered: 1 week ago