C++ existing code #include \"ArrayBag.hpp\" #include <iostream> /****************************************************** Public Methods *****************************************************/ /* Default Constructor */ template <typename ItemType> ArrayBag<ItemType>::ArrayBag() : item_count_(0) { // initializer...

80.2K

Verified Solution

Question

Programming

C++

existing code

#include \"ArrayBag.hpp\"
#include

/****************************************************** PublicMethods *****************************************************/

/* Default Constructor */
template
ArrayBag::ArrayBag() : item_count_(0) { //initializer list

} // end default constructor

template
int ArrayBag::getCurrentSize() const {

return item_count_;
}

template
bool ArrayBag::isEmpty() const {

return item_count_ == 0;
}

template
bool ArrayBag::add(const ItemType &new_entry){
  
bool has_room_to_add = (item_count_ < DEFAULT_CAPACITY);

if (has_room_to_add) {
items_[item_count_] = new_entry;
item_count_++;
} // end if

return has_room_to_add;
} // end add

template
bool ArrayBag::remove(const ItemType &an_entry){

int located_index = getIndexOf(an_entry);
bool can_remove_item = !isEmpty() && (located_index >-1);

if (can_remove_item) {
item_count_--;
items_[located_index] = items_[item_count_]; // copy
// last item in place of item to be removed
} // end if

return can_remove_item;
} // end remove

template
void ArrayBag::clear() {

item_count_ = 0;
}

template
bool ArrayBag::contains(const ItemType&an_entry) const {

return getIndexOf(an_entry) > -1;
} // end contains

template
int ArrayBag::getFrequencyOf(const ItemType&an_entry) const {
  
int frequency = 0;
int current_index = 0; // array index currently
// being inspected
while (current_index < item_count_) {
  
if (items_[current_index] == an_entry) {
frequency++;
} // end if
current_index++; // increment to next entry
} // end while

return frequency;
} //end getFrequencyOf

template
void ArrayBag::operator+=(constArrayBag &a_bag) {


}

template
void ArrayBag::operator-=(constArrayBag &a_bag) {


}

template
void ArrayBag::operator/=(constArrayBag &a_bag) {


}

template
bool ArrayBag::operator==(constArrayBag &a_bag) {

}

template
bool ArrayBag::operator!=(constArrayBag &a_bag) {

}

/***************************************************** PrivateMethods *****************************************************/

template
int ArrayBag::getIndexOf(const ItemType&target) const {

bool found = false;
int result = -1;
int search_index = 0;

while (!found && (search_index < item_count_)) { //if the bag is empty, item count is zero, so loop is skipped

if (items_[search_index] == target) {
found = true;
result = search_index;
}

else {

search_index++;
} // end if
} // end while

return result;
} // end getIndexOf

Define the following operator overloads in ArrayBag.hpp andimplement them accordingly in ArrayBag.cpp.

/**  Implements Set Union. The union of two sets A and B is the set of  elements, which are in A, in B, or in both A and B.  @param a_bag to be combined with the contents of this (the calling) bag  @post adds as many items from a_bag as space allows lhs += rhs,  the left hand side (the calling side) of the operator will be modified. **/void operator+=(const ArrayBag &a_bag);/**  Implements Set Difference  The (set) difference between two sets A and B is the set that  consists of the elements in A which are not elements of B.  @param a_bag to be subtracted from this (the calling) bag  @post removes all the data from items_ that is also found in a_bag   lhs -= rhs, the left hand side (the calling side) of the operator    will be modified, remove elements from lhs that are also elements    of the rhs (a_bag). **/void operator-=(const ArrayBag &a_bag);/**  Implements Set Intersection  The intersection of two sets A and B is the set that  consists of the elements that are in both A and B.  @param a_bag to be intersected with this (the calling) bag  @post items_ no longer contains elements not found in a_bag   lhs /= rhs, the left hand side (the calling side) of    the operator will be modified, remove elements from    lhs that are NOT elements of the rhs (a_bag).**/void operator/=(const ArrayBag &a_bag);/**  Implements Equal Comparison  Two ArrayBags are equal to each other if they contain the same elements  the order of the elements do not matter.  @param a_bag to be compared with this (the calling) bag  @return true if the two bags contain the same elements, false otherwise  Note that in order for two bags to be equal the must be of equal sizes.**/bool operator==(const ArrayBag &a_bag);/**  Implements Not Equal Comparison Opposite of the == operator, if two bags    have at least one element different they are not equal  @param a_bag to be compared with this (the calling) bag  @return true if two bags have a differing element, false if they are equal   In this case we can be sure that two arrays are not equal if they have                                different sizes.**/bool operator!=(const ArrayBag &a_bag);

Answer & Explanation Solved by verified expert
4.4 Ratings (653 Votes)
include ArrayBaghpp include Public Methods Default Constructor template ArrayBagArrayBag itemcount0 initializer list end default constructor template int ArrayBaggetCurrentSize const return itemcount template bool ArrayBagisEmpty const return itemcount 0 template bool ArrayBagaddconst ItemType newentry bool hasroomtoadd itemcount DEFAULTCAPACITY if hasroomtoadd itemsitemcount newentry itemcount end if return hasroomtoadd end add template bool ArrayBagremoveconst ItemType anentry int locatedindex getIndexOfanentry bool canremoveitem isEmpty locatedindex 1 if canremoveitem itemcount itemslocatedindex itemsitemcount copy last item in place of item to be removed end if return canremoveitem end remove template void ArrayBagclear itemcount 0 template    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students