In: Computer Science
C++
existing code
#include "ArrayBag.hpp"
#include <iostream>
/****************************************************** Public Methods *****************************************************/
/* Default Constructor */
template <typename ItemType>
ArrayBag<ItemType>::ArrayBag() : item_count_(0) { //
initializer list
} // end default constructor
template <typename ItemType>
int ArrayBag<ItemType>::getCurrentSize() const {
return item_count_;
}
template <typename ItemType>
bool ArrayBag<ItemType>::isEmpty() const {
return item_count_ == 0;
}
template <typename ItemType>
bool ArrayBag<ItemType>::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 <typename ItemType>
bool ArrayBag<ItemType>::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 <typename ItemType>
void ArrayBag<ItemType>::clear() {
item_count_ = 0;
}
template <typename ItemType>
bool ArrayBag<ItemType>::contains(const ItemType
&an_entry) const {
return getIndexOf(an_entry) > -1;
} // end contains
template <typename ItemType>
int ArrayBag<ItemType>::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 <typename ItemType>
void ArrayBag<ItemType>::operator+=(const
ArrayBag<ItemType> &a_bag) {
}
template <typename ItemType>
void ArrayBag<ItemType>::operator-=(const
ArrayBag<ItemType> &a_bag) {
}
template <typename ItemType>
void ArrayBag<ItemType>::operator/=(const
ArrayBag<ItemType> &a_bag) {
}
template <typename ItemType>
bool ArrayBag<ItemType>::operator==(const
ArrayBag<ItemType> &a_bag) {
}
template <typename ItemType>
bool ArrayBag<ItemType>::operator!=(const
ArrayBag<ItemType> &a_bag) {
}
/***************************************************** Private Methods *****************************************************/
template <typename ItemType>
int ArrayBag<ItemType>::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 and implement 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);
#include "ArrayBag.hpp"
#include <iostream>
/****************************************************** Public Methods *****************************************************/
/* Default Constructor */
template <typename ItemType>
ArrayBag<ItemType>::ArrayBag() : item_count_(0) { //
initializer list
} // end default constructor
template <typename ItemType>
int ArrayBag<ItemType>::getCurrentSize() const {
return item_count_;
}
template <typename ItemType>
bool ArrayBag<ItemType>::isEmpty() const {
return item_count_ == 0;
}
template <typename ItemType>
bool ArrayBag<ItemType>::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 <typename ItemType>
bool ArrayBag<ItemType>::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 <typename ItemType>
void ArrayBag<ItemType>::clear() {
item_count_ = 0;
}
template <typename ItemType>
bool ArrayBag<ItemType>::contains(const ItemType
&an_entry) const {
return getIndexOf(an_entry) > -1;
} // end contains
template <typename ItemType>
int ArrayBag<ItemType>::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 <typename ItemType>
void ArrayBag<ItemType>::operator+=(const
ArrayBag<ItemType> &a_bag) {
}
template <typename ItemType>
void ArrayBag<ItemType>::operator-=(const
ArrayBag<ItemType> &a_bag) {
}
template <typename ItemType>
void ArrayBag<ItemType>::operator/=(const
ArrayBag<ItemType> &a_bag) {
}
template <typename ItemType>
bool ArrayBag<ItemType>::operator==(const
ArrayBag<ItemType> &a_bag) {
}
template <typename ItemType>
bool ArrayBag<ItemType>::operator!=(const
ArrayBag<ItemType> &a_bag) {
}
/***************************************************** Private Methods *****************************************************/
template <typename ItemType>
int ArrayBag<ItemType>::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 and implement 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);
What to do can you explain brief ?