Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Design and write a C++ class (Also please leave comments in the code to explain

ID: 667532 • Letter: D

Question

Design and write a C++ class (Also please leave comments in the code to explain what each code does) that implements a Set abstract data type and a menu-driven program that exercises the class. A typedef statement will identify the type of Items in the class's Sets. The program will manipulate sets of integers.
The program will maintain an array of three sets, identified by the integers 0, 1, and 2, and it will call class functions to carry out operations on the sets.
INPUT
The program will be interactive; it will respond to commands entered by the user.
OUTPUT
The program will prompt for inputs and will report the contents of sets in response to the user's commands.
ERRORS
The program should ignore non-command letters.
EXAMPLE
A run of the program might look like this:
a.out> sets
Enter a command according to this menu:
e n - Make set n empty.
i v n - Insert the value v in set n.
r v n - Remove the value v from set n.
u n1 n2 n3 - Assign the union of sets n1 and n2 to set n3.
s n - Report the size of set n.
p v n - Is value v present in set n?
w n - Write the contents of set n to the terminal.
h - See this menu.
q - Quit.
-> i 5 0
-> i 8 0
-> i 256 0
-> p 0
Set 0 = { 5, 8, 256 }
-> r 8 0
-> w 0
Set 0 = { 5, 256 }
-> w 2
Set 2 = { }
-> i 10 1
-> i 5 1
-> i 7 1
-> w 1
Set 1 = { 10, 5, 7 }
-> u 0 1 2
-> w 2
Set 2 = { 5, 256, 10, 7 }
-> s 2
Set 2 contains 4 values.
-> p 18 2
Set 2 does not contain 18.
-> q

OTHER REQUIREMENTS
In a class, implement a Set abstract data type whose values are sets of Items, with member functions corresponding to the operations listed in the example's menu:
A default constructor that initializes a newly declared Set to be empty.
A function that re-initializes a Set to be empty.
A function that inserts a value into a Set. If the Set already contains that value, the Set is unchanged.
A function that removes a value from a Set. If the Set does not initially contain the value, the Set is unchanged.
A function that forms the union of two Sets. It should be possible to assign that union to one of the original Sets.
A function that reports the size of a Set.
A function that reports if a value is present in a Set.
A function that writes the contents of a Set (in any order) to the terminal.
In the client program, use a constant to set the number of sets the program will manipulate.
HINTS
In each operation, consider the empty set.
The menu will be printed by a function in the client program.
Question: How might a function work that identifies the intersection of two Sets? If you like, you can include such a function in your class and exercise it in the program.
If this helps my following code right now is as follows.
Class Set{
Set() { //Constructor initialized with zero elements in the set
std::set<int> myset;
myset.clear();
}
void InsertFunc() {
std::set<int> myset;   //Here I have declared the myset variable as Set type
std::set<int>::iterator itr; //Used the iterator variable to check whether the element      inserted exists in the array already or not.

std::pair<std::set<int>::iterator,bool> retn; //Set the boolean value and returns the true or false if element already exists.
for (int i=1; i<=50; ++i) {
if (retn.second==false)
itr=ret.first; //If duplicate exist then it will not be added and point to same location to add new entry
retn = myset.insert(set[i]); //Check whether duplicate element exist
}
std::cout << "myset contains:";
for (itr=myset.begin(); itr!=myset.end(); ++itr) //Iterate through all the elements added
    std::cout << ' ' << *itr; //prints the Set in the ordered manner.
std::cout << ' ';
}
To delete the specific element from the array we have the function:
void Delete() {
std::set<int> myset;   //Here I have declared the myset variable as Set type
std::set<int>::iterator itr; //Used the iterator variable to check whether the element      inserted exists in the array already or not.
int a; //We have the variable which store the value of the number to be deleted from the set
cout<<"Enter the element to be deleted";
cin>>a;
myset.erase(a); //Erase is the standarad function that delete the element from the list.
std::cout << "myset contains:";
for (itr=myset.begin(); itr!=myset.end(); ++itr) //Iterate through all the elements
    std::cout << ' ' << *itr; //prints the Set in the ordered manner.
std::cout << ' ';
}
To find the size of the set we have size() function that count the number of the element in the container.
void DispSize() {
std::set<int> myset;   //Here I have declared the myset variable as Set type
std::cout << "size: " << myset.size() << ' ';
}
To destroy all the element from the set we have clear() .
Void Destroy() {
std::set<int> myset;
myset.clear();
std::cout << "myset contains:";
for (std::set<int>::iterator itr=myset.begin(); itr!=myset.end(); ++it)
    std::cout << ' ' << *itr;
std::cout << ' ';
}
}

Explanation / Answer

odd number

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote