Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

One of the most important skills in our craft is interpreting error messages. Remember the ones you receive when you attempt to compile and run

One of the most important skills in our craft is interpreting error messages. Remember the ones you receive when you attempt to compile and run the unmodified code.
The key abstractions employed in this program are Item, ItemStack, and Inventory. Complete ADT implementations have been provided for Item and Inventory.
A partial implementation has been provided for the ItemStack. Your task is to finish the update ItemStack ADT.
This assignment is smaller than the previous two (in terms of code and number of new concepts). Most of your time will be spent reviewing the basics of pointers. Spend the time reviewing. Practice with pointers. You will need to use pointers (in one form or another) for the reminder of the semester.
You must implement the:
Copy Constructor
Destructor
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, Destructor, and swap you are done with the Big-3.
Logical Equivalence (i.e., operator==).
Less-Than (i.e., operator<).
swap
Refer to the comments in each function for additional detail.
#include
#include "ItemStack.h"
const Item ItemStack::DEFAULT_ITEM(0, "Air");
//------------------------------------------------------------------------------
ItemStack::ItemStack()
:ItemStack(DEFAULT_ITEM, 0)
{
}
//------------------------------------------------------------------------------
ItemStack::ItemStack(const Item& inputItem, int s)
:quantity(s)
{
// Create a copy (clone) of inputItem and reference it with the this->item
// pointer
this->item = inputItem.clone();
}
//------------------------------------------------------------------------------
ItemStack::ItemStack(const ItemStack& src)
{
//1. Create a copy (clone) of src.item and reference it with the
// this->item pointer.
//
//2. Do not forget to copy src.quantity.
//
}
//------------------------------------------------------------------------------
ItemStack::~ItemStack()
{
// Every pointer must be deleted to prevent memory leaks (item is a pointer).
}
//------------------------------------------------------------------------------
ItemStack& ItemStack::operator=(ItemStack rhs)
{
swap(*this, rhs);
return *this;
}
//------------------------------------------------------------------------------
Item ItemStack::getItem() const
{
return *(this->item);
}
//------------------------------------------------------------------------------
int ItemStack::size() const
{
return this->quantity;
}
//------------------------------------------------------------------------------
void ItemStack::addItems(int a)
{
this->quantity += a;
}
//------------------------------------------------------------------------------
void ItemStack::addItemsFrom(const ItemStack& other)
{
this->quantity += other.quantity;
}
//------------------------------------------------------------------------------
bool ItemStack::operator==(const ItemStack& rhs) const
{
// Compare this and rhs for equivalence based on the ids of this->item and
// rhs.item.
return false; // replace this line
}
//------------------------------------------------------------------------------
bool ItemStack::operator<(const ItemStack& rhs) const
{
// Order (sort) this and rhs based on the ids of this->item and rhs.item.
return false; // replace this line
}
//------------------------------------------------------------------------------
void ItemStack::display(std::ostream& outs) const
{
outs << std::right <<"("<< std::setw(2)<< this->size()<<")"
<<(this->getItem()).getName();
}
//------------------------------------------------------------------------------
void swap(ItemStack& lhs, ItemStack& rhs)
{
// Swap the item data members and quantity data members for lhs and rhs.
#ifndef ITEMSTACK_H_INCLUDED
#define ITEMSTACK_H_INCLUDED
#include
#include "Item.h"
using namespace std::rel_ops;
/**
* A Homogeneous--i.e., uniform--stack of Items.
*/
class ItemStack {
private:
/**
* Default Air item with id 0.
*/
static const Item DEFAULT_ITEM;
/**
* Item out of which the stack is composed.
*/<

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

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

2. What are the different stages of a play? Describe briefly.

Answered: 1 week ago