In: Computer Science
C++ Class involving union.
The goal is to overload the function:
void Bag<T>::operator+=(const Bag<T>& a_bag)
// 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. For instance, Bag<int> bag1 = 1,2,3 and Bag<int> bag2 = 3,4,5 then bag1+=bag2 should return 1,2,3,4,5.
//Since type is void, it should not return an array.
#include <iostream> #include <string> #include <vector> using namespace std; template<class T> 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<T> toVector() const; void display() const; // displays the output void operator+=(const Bag<T>& 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<class T> Bag<T>::Bag(): item_count_(0) { } // end default constructor template<class T> int Bag<T>::getCurrentSize() const { return item_count_; } // end getCurrentSize template<class T> bool Bag<T>::isEmpty() const { return item_count_ == 0; } // end isEmpty template<class T> bool Bag<T>::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<class T> void Bag<T>::display() const { for(int x = 0; x < item_count_;x++) cout << items_[x] << ", "; } /** @return true if an_etry was successfully removed from items_, false otherwise **/ template<class T> bool Bag<T>::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<class T> void Bag<T>::clear() { item_count_ = 0; } // end clear template<class T> int Bag<T>::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<class T> bool Bag<T>::contains(const T& an_entry) const { return getIndexOf(an_entry) > -1; } // end contains template<class T> std::vector<T> Bag<T>::toVector() const { std::vector<T> bag_contents; for (int i = 0; i < item_count_; i++) bag_contents.push_back(items_[i]); return bag_contents; } // end toVector
template<class T> void Bag<T>::operator+=(const Bag<T>& a_bag)
{}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template<class T>
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<T> toVector() const;
void display() const; // displays the output
void operator+=(const Bag<T>& 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<class T>
Bag<T>::Bag(): item_count_(0)
{
} // end default constructor
template<class T>
int Bag<T>::getCurrentSize() const
{
return item_count_;
} // end getCurrentSize
template<class T>
bool Bag<T>::isEmpty() const
{
return item_count_ == 0;
} // end isEmpty
template<class T>
bool Bag<T>::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<class T>
void Bag<T>::display() const
{
for(int x = 0; x < item_count_;x++)
cout << items_[x] << ", ";
}
/**
@return true if an_etry was successfully removed from items_, false
otherwise
**/
template<class T>
bool Bag<T>::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<class T>
void Bag<T>::clear()
{
item_count_ = 0;
} // end clear
template<class T>
int Bag<T>::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<class T>
bool Bag<T>::contains(const T& an_entry) const
{
return getIndexOf(an_entry) > -1;
} // end contains
template<class T>
std::vector<T> Bag<T>::toVector() const
{
std::vector<T> bag_contents;
for (int i = 0; i < item_count_; i++)
bag_contents.push_back(items_[i]);
return bag_contents;
} // end toVector
template<class T>
// Overloads += operator
void Bag<T>::operator+=(const Bag<T>& a_bag)
{
// Loops till number of elements of object a_bag
for(int c = 0; c < a_bag.item_count_; c++)
{
// To keep track number found or not (-1 for not found)
int f = -1;
// Loops till number of elements of implicit object object
for(int d = 0; d < item_count_; d++)
{
// Checks if parameter list c index position value is equals
to
// Implicit object d index position value
if(a_bag.items_[c] == items_[d])
{
// Assigns the value of d to f (for found)
f = d;
// Come out of the loop
break;
}// End of if condition
}// End of for loop
// Checks if f value is -1 then number not found
if(f == -1)
// Call the function to add the value at c index position of
// parameter object a_bag
add(a_bag.items_[c]);
}// End of for loop
}// end operator+=
// main function definition
int main()
{
// Creates two objects of class Bag of type integer
Bag <int> b1;
Bag <int> b2;
// Loops 3 times
for(int x = 1; x < 4; x++)
// Calls the function to add value x to bag one
b1.add(x);
// Loops 3 times
for(int x = 3; x < 6; x++)
// Calls the function to add value x to bag two
b2.add(x);
// Calls the function to display the bag two
cout<<"\n Bag One: ";
b1.display();
// Calls the function to display the bag two
cout<<"\n Bag Two: ";
b2.display();
b1 += b2;
// Calls the function to display the bag one after union
cout<<"\n After Union Bag One: ";
b1.display();
}// End of main function
Sample Output:
Bag One: 1, 2, 3,
Bag Two: 3, 4, 5,
After Union Bag One: 1, 2, 3, 4, 5,