Here is my code so far: // #include <cstddef> // Constructor; creates and initia
ID: 3541562 • Letter: H
Question
Here is my code so far:
//
#include <cstddef>
// Constructor; creates and initializes an empty Bag
template <class ItemType>
ArrayBag<ItemType>::ArrayBag(): numberOfItems(0)
{
}
// Return the number of Items being stored in the Bag
template <class ItemType>
int ArrayBag<ItemType>::getCurrentSize() const
{
return numberOfItems; // STUBclass="apple-tab-span">class="apple-tab-span">
}
// Return the capacity of the bag (the maximum Items it can store)
template <class ItemType>
int ArrayBag<ItemType>::getCapacity( ) const
{
return DEFAULT_CAPACITY; // STUBclass="apple-tab-span">class="apple-tab-span">
}
// Report whether the Bag is empty
// Return true if the Bag is empty (storing no Items);
// Return false if Items exist in the Bag
template <class ItemType>
bool ArrayBag<ItemType>::isEmpty() const
{
if (numberOfItems == 0)class="apple-tab-span">
{class="apple-tab-span">
return true;class="apple-tab-span">
}class="apple-tab-span">
else {class="apple-tab-span">
return false;class="apple-tab-span">
} // STUBclass="apple-tab-span">class="apple-tab-span">
}
// Report whether the Bag is full
// Return true if the Bag is filled to capacity
// Return false if there is still room
template <class ItemType>
bool ArrayBag<ItemType>::isFull() const
{
if (numberOfItems == DEFAULT_CAPACITY)class="apple-tab-span">
{class="apple-tab-span">
return true;class="apple-tab-span">
}class="apple-tab-span">
else {class="apple-tab-span">
return false;class="apple-tab-span">
}class="apple-tab-span">
// Give the Bag a new Item to store
// If Bag is full, nothing changes
// Else, Bag must add this Item to its Item array and update its numberOfItems
// If Bag is full after this, return true; else return false
template <class ItemType>
bool ArrayBag<ItemType>::add(const ItemType& newItem)
{
bool bueno = (numberOfItems < DEFAULT_CAPACITY);class="apple-tab-span">
if (bueno)class="apple-tab-span">
{class="apple-tab-span">
items[numberOfItems] = newItem;class="apple-tab-span">
numberOfItems++;class="apple-tab-span">
}class="apple-tab-span">
return bueno;class="apple-tab-span">
}
// Make the Bag act like an empty Bag again
template<class ItemType>
void ArrayBag<ItemType>::clear()
{
numberOfItems = 0;class="apple-tab-span">
// empty STUBclass="apple-tab-span">
}
// Remove an Item from the bag
// If Item is not there, nothing changes and we return false
// Else, we fill in its spot in that Item array and count number of Items down
template <class ItemType>
bool ArrayBag<ItemType>::remove(const ItemType& anItem)
{
for (int i = 0; i < DEFAULT_CAPACITY; i++)
{
if (ArrayBag[i] == remove) class="apple-tab-span">
{
for (int x = i; x < DEFAULT_CAPACITY - 1; x++)class="apple-tab-span">
{
ArrayBag[x] = ArrayBag[x + 1]; class="apple-tab-span">
}
numberOfItems--;
}
else {class="apple-tab-span">
return false;class="apple-tab-span">
}class="apple-tab-span">
}
// Check if an Item is in the Bag
// Return true if it is in the Bag, and false if not
template <class ItemType>
bool ArrayBag<ItemType>::contains(const ItemType& anItem) const
{
for (int i = 0; i < DEFAULT_CAPACITY; i++) class="apple-tab-span">
{
if (ArrayBag[i] == contains) class="apple-tab-span">
{class="apple-tab-span">
return true;class="apple-tab-span">
}class="apple-tab-span">
else {class="apple-tab-span">
return false;class="apple-tab-span">
}class="apple-tab-span">
}
// Check how many times an Item is in the Bag
// return 0 if it's not there; otherwise,
// return the number of times it occurs
template <class ItemType>
int ArrayBag<ItemType>::getFrequencyOf(const ItemType& anItem) const
{
for (int i = 0; i < DEFAULT_CAPACITY; i++) class="apple-tab-span">
{
x = 0;class="apple-tab-span">
if (ArrayBag[i] == getFrequencyOf)class="apple-tab-span">
{class="apple-tab-span">
x++;class="apple-tab-span">
for (int y = i; y < DEFAULT_CAPACITY - 1; y++)class="apple-tab-span">
{class="apple-tab-span">
x++;class="apple-tab-span">
return x;class="apple-tab-span">
}
}
else {class="apple-tab-span">
return 0;class="apple-tab-span">
}class="apple-tab-span">
}
// Make an output vector of Items from the bag (for checking)
template <class ItemType>
vector<ItemType> ArrayBag<ItemType>::toVector() const
vector<ItemType> bagContents;class="apple-tab-span">
for (int i = 0; i < numberOfItems; i++)class="apple-tab-span">
bagContents.push_back(items[i])class="apple-tab-span">
return bagContents; class="apple-tab-span">class="apple-tab-span">
my errors are:
59:1 (template <class ItemType>; above add): error: a template declaration cannot appear at block scope
72:1 (template <class ItemType>; above clear): error: expected ';' before template
82:1 (template <class ItemType>; above remove) error: a template declaration cannot appear at block scope.
Thanks
// Created by Frank M. Carrano and Tim Henry.
// Copyright (c) 2013 __Pearson Education__. All rights reserved.
/** Listing 1-1.
@file BagInterface.h */
#ifndef _BAG_INTERFACE
#define _BAG_INTERFACE
#include <vector>
using namespace std;
template<class ItemType>
class BagInterface
{
public:
/** Gets the current number of entries in this bag.
@return The integer number of entries currently in the bag. */
virtual int getCurrentSize() const = 0;
/** Sees whether this bag is empty.
@return True if the bag is empty, or false if not. */
virtual bool isEmpty() const = 0;
/** Sees whether this bag is full.
@return True if the bag is full, or false if not. */
virtual int getCapacity() const = 0;
/** Sees whether this bag is full.
@return True if the bag is full, or false if not. */
virtual bool isFull() const = 0;
/** Adds a new entry to this bag.
@post If successful, newEntry is stored in the bag and
the count of items in the bag has increased by 1.
@param newEntry The object to be added as a new entry.
@return True if addition was successful, or false if not. */
virtual bool add(const ItemType& newEntry) = 0;
/** Removes one occurrence of a given entry from this bag,
if possible.
@post If successful, anEntry has been removed from the bag
and the count of items in the bag has decreased by 1.
@param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtual bool remove(const ItemType& anEntry) = 0;
/** Removes all entries from this bag.
@post Bag contains no items, and the count of items is 0. */
virtual void clear() = 0;
/** Counts the number of times a given entry appears in bag.
@param anEntry The entry to be counted.
@return The number of times anEntry appears in the bag. */
virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
/** Tests whether this bag contains a given entry.
@param anEntry The entry to locate.
@return True if bag contains anEntry, or false otherwise. */
virtual bool contains(const ItemType& anEntry) const = 0;
/** Empties and then fills a given vector with all entries that
are in this bag.
@return A vector containing all the entries in the bag. */
virtual vector<ItemType> toVector() const = 0;
}; // end BagInterface
#endif
Explanation / Answer
see here pastebin.com/tjA76e6T
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.