Please Help This: please follow style guidelines Rule 1: use const where appropr
ID: 668396 • Letter: P
Question
Please Help This:
please follow style guidelines
Rule 1: use const where appropriate
Rule 2: every member function must include a description in the header file including precondition and postcondition.
Rule 3: every member variable must include a description of what the variable holds.
Rule 4: Classes should be capitalized. Bag, not bag. Person, not person. (I know this is different than how the book does it)
Rule 5: member variables of classes should be preceded by “m_”. If it’s not a member variable DON’T precede with m_
Rule 6: static const variables should be all capital letters. e.g: static const std::size_t CAPACITY = 30;
Rule 7: declare public:, protected:, then private:
Rule 8: non-member variable names should be lowercase.
Rule 9: Every class you define should be in its own .cpp and .h.
create a member function in Bag called get that has a parameter called indexwhich is the index of the element in the Bag to return. The get function should return this element.
Make sure you have a reasonable Precondition for this function (Hint: what if the user passes an index of 7 when you only have 5 elements in the Bag?). Use assert to guarantee your precondition.
Write a main() that puts 5 ints in a Bag and then iterates through the bag with a for loop.
No user input is necessary for Part 1. Just pick 5 numbers to add to the bag.
//////// Bag.h //////
#ifndef BAG_BAG_H
#define BAG_BAG_H
#include <cstdlib>
namespace CSCI2421
{
class Bag
{
public:
typedef int value_type;
static const std::size_t MAX_BAG_SIZE = 30;
// Initializes the Bag
// Postcondition: Bag is initialized
Bag();
// inserts an element into the Bag
// Precondition: size() < CAPACITY
// Postcondition: A new copy of entry has been added to the Bag
void insert (const value_type& x);
// attempts to remove one element that is equal to x from the Bag.
// Returns: true if the element was removed, else false.
// Precondition: none
// Postcondition: one element that is equal to x is removed from the Bag.
bool erase_one (const value_type& x);
// erases every element that is equal to x from the Bag.
// Returns: the number of elements erased
// Precondition: none
// Postcondition: all the elements that are equal to x are removed from the Bag
std::size_t erase (const value_type& x);
// returns the number of elements in the bag
// Precondition: none
// Postcondition: none
std::size_t size() const;
// returns the number of elements equal to x in the Bag
// Precondition: none
// Postcondition: none
std::size_t count (const value_type& x) const;
private:
// the number of elements currently in the Bag
std::size_t m_NumberOfElements;
// where the Bag stores it's elements
value_type m_Data[MAX_BAG_SIZE];
};
}
#endif //BAG_BAG_H
///// Bag.cpp ///////
#include "Bag.h"
#include <cassert>
CSCI2421::Bag::Bag() : m_NumberOfElements(0)
{
}
void CSCI2421::Bag::insert(const value_type& x)
{
assert (size() < MAX_BAG_SIZE);
m_Data[m_NumberOfElements] = x;
m_NumberOfElements++;
}
bool CSCI2421::Bag::erase_one(const value_type& x)
{
for (std::size_t index = 0; index < m_NumberOfElements; index++)
{
if (m_Data[index] == x)
{
m_NumberOfElements--;
m_Data[index] = m_Data[m_NumberOfElements];
return true;
}
}
return false;
}
std::size_t CSCI2421::Bag::erase(const value_type& x)
{
std::size_t index = 0;
std::size_t numberRemoved = 0;
while (index < m_NumberOfElements)
{
if (m_Data[index] == x)
{
m_NumberOfElements--;
m_Data[index] = m_Data[m_NumberOfElements];
numberRemoved++;
}
else
{
index++;
}
}
return numberRemoved;
}
std::size_t CSCI2421::Bag::size() const
{
return m_NumberOfElements;
}
std::size_t CSCI2421::Bag::count(const value_type& x) const
{
std::size_t counter = 0;
for (std::size_t i = 0; i < m_NumberOfElements; i++)
{
if (m_Data[i] == x)
counter++;
}
return counter;
}
Explanation / Answer
Check this :
#include "Common.h" #include "ObjectMgr.h" #include "Database/DatabaseEnv.h" #include "Bag.h" #include "Log.h" #include "UpdateData.h" Bag::Bag(): Item() { m_objectType |= TYPEMASK_CONTAINER; m_objectTypeId = TYPEID_CONTAINER; m_valuesCount = CONTAINER_END; memset(m_bagslot, 0, sizeof(Item *) * MAX_BAG_SIZE); } Bag::~Bag() { for (uint8 i = 0; i < MAX_BAG_SIZE; ++i) if (Item *item = m_bagslot[i]) { if (item->IsInWorld()) { sLog.outCrash("Item %u (slot %u, bag slot %u) in bag %u (slot %u, bag slot %u, m_bagslot %u) is to be deleted but is still in world.", item->GetEntry(), (uint32)item->GetSlot(), (uint32)item->GetBagSlot(), GetEntry(), (uint32)GetSlot(), (uint32)GetBagSlot(), (uint32)i); item->RemoveFromWorld(); } delete m_bagslot[i]; } } void Bag::AddToWorld() { Item::AddToWorld(); for (uint32 i = 0; i < GetBagSize(); ++i) if (m_bagslot[i]) m_bagslot[i]->AddToWorld(); } void Bag::RemoveFromWorld() { for (uint32 i = 0; i < GetBagSize(); ++i) if (m_bagslot[i]) m_bagslot[i]->RemoveFromWorld(); Item::RemoveFromWorld(); } bool Bag::Create(uint32 guidlow, uint32 itemid, Player const* owner) { ItemPrototype const * itemProto = objmgr.GetItemPrototype(itemid); if (!itemProto || itemProto->ContainerSlots > MAX_BAG_SIZE) return false; Object::_Create(guidlow, 0, HIGHGUID_CONTAINER); SetEntry(itemid); SetFloatValue(OBJECT_FIELD_SCALE_X, 1.0f); SetUInt64Value(ITEM_FIELD_OWNER, owner ? owner->GetGUID() : 0); SetUInt64Value(ITEM_FIELD_CONTAINED, owner ? owner->GetGUID() : 0); SetUInt32Value(ITEM_FIELD_MAXDURABILITY, itemProto->MaxDurability); SetUInt32Value(ITEM_FIELD_DURABILITY, itemProto->MaxDurability); SetUInt32Value(ITEM_FIELD_FLAGS, itemProto->Flags); SetUInt32Value(ITEM_FIELD_STACK_COUNT, 1); // Setting the number of Slots the Container has SetUInt32Value(CONTAINER_FIELD_NUM_SLOTS, itemProto->ContainerSlots); // Cleaning 20 slots for (uint8 i = 0; i < MAX_BAG_SIZE; ++i) { SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (i*2), 0); m_bagslot[i] = NULL; } return true; } void Bag::SaveToDB() { Item::SaveToDB(); } bool Bag::LoadFromDB(uint32 guid, uint64 owner_guid, QueryResult_AutoPtr result) { if (!Item::LoadFromDB(guid, owner_guid, result)) return false; // cleanup bag content related item value fields (its will be filled correctly from `character_inventory`) for (uint8 i = 0; i < MAX_BAG_SIZE; ++i) { SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (i*2), 0); if (m_bagslot[i]) { delete m_bagslot[i]; m_bagslot[i] = NULL; } } return true; } void Bag::DeleteFromDB() { for (uint8 i = 0; i < MAX_BAG_SIZE; ++i) if (m_bagslot[i]) m_bagslot[i]->DeleteFromDB(); Item::DeleteFromDB(); } uint32 Bag::GetFreeSlots() const { uint32 slots = 0; for (uint32 i=0; i < GetBagSize(); ++i) if (!m_bagslot[i]) ++slots; return slots; } void Bag::RemoveItem(uint8 slot, bool /*update*/) { assert(slot < MAX_BAG_SIZE); if (m_bagslot[slot]) m_bagslot[slot]->SetContainer(NULL); m_bagslot[slot] = NULL; SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (slot * 2), 0); } void Bag::StoreItem(uint8 slot, Item *pItem, bool /*update*/) { assert(slot < MAX_BAG_SIZE); if (pItem && pItem->GetGUID() != this->GetGUID()) { m_bagslot[slot] = pItem; SetUInt64Value(CONTAINER_FIELD_SLOT_1 + (slot * 2), pItem->GetGUID()); pItem->SetUInt64Value(ITEM_FIELD_CONTAINED, GetGUID()); pItem->SetUInt64Value(ITEM_FIELD_OWNER, GetOwnerGUID()); pItem->SetContainer(this); pItem->SetSlot(slot); } } void Bag::BuildCreateUpdateBlockForPlayer(UpdateData *data, Player *target) const { Item::BuildCreateUpdateBlockForPlayer(data, target); for (uint32 i = 0; i < GetBagSize(); ++i) if (m_bagslot[i]) m_bagslot[i]->BuildCreateUpdateBlockForPlayer(data, target); } // If the bag is empty returns true bool Bag::IsEmpty() const { for (uint32 i = 0; i < GetBagSize(); ++i) if (m_bagslot[i]) return false; return true; } uint32 Bag::GetItemCount(uint32 item, Item* eItem) const { Item *pItem; uint32 count = 0; for (uint32 i=0; i < GetBagSize(); ++i) { pItem = m_bagslot[i]; if (pItem && pItem != eItem && pItem->GetEntry() == item) count += pItem->GetCount(); } if (eItem && eItem->GetProto()->GemProperties) { for (uint32 i=0; i < GetBagSize(); ++i) { pItem = m_bagslot[i]; if (pItem && pItem != eItem && pItem->GetProto()->Socket[0].Color) count += pItem->GetGemCountWithID(item); } } return count; } uint8 Bag::GetSlotByItemGUID(uint64 guid) const { for (uint32 i = 0; i < GetBagSize(); ++i) if (m_bagslot[i] != 0) if (m_bagslot[i]->GetGUID() == guid) return i; return NULL_SLOT; } Item* Bag::GetItemByPos(uint8 slot) const { if (slot < GetBagSize()) return m_bagslot[slot]; return NULL; }