Write a class called IntSet, which represents a mathematical set of integers. It
ID: 3764140 • Letter: W
Question
Write a class called IntSet, which represents a mathematical set of integers. It should have the following data members:
a pointer-to-int that points to a dynamically allocated array that holds the values currently in the IntSet
an int that holds the current size of the array - it will need to be updated whenever the add() method creates a larger array
an int that holds the number of values currently in the IntSet - it will need to be updated in the add() and remove() methods
The IntSet class should have the following methods:
a default constructor that initializes the first data member to point to an array of 10 ints, initializes the size of the array to 10, and initializes the number of values in the set to zero
a destructor that deallocates the dynamically allocated array
the size() method should return the number of values currently in the IntSet
the isEmpty() method should return true if the IntSet contains no integers, and return false otherwise
the contains() method should take an int parameter and return true if that value is in the IntSet, and return false otherwise - you will find this function useful
the add() method should take an int parameter and add that int to the IntSet (if that value is not already in the IntSet) - if the array is currently full and you need to add another int, then you must first increase the size of the array by allocating a new array that is twice as large, copying the contents of the old array into the new array, redirecting the data member pointer to the new array, and deallocating the old array
the remove() method should take an int parameter and remove it from the IntSet (if that value is in the IntSet) by shifting over all of the subsequent elements of the array
the addAll() method: setA.addAll(setB) should add to setA any values from setB that were not already in setA (i.e. setA becomes the union of the two sets)
the removeDifferent() method: setA.removeDifferent(setB) should remove from setA any values that are not also in setB (i.e. setA becomes the intersection of the two sets)
the removeSame() method: setA.removeSame(setB) should remove from setA any values that are also in setB (i.e. setA becomes the relative complement of setA in setB)
Note: When designing a class that has dynamically allocated data members, you would normally overload the copy constructor, the assignment operator, and possibly the equality operator. The copy constructor for a class is invoked any time you:
pass an object of that class by value
return an object of that class from a function
declare an object of that type and assign a value to it in the same line (if you assign a value on a different line then the assignment operator is invoked)
The files must be named: IntSet.hpp and IntSet.cpp
Explanation / Answer
IntSet.hpp:
----------------------------------
#ifndef INTSET_HPP
#define INTSET_HPP
//class representing mathematical set of integers
class IntSet
{
private:
int* values; //array containing values in IntSet
int size; //size of array
int numValues; //number of values in array
public:
IntSet(); //constructor
IntSet(IntSet&); //copy contructor
~IntSet(); //destructor
int getSize() const; //returns number of values currently in IntSet
bool isEmpty(); //returns whether IntSet is empty.
bool contains(int) const; //returns whether a given value is in IntSet
int value(int) const; //returns a value at a given index in IntSet
void add(int); //adds an integer to IntSet
void remove(int); //removes an integer from IntSet
void addAll(IntSet); //adds all the integers of a given IntSet to invoking IntSet
void removeDifferent(IntSet); //removes all integers from invoking IntSet not present in a given IntSet
void removeSame(IntSet); //removes all integers from invoking IntSet present in a given IntSet
void operator=(const IntSet&); //overloads assignment operator
bool operator==(const IntSet&); //overloads equality operator
};
#endif /* INTSET_HPP */
-------------------------------------------
IntSet.cpp:
---------------------------------
#include "IntSet.hpp"
//Implementation details of IntSet class
//Implementation of default constructor
IntSet::IntSet() {
values=new int[10];
size=10;
numValues=0;
}
//Implementation of copy constructor
IntSet::IntSet(IntSet& is):values(is.values),size(is.size),numValues(is.numValues) {
}
//Implementation of destructor
IntSet::~IntSet()
{
delete values;
size=0;
numValues=0;
}
//returns number of integers in IntSet
int IntSet::getSize() const
{
return numValues;
}
//returns whether IntSet is empty
bool IntSet::isEmpty()
{
return (numValues==0);
}
//returns whether val is present in IntSet
bool IntSet::contains(int val) const
{
bool flag=false;
for(int i=0;i<numValues;i++)
{
if(val==values[i])
{
flag=true;
break;
}
}
return flag;
}
//returns a value at a given index in IntSet
int IntSet::value(int index) const
{
int ele;
for(int i=0;i<numValues;i++)
{
if(index==i)
{
ele=values[index];
}
}
return ele;
}
//adds val to IntSet
void IntSet::add(int val)
{
bool flag=false;
//checks if val is already present
for(int i=0;i<numValues;i++)
{
if(val==values[i])
{
flag=true;
break;
}
}
//add val, if not present.
if(flag!=true)
{
if(numValues==size)
{
int i;
size=2*size;
int* temp=new int[size];
for(i=0;i<numValues;i++)
{
temp[i]=values[i];
}
temp[i]=val;
int* oldvalues=values;
values=temp;
delete oldvalues;
numValues++;
}
}
}
//removes val, if present, from IntSet
void IntSet::remove(int val)
{
bool flag=false;
int valpos;
//checks if val is already present
for(int i=0;i<numValues;i++)
{
if(val==values[i])
{
flag=true;
valpos=i;
break;
}
}
//removes val, if present
if(flag=true)
{
for(int i=valpos;i<numValues;i++)
{
values[i]=values[i+1];
}
numValues--;
}
}
//adds all the integers of a given IntSet to invoking IntSet
void IntSet::addAll(IntSet secondSet)
{
int i,j,temp;
bool found=false;
for(j=0;j<secondSet.getSize();j++)
{
temp=secondSet.value(j);
if(!this->contains(temp))
this->add(temp);
}
}
//removes all integers from invoking IntSet not present in a given IntSet
void IntSet::removeDifferent(IntSet secondSet)
{
int i,temp;
for(i=0;i<this->getSize();i++)
{
temp=this->value(i);
if(!secondSet.contains(temp))
this->remove(temp);
}
}
//removes all integers from invoking IntSet present in a given IntSet
void IntSet::removeSame(IntSet secondSet)
{
int i,temp;
for(i=0;i<this->getSize();i++)
{
temp=this->value(i);
if(secondSet.contains(temp))
this->remove(temp);
}
}
//overloads assignment = operator
void IntSet::operator =(const IntSet& secondSet)
{
this->values=secondSet.values;
this->size=secondSet.size;
this->numValues=secondSet.numValues;
}
//overloads equality == operator
bool IntSet::operator ==(const IntSet& secondSet)
{
int sz=secondSet.getSize();
for(int i=0;i<sz;i++)
{
int temp=secondSet.value(i);
if(!this->contains(temp))
return false;
}
return true;
}
-------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.