In: Computer Science
THIS IS IMPLEMENTED IN C++
The SetInt class (Annex B1) represents a set of integers. This class contains a constructor without parameters which creates an empty set and a constructor which receives as parameters an array of integers as well as its size and which creates a set of integers containing the elements of this array. A test program is given in Annex B2.
The class also contains a destructor and the copy constructor, as well as methods that have the following function:
add() allows you to add an integer to the set (if it does not already belong to it);
remove() allows you to delete an integer from the set;
contains() allows to test the membership of an integer to the set;
nbElem() which provides the number of elements of the set;
tabElem() which returns an array of dynamically allocated integers containing exactly the elements of the set (if the set is empty this function must return NULL).
containsAux() allows you to test whether an integer (first argument) belongs to the set in a given position (second argument).
We impose here the use of an array of integers as data members of the class to contain the elements of the set. At any time, the size of the array will be equal to the number of elements in the set, when the set is empty, no array must be allocated.
Questions :
Define the methods of the SetInt class, including constructors and destructors.
Annex B1
/*SetInt.h*/
#include <cstdlib>
using namespace std;
class SetInt
{
public:
SetInt(): elem(NULL), size(0) {};
SetInt(int [], int);
~ SetInt();
SetInt (const SetInt &); //copy constructor
void add(int);
void remove(int);
bool contains(int);
int nbElem();
int *tabElem();
private:
int *elem;
int size;
bool containsAux (int n, int &)
};
Annex B2
/*File myfile2.cpp*/
#include "SetInt.h"
int main() {
SetInt a; // object creation
while (true)
{
cout << "add an element" << << endl;
cin >> elem;
a.add(elem);
cout << "add an other element" << << endl;
cout << "(Y)es/(N)o :" << << endl;
string chain ;
getline(cin,chain); //reads a sequence of characters ending with an end of line and store it in the chain
//object (end of line not included)
if (chain[0]=='n'||'N')break;
}
return 0;
}
class SetInt{
private:
int *elem;
int size;
bool containsAux (int n, int pos){
if(pos>size){
return false;
}
if(elem[pos]==n){
return true;
}
return false;
}
public:
SetInt(): elem(NULL), size(0) {};
SetInt(int array[], int size){
elem = new int(size);
for (int i=0; i<size;i++){
elem[i]=array[i];
}
}
~ SetInt(){
}
SetInt (const SetInt & object){
elem =object.elem;
size=object.size;
} //copy constructor
void add(int newElement){
if(contains(newElement)){
return;
}
int *temp =new int(size+1);
for (int i=0; i<size;i++){
temp[i]=elem[i];
}
temp[size++]= newElement;
elem =temp;
temp=NULL;
delete(temp);
}
void remove(int newElement){
if(contains(newElement)){
int *temp =new int(--size);
for (int i=0; i<size;i++){
temp[i]=elem[i];
}
elem =temp;
temp=NULL;
delete(temp);
}
}
bool contains(int element){
for(int i=0;i<size;i++){
if(elem[i]==element){
return true;
}
}
return false;
}
int nbElem(){
return size;
}
int *tabElem(){
if(size==0){
return NULL;
}
int *temp =new int (size);
for(int i=0;i<size;i++){
temp[i]=elem[i];
}
return temp;
}
};