C++ - Create a Number Set class that takes in integer values ranged 0 to an inde
ID: 3729602 • Letter: C
Question
C++ - Create a Number Set class that takes in integer values ranged 0 to an independent upper Limit for each object.
Class requires these functions:
- A standard constructor that takes in an optional int to set upper limit of the set. (The standard default upper limit is 1000 and is represented by a const).
- A constructor that creates a new object containing the same set as the Number Set object in the parameter by copying it. (Copy constructor)
- Destructor to restore array to original status.
- insert and delete functions for elements of the set. (both type bool)
- function that takes in a number and determines if a set contains that number (type bool)
- string function that returns a string of the numbers that are in the set of the object (type string)
Private member variables of class:
- int for the upper limit of each set.
- static constant int for the default upper limit of the set
- pointer to represent array containing each set (bool *set)
Explanation / Answer
class Set{
private:
int upperLimit;
static const int defaultUpperLimit=1000;
int *array;
int current_index=0;
public:
Set(int size=defaultUpperLimit){
upperLimit=size;
}
Set(const Set& newObj){
upperLimit=newObj.upperLimit;
array=newObj.array;
}
~Set(){
array=new int[upperLimit];
}
bool insertToSet(int number){
if(current_index==upperLimit)
return false;
array[current_index++]=number;
return true;
}
bool deleteFromSet(){
if(current_index<0)
return false;
delete &array[current_index--];
return true;
}
bool contains(int num){
for(int i=0;i<=current_index;i++){
if(array[i]==num)
return true;
}
return false;
}
string setToString(){
string res;
for(int i=0;i<=current_index;i++){
res += std::to_string(array[i]);
}
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.