C++ Class involving difference.
The goal is to overload the function:
void Bag::operator-=(const Bag& a_bag)
// The Set Difference between two sets A and B is the set that consists of the elements of A which are not elements of B. Bag bag1 = (1,2,3) and Bag bag2 = (2,4,5) then bag1-=bag2 should return 1,3,4,5.
//parameter a_bag to be subtracted from this (the calling) bag
//post removes all data from items_ that is also found in a_bag
//Since type is void, it should not return an array.
#include <iostream>
#include <string>
#include <vector>
using namespace std; template class Bag { public: Bag(); int getCurrentSize() const; bool isEmpty() const; bool add(const T& new_entry); bool remove(const T& an_entry); /** @post item_count_ == 0 **/ void clear(); /** @return true if an_etry is found in items_, false otherwise **/ bool contains(const T& an_entry) const; /** @return the number of times an_entry is found in items_ **/ int getFrequencyOf(const T& an_entry) const; /** @return a vector having the same cotntents as items_ **/ std::vector toVector() const; void display() const; // displays the output void operator-=(const Bag& a_bag); //DO NOT CHANGE RETURN TYPE protected: static const int DEFAULT_CAPACITY = 200; //max size of items_ T items_[DEFAULT_CAPACITY]; // Array of bag items int item_count_; // Current count of bag items int getIndexOf(const T& target) const; };
template Bag::Bag(): item_count_(0) { } // end default constructor template int Bag::getCurrentSize() const { return item_count_; } // end getCurrentSize template bool Bag::isEmpty() const { return item_count_ == 0; } // end isEmpty template bool Bag::add(const T& new_entry) { bool has_room = (item_count_ < DEFAULT_CAPACITY); //bool notDup = items_.getFrequencyOf(new_entry) == 0; if (has_room) //&& notDup) { items_[item_count_] = new_entry; item_count_++; return true; } // end if return false; } // end add template void Bag::display() const { for(int x = 0; x < item_count_;x++) cout << items_[x] << ", "; } /** @return true if an_entry was successfully removed from items_, false otherwise **/ template bool Bag::remove(const T& an_entry) { int found_index = getIndexOf(an_entry); bool can_remove = !isEmpty() && (found_index > -1); if (can_remove) { item_count_--; items_[found_index] = items_[item_count_]; } // end if return can_remove; } // end remove /** @post item_count_ == 0 **/ template void Bag::clear() { item_count_ = 0; } // end clear template int Bag::getFrequencyOf(const T& an_entry) const { int frequency = 0; int cun_index = 0; // Current array index while (cun_index < item_count_) { if (items_[cun_index] == an_entry) { frequency++; } // end if cun_index++; // Increment to next entry } // end while return frequency; } // end getFrequencyOf template bool Bag::contains(const T& an_entry) const { return getIndexOf(an_entry) > -1; } // end contains template std::vector Bag::toVector() const { std::vector bag_contents; for (int i = 0; i < item_count_; i++) bag_contents.push_back(items_[i]); return bag_contents; } // end toVector
template void Bag::operator-=(const Bag& a_bag)
{}
template void Bag::operator-=(const Bag& a_bag){ for(int i=item_count_; i>=0; i--) { T x = items_[i]; if(a_bag.contains(x)) { remove(x); } else { add(x); } } }
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.
Get Answers For Free
Most questions answered within 1 hours.