Design an ADT ‘Set’ whose objects should be able to store an integer set. Size of the set will be given by the user.
Data Members:
· int *data; /* pointer to an array of integers which will be treated as
set of integers */
· int noOfElements; // number of elements in the Set
· int capcity; /* maximum possible number of elements that can be
stored in the Set */
Supported Operations:
The class ‘Set’ should support the following operations
1. Set( i nt cap = 5 ); ( . 1)
S e t s ‘ c a p ’ t o ‘ c a p a c i ty ’ a nd i ni t i a l i ze s r e s t o f t he d a t a m e m b e r s
a c c o r d i ng l y . I f us e r s e nd s a ny i nv a l i d v a l u e t he n s e t s t he c a p t o d e f a ul t
v a l ue .
2. Set( Set & ref) (01)
The sizzling copy ctor J
3. ~Set() ( . 5)
F r e e t he d y na m i c a l l y a l l o c at e d m e mo r y .
4. voi d i nsert (i nt el ement ); ( . 5)
s t o r e s t he e le m e nt i n t he S e t .
5. voi d rem ov e (i nt element ); ( . 5)
r e m o v e s t he e l e me nt f r o m t he S e t .
6. int getCardinality() ( . 1)
r e t ur ns t he n um b e r o f e l e m e nt s i n t he s e t .
7. Set calcUnion ( Set & s2 ) (01)
r e t ur ns a ne w S e t o b j e c t w hi c h c o nt a i ns t he uni o n o f ‘ s 2 ’ s e t a nd c a l l i ng
o b j e ct s e t .
8. Set calcIntersection ( Set & s2 ) (01)
r e t ur ns a ne w S e t o b j e c t w hi c h c o nt a i ns t he i nt e r s e c t io n o f ‘ s 2 ’ s e t a nd
c a l l i ng o bj e c t s et .
9. Set calcSymmetricDifference ( Set & s2 ) (01)
r e t ur ns a ne w S e t o b j e c t w hi c h c o nt a i ns t he s y m m e t r i c Di f f e r e nc e o f
‘ s 2 ’ s e t a nd c a l l i ng o b j e c t s e t .
Where symm etri c di fferen ce i s: ADB = A U B - A I B
#include <set>
#include <stdexcept>
class Set {
int *set;
int cap;
int count;
public:
Set(int cap = 5) {
if (cap <= 0) { // if user enters invalid value
this->cap = 5; // set capacity to default
}
this->cap = cap;
this->set = newint[cap];
this->count = 0;
}
int *begin() const {
return set;
}
int *end() const {
return &set[this->count];
}
void insert(int elem) {
if (count == cap) {
throw std::range_error("The set is full!");
}
// insert the element
// should probably check if this element is already in the set
// before inserting it. This is a set data structure afterall
}
friend Set operator & (const Set& s1, const Set& s2) { // this is the set intersection function
std::set<int> andop;
for (int& v : s1) andop.insert(v);
for (int& v : s2) andop.insert(v);
Set *s = new Set(andop.size());
for (constint& v : andop) s->insert(v);
return *s;
}
Set calcIntersection(Set &s2) {
return (*this) & s2;
}
};
int main() {
Set a = 10;
Set b = 10;
return 0;
}
Since sets are used extensively in mathematics, it might be a good idea to understand the implementation from a math perspective.
Thanks alot for your time, i never though someone would waste time in it and i wanted step by codes to understand how to do it and you did it the way i wanted even if the bad font. Thanks again.