本文整理汇总了C++中item::getWeight方法的典型用法代码示例。如果您正苦于以下问题:C++ item::getWeight方法的具体用法?C++ item::getWeight怎么用?C++ item::getWeight使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类item
的用法示例。
在下文中一共展示了item::getWeight方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: addWeightAndCount
void inventory::addWeightAndCount(itemNode* node, const item& newItem) {
if(newItem.getWeight() == node->getWeight()) {
node->incrementCount();
_weight += newItem.getWeight();
_count++;
newItem.printSuccess();
}
else {
std::cout << "ERROR: tried to add a duplicate item "
<< "with wrong weight!\n";
}
return;
}
示例2: AddItem
void inventory::AddItem(const item& newItem) {
// We start by checking the weight to see if it will overload us.
// If so, we don't do anything and can immediately return.
if(newItem.getWeight() + _weight > _maxWeight) {
newItem.printFailure();
return;
}
itemNode *newNode = new itemNode;
if(newNode) {
newNode->setItem(newItem);
itemNode *currentNode = _head;
itemNode *previousNode = NULL;
// Go to the first node that is greater than newItem.
// previousNode will either be NULL, (in which case it's
// at the head of the list) equal to newItem,
// (in which case it will be incremented)
// or less than newItem (in which case newNode will be
// put between previousNode and currentNode).
while(currentNode && newItem >= currentNode->getItem()) {
previousNode = currentNode;
currentNode = currentNode->getNext();
}
if(previousNode == NULL) {
newNode->setNext(_head);
_head = newNode;
addWeightAndCount(newNode, newItem);
}
else if(newItem == previousNode->getItem()) {
addWeightAndCount(previousNode, newItem);
delete newNode;
}
else {
previousNode->setNext(newNode);
newNode->setNext(currentNode);
addWeightAndCount(newNode, newItem);
}
}
return;
}
示例3: incWeight
//Adds the weight of aItem to the inventory's total weight
//Returns whether or not it succeeded.
bool inventory::incWeight(const item& aItem)
{
//If the new items weight added to the inventory total weight exceeds the
//MAX_WEIGHT value, return a failure.
double item_weight = aItem.getWeight();
if((item_weight + weight) > MAX_WEIGHT)
{
cout << "You're not strong enough to pick up the " << aItem << " with everything else you're carrying." << endl;
return false;
}
else
{
cout << "You picked up a " << aItem << "." << endl;
weight+=item_weight;
return true;
}
}
示例4: subtractWeightAndCount
void inventory::subtractWeightAndCount(const item& newItem) {
_weight -= newItem.getWeight();
_count--;
}