Thanks in advance! This if the class Definition. #ifndef SET_H #define SET_H #in
ID: 3776447 • Letter: T
Question
Thanks in advance!
This if the class Definition.
#ifndef SET_H
#define SET_H
#include <iostream>
using namespace std;
class Set
{
public:
Set();
Set(int n);
Set(Set &);
int getSize() const;
bool contains(int n) const;
void add(int n);
Set& operator=(const Set &);
void out();
virtual~Set();
private:
int* elements;
int* size;
};
#endif
20.21 x D Memory Management p x set insert C++ Referen x es/1/83220 2169p/content/ 1494003 1/Memory%20Management.pdf This assignment is taken from the chapter entitled memory management (chapte 15 in the 2nd edition) Define a class called Set that stores integers in a dynamically allocated array of integers. class set public: void add int n); bool contains (int n) const; int get-size const private int* elements int size In a set, the order of elements does not matter, and every element can occur at most once. supply the add, contains and get size member functions and the big three memory management functions 1. Copy constructor 2. Assignment operator 3. DestructorExplanation / Answer
#ifndef SET_H
#define SET_H
#include <iostream>
using namespace std;
class Set
{
public:
Set()
{
this->capacity = 16;
this->elements = new int[16];
this->size=0;
};
Set(int n) {
this->capacity=n;
this->elements = new int[n];
this->size=0;
};
Set(Set &set) {
this->capacity = set.capacity;
this->size=set.size;
this->elements= new int[capacity];
for(int i=0; i<=this->size;i++) {
*(this->elements+i)=*(elements+i);
}
};
int getSize() const {return size;};
bool contains(int n) const;
void add(int n);
void operator=(const Set &);
void out();
virtual~Set() {
delete[] elements;
elements = NULL;
size=0;
};
private:
int* elements;
int size;
int capacity;
};
bool Set::contains(int n) const{
int *ptr=elements;
int index=0;
for (index=0; index <=size; index++) {
if(*(ptr+index) == n);
return true;
}
return false;
}
void Set::operator=(const Set& set){
delete[] elements;
elements = NULL;
size=0;
this->capacity = set.capacity;
this->size=set.size;
this->elements= new int[capacity];
for(int i=0; i<=this->size;i++) {
*(this->elements+i)=*(elements+i);
}
}
void Set::add(int n) {
if(!this->contains(n)) {
size++;
*(elements+size)=n;
}
}
#endif
int main() {
Set cc(100);
cc.add(50);
cc.add(17);
Set cSet(cc);
Set mm(10);
mm.add(20);
mm=cSet;
cout<<"End of main";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.