Running
Source Code
main.cpp
#include <iostream>
#include <sstream>
using namespace std;
class Set
{
public:
Set();
void add(int n);
bool contains(int n) const;
int get_size() const;
void display();
~Set();
Set(const Set& other_set);
Set& operator=(const Set& other_set);
private:
int* elements;
int size;
};
Set::Set()
{
cout << "Enter # of integers: ";
cin >> size;
elements = new int[size];
for (int x = 0; x < size; x++)
{
cout << "Enter integer " << x << ": ";
cin >> *(elements + x);
}
}
void Set::add(int n)
{
int* copy;
for (int x = 0; x < size; x++)
{
copy[x] = elements[x];
}
elements = new int[size + 1];
for (int x = 0; x < size; x++)
{
elements[x] = copy[x];
}
size++;
elements[size - 1] = n;
}
bool Set::contains(int n)const
{
bool has = false;
for (int x = 0; x < size; x++)
{
if (elements[x] == n) has = true;
}
return has;
}
int Set::get_size() const
{
return size;
}
void Set::display()
{
for (int x = 0; x < size; x++)
{
cout << "\nElement " << x << " = " << elements[x];
}
}
Set::~Set()
{
delete elements;
elements = NULL;
}
Set::Set(const Set& other_set)
{
size = other_set.size;
elements = new int[size];
copy(other_set.elements, other_set.elements + size, elements);
}
Set& Set::operator=(const Set& other_set)
{
if (&other_set != this)
{
size = other_set.size;
elements = new int[size];
copy(other_set.elements, other_set.elements + size, elements);
}
return *this;
}
int main(int argc, char** argv)
{
Set x;
x.display();
x.add(5);
cout << "\n\nNew set with 5 added:\n";
x.display();
cout << "\n\nDoes it have a 3? ";
if (x.contains(3))
{
cout << "yes";
}
else
{
cout << "no";
}
stringstream ss;
ss << x.get_size();
cout << "\n\nSize of array: " << ss.str();
Set y = x;
cout << "\n\nMade Set y = x. This is values of Set y:\n";
y.display();
return 0;
}