C++ Sets Program: Create a class called Sets for positive integers that will hav
ID: 3843405 • Letter: C
Question
C++ Sets Program:
Create a class called Sets for positive integers that will have the following functions:
addElement , will take a positive integer and add it to the set
getElement, will take a position number and return the element at the position (return -1 if bad position)
getSize, return the size of the set
isSubset, takes a sets object (call it b) and see if the current set (call it a) is a subset of b. If so, return a Boolean true, else return a Boolean false
isProper, takes a sets object (call it b) and see if the current set (call it a) is a proper subset of b. Note, use the isSubset function first then see if b has at least one more element than a
printOrderedPairs, takes a sets object (call it b) and takes the current set (call it a) and prints the ordered pairs of of A X B
Have main create two objects: setA and setB.
Input the values into setA (end with a 0 or negative) and input the values into setB (end with 0 or negative).
Print the ordered pairs using printOrderedPairs.
Print if setA is a subset of setB.
Then print if setA is a proper subset of setB.
Explanation / Answer
The desired program is given below:
#include<iostream>
#include<set>
using namespace std;
class Sets
{
private:
set<int> elements;
public:
int getElement(int pos)
{
int i = 0;
for(set<int>::iterator it = elements.begin(); it!=elements.end(); ++it)
{
if(pos == i)
{
return (*it);
}
i++;
}
}
int getSize()
{
return elements.size();
}
void setElement(int val)
{
elements.insert(val);
}
};
int main()
{
Sets setA;
setA.setElement(10);
setA.setElement(20);
setA.setElement(30);
setA.setElement(40);
setA.setElement(50);
setA.setElement(0);
cout<<"The element on index position 0th in setA is "<<setA.getElement(0)<<endl;
cout<<"The element on index position 1st in setA is "<<setA.getElement(1)<<endl;
cout<<"The element on index position 2nd in setA is "<<setA.getElement(2)<<endl;
cout<<"The element on index position 3rd in setA is "<<setA.getElement(3)<<endl;
cout<<"The element on index position 4th in setA is "<<setA.getElement(4)<<endl;
cout<<"The element on index position 5th in setA is "<<setA.getElement(5)<<endl;
cout<<"The total number of elements in setA is "<<setA.getSize()<<endl;
Sets setB;
setB.setElement(60);
setB.setElement(70);
setB.setElement(80);
setB.setElement(90);
setB.setElement(100);
setB.setElement(110);
cout<<"The element on index position 0th in setB is "<<setB.getElement(0)<<endl;
cout<<"The element on index position 1st in setB is "<<setB.getElement(1)<<endl;
cout<<"The element on index position 2nd in setB is "<<setB.getElement(2)<<endl;
cout<<"The element on index position 3rd in setB is "<<setB.getElement(3)<<endl;
cout<<"The element on index position 4th in setB is "<<setB.getElement(4)<<endl;
cout<<"The element on index position 5th in setB is "<<setB.getElement(5)<<endl;
cout<<"The total number of elements in setB is "<<setB.getSize()<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.