A bag can contain more than one copy of an item. For example, the chapter descri
ID: 3848532 • Letter: A
Question
A bag can contain more than one copy of an item. For example, the chapter describes a bag that contains the number 4 and two cop- ies of the number 8. This bag behavior is different from a set, which can contain only a single copy of any given item. Write a new container class called set, which is similar to a bag, except that a set can contain only one copy of any given item. You'll need to change the interface a bit. For example, in- stead of the bag's count function, you'll want a con- stant member function such as this: boo 7 set Contains (const value type& target const; Postcondition: The return value is true if target is in the set, otherwise the return value is false. Make an explicit statement of the invariant ofthe set class. Do a time analysis for each operation. At thisExplanation / Answer
Please find your designed classes :
set.h
#include<iostream>
using namespace std;
class Set
{
private: // member data
enum { CAPACITY = 1000 }; // default capacity of stack
int capacity; // actual length of stack array
Object* SetArray; // the set array
int size;
public:
Set(int cap);
int sizee() const; //returns sizee of set
bool isEmpty() const; //to check whether set is empty
bool isFull() const; // to check whether set is full
void add(const Object& element); // adds the elementent to the set
void remove(const Object& element); // removes the elementent from the set
void print() const; // prints the elementents of the set
bool contains(const Object& target) const; //checks if target is present in the set
~Set(); //destructor
};
set.cpp
Set<Object>::Set(int cap=CAPACITY)
{
capacity=cap;
SetArray=new Object[cap];
size = 0;
}
int Set<Object>::sizee() const
{
return size;
}
bool Set<Object>::isEmpty() const
{
return (size==0);
}
bool Set<Object>::isFull() const
{
return (size==capacity);
}
void Set<Object>::add(const Object& element)
{
if(!isFull())
{
int i = 0;
bool flag = true;
for(i=0;i<size;i++)
{
if(element == SetArray[i])
{
flag = false;
break;
}
}
if(flag)
SetArray[size++] = element;
}
else
{
Object* temp = new Object[capacity];
copy(SetArray,SetArray+size,temp);
delete[]SetArray;
capacity += 2*capacity;
SetArray = new Object[capacity];;
copy(temp,temp+size,SetArray);
int i = 0;
bool flag = true;
for(i=0;i<size;i++)
{
if(element == SetArray[i])
{
flag = false;
break;
}
}
if(flag)
SetArray[size++] = element; }
}
void Set<Object>::remove(const Object& element)
{
if(!isEmpty())
{
int i = 0;
int j;
bool flag = true;
for(i=0;i<size;i++)
{
if(element == SetArray[i])
{
flag = false;
break;
}
}
if(!flag)
{
for(j=i;j<size-1;j++)
SetArray[j] = SetArray[j+1];
size--;
}
}
}
void Set<Object>::print() const
{
if(!isEmpty())
{
int i = 0;
for(i=0;i<size;i++)
{
cout<<SetArray[i]<<" ";
}
cout<<endl;
}
}
bool Set<Object>::contains(const Object& element) const
{
int i = 0;
bool flag = true;
for(i=0;i<size;i++)
{
if(element == SetArray[i])
{
return true;
}
}
return false;
}
Set<Object>::~Set()
{
delete[]SetArray;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.