Reference no: EM13829602
Problem:
Implement the following Set class. A set is a collection of items with no duplicates. The following class implements a set of integers.
#ifndef SET_H
#define SET_H
#include <iostream>
#include <vector>
using namespace std;
class Set
{
// friend operator functions
friend ostream &operator<<(ostream & out, const Set & s);
friend istream &operator>>(istream & in, Set & s);
public:
// Constructors
Set(); // default constructor
Set(const Set & s); // copy constructor
// destructor
~Set();
// operator functions
Set operator||(Set & s); // union of the set with the set s
Set operator&&(Set & s); // intersection of the set with the set s
const Set& operator=(const Set & s); // assignment
bool operator==(const Set & s); // equality
int& operator[](int index); // returns modifiable lvalue
int operator[](int index) const; // returns rvalue
// Set membership functions
bool member(int m); // The integer m is a member
void add(int m); // add the integer m to the set
void remove(int m); // remove the integer m from the set
private:
vector<int> _s;
};
#endif
The member functions must be written in a file Set.cpp. Now consider which can contain duplicate items. Thus a Set is a Bag but not vice versa in general. Design a polymorphic class hierarchy for Sets and Bags. The header file for the Set class in this hierarchy must be given in Set.h and the header file for Bag class must be given in Bag.h. Provide the implementation files Set.cpp and Bag.cpp.. The following are some examples of Bag intersection (), and Bag union ().∩∪
{1,2,2,2,3,3}{1,1,2,2,3} = {1,2,2,3} ∩
{1,2,2,2,3,3}{1,1,2,2,3} = {1,1,2,2,2,3,3}∪
You must clearly identify which member functions must be virtual
Additional Information:
This question is from Computer Science as well as it explains about designing the polymorphic class hierarchy for sets and bags.