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

I am looking for help declaring and designing a C++ function to add an \"interse

ID: 3743855 • Letter: I

Question

I am looking for help declaring and designing a C++ function to add an "intersection" function to my "myset.h" and "set.cpp" files, that have been defined per instructions from question 3.5 in the textbook (https://www.chegg.com/homework-help/Data-Structures-and-Other-Objects-Using-C--4th-edition-chapter-3-problem-5PP-solution-9780132129480). All of the other functions work perfectly; I'm only in need of a function to handle the intersection of two sets that will work with my code.

Here are my existing code and implementation + test files (copyable versions are listed below the pictures):

myset.h

set.cpp

testset.cpp

COPYABLE CODE FOR MYSET.H:

#ifndef _MY_SET_H

#define _MY_SET_H

class set

{
public:

typedef int value_type;
typedef size_t size_type;
static const size_type CAPACITY = 100;

//Constructor
set() { used = 0; }

//Function to erase setItem
void erase(const value_type& setItem);

//Function to insert NewItem
void insert(const value_type& NewItem);

//overloading +=
void operator +=(const set& addend);

//Function to print elements of the set
void printset();

//Function to return size of the array

size_type size() const { return used; }

//Function to confirm whether or not something is in the set
bool contains(const value_type& setItem) const;

//Friend functions
friend set operator +(const set& set1, const set& set2); // union operator

friend set operator -(const set& set1, const set& set2); // compliment operator

//Access specifier

private:

value_type data[CAPACITY];

size_type used;

};

#endif

COPYABLE CODE FOR SET.CPP:

#include <iostream>

#include <string>
#include "myset.h"

using namespace std;

void set::erase(const value_type& setItem)
{
if (contains(setItem))
{
for (int i = 0; i < used; i++)
{
if (data[i] == setItem)
{
for (int ii = i; ii < used - 1; ii++)
data[ii] = data[ii + 1];
used--;
cout << "Deleted Succcessfully" << endl;
}
}
}
else
cout << "Deletion failed." << endl;
}

void set::insert(const value_type& NewItem)
{
if (size() <= CAPACITY && !contains(NewItem))
{
data[used] = NewItem;
used++;
}
}

void set::operator +=(const set& addend)
{
if (size() + addend.size() <= CAPACITY)
{
for (int i = 0; i < addend.size(); i++)
insert(addend.data[i]);
}
}

bool set::contains(const value_type& setItem) const
{
if (used != 0)
{
for (int i = 0; i < used; i++)
if (data[i] == setItem)
return true;
return false;
}
else
return false;
}

void set::printset()
{
if (used == 0)
cout << "Set empty." << endl;
else
{
cout << "Set contains:" << endl;
for (int i = 0; i < size(); i++)
cout << data[i] << " ";
cout << endl << endl;
}
}

set operator +(const set& set1, const set& set2)
{
set result;
for (int i = 0; i < set1.size(); i++)
result.insert(set1.data[i]);

for (int i = 0; i < set2.size(); i++)
result.insert(set2.data[i]);

return result;
}

set operator -(const set& set1, const set& set2)
{
set result;

for (int i = 0; i < set1.size(); i++)
if (!set2.contains(set1.data[i]))
result.insert(set1.data[i]);
return result;
}

COPYABLE CODE FOR TESTSET.CPP:

#include <iostream>
#include <string>
#include "myset.h"

using namespace std;

int main()
{
//Create sets
set s1, s2, s3;

//Insert elements
s1.insert(1); s1.insert(2); s1.insert(3); s1.insert(4); s1.insert(5);
s1.insert(6); s1.insert(7); s1.insert(8); s1.insert(9); s1.insert(10);

s2.insert(6); s2.insert(7); s2.insert(8); s2.insert(9); s2.insert(10);
s2.insert(11); s2.insert(12); s2.insert(13); s2.insert(14); s2.insert(15);

//Display set 1
cout << "S1:" << endl;

s1.printset();

//Display set2
cout << "S2:" << endl;

s2.printset();

//Print size
cout << "Size of set 1:" << s1.size() << endl;
cout << "Size of set 2:" << s2.size() << endl << endl << endl;

//Add set 1 and 2
cout << "Adding set 1 and set 2 to demonstrate union functionality:" << endl;

s3 = s1 + s2;
//Display addition outcome
cout << "Union of S1 and S2:" << endl;

s3.printset();

// test of the += overload
cout << "Testing the += overload:" << endl;
s3 += s1 + s2;

cout << "S1 += S2 result:" << endl;
s3.printset();

//subtracting set 1 and 2 for compliment functionality
cout << "Subtracting set 1 and set 2 from each other to demonstrate compliment functionality:" << endl;

s3 = s1 - s2;

//Display compliment outcome
cout << "The compliment of set S2:" << endl;

s3.printset();

cout << "The compliment of set S1:" << endl;

s3 = s2 - s1;
s3.printset();

// testing the erase function:
cout << "S3 contains the following items at this point:" << endl;

s3.printset();

cout << "Removing 11 from S3 to test the erase function:" << endl;

s3.erase(11);
s3.printset();

//testing "contains" feature

cout << "Checking for the number 15 in S3 to test the "contains" function:" << endl;

s3.printset();

cout << "number of instances of the number 15 in S3: " << s3.contains(15) << endl;

//Pause window
system("pause");

//Stop
return 0;
}

#ifndef MY SETH #define MY SETH - class set public: typedef int value_type; static const size_type CAPACITY 100; //Constructor typedef size t size type set) used ; //Function to erase setItem : void erase(const value_type& setItem); //Function to insert NewItem void insert(const value_type& NewItem); /overloading + void operator +=(const set& addend); //Function to print elements of the set void printset (); //Function to return size of the array size type size() const return used: /Function to confirm whether or not something is in the set : bool contains (const value_type& setItem) const; //Friend functions friend set operator +(const set& set1, const set& set2) I/ union operator friend set operator -(const set& set1, const set& set2); I/ compliment operator //Access specifier private: value_type data[CAPACITY] size type used; 3; #endif

Explanation / Answer


Given below are the modified files . The output for intersection is shown after union
Please do rate the answer if it was helpful. Thank you


myset.h
--------
#ifndef _MY_SET_H

#define _MY_SET_H

class set

{
public:

typedef int value_type;
typedef size_t size_type;
static const size_type CAPACITY = 100;

//Constructor
set() { used = 0; }

//Function to erase setItem
void erase(const value_type& setItem);

//Function to insert NewItem
void insert(const value_type& NewItem);

//overloading +=
void operator +=(const set& addend);

//Function to print elements of the set
void printset();

//Function to return size of the array

size_type size() const { return used; }

//Function to confirm whether or not something is in the set
bool contains(const value_type& setItem) const;

set intersection(const set& set2);

//Friend functions
friend set operator +(const set& set1, const set& set2); // union operator

friend set operator -(const set& set1, const set& set2); // compliment operator

//Access specifier

private:

value_type data[CAPACITY];

size_type used;

};

#endif


set.cpp
-------
#include <iostream>

#include <string>
#include "myset.h"

using namespace std;

void set::erase(const value_type& setItem)
{
if (contains(setItem))
{
for (int i = 0; i < used; i++)
{
if (data[i] == setItem)
{
for (int ii = i; ii < used - 1; ii++)
data[ii] = data[ii + 1];
used--;
cout << "Deleted Succcessfully" << endl;
}
}
}
else
cout << "Deletion failed." << endl;
}

void set::insert(const value_type& NewItem)
{
if (size() <= CAPACITY && !contains(NewItem))
{
data[used] = NewItem;
used++;
}
}

void set::operator +=(const set& addend)
{
if (size() + addend.size() <= CAPACITY)
{
for (int i = 0; i < addend.size(); i++)
insert(addend.data[i]);
}
}

bool set::contains(const value_type& setItem) const
{
if (used != 0)
{
for (int i = 0; i < used; i++)
if (data[i] == setItem)
return true;
return false;
}
else
return false;
}

void set::printset()
{
if (used == 0)
cout << "Set empty." << endl;
else
{
cout << "Set contains:" << endl;
for (int i = 0; i < size(); i++)
cout << data[i] << " ";
cout << endl << endl;
}
}

set set::intersection(const set& set2)
{
set result;
for(int i = 0; i < size(); i++)
{
if(set2.contains(data[i]))
result.insert(data[i]);
}
return result;
}

set operator +(const set& set1, const set& set2)
{
set result;
for (int i = 0; i < set1.size(); i++)
result.insert(set1.data[i]);

for (int i = 0; i < set2.size(); i++)
result.insert(set2.data[i]);

return result;
}

set operator -(const set& set1, const set& set2)
{
set result;

for (int i = 0; i < set1.size(); i++)
if (!set2.contains(set1.data[i]))
result.insert(set1.data[i]);
return result;
}


testset.cpp
----------
#include <iostream>
#include <string>
#include "myset.h"

using namespace std;

int main()
{
//Create sets
set s1, s2, s3, s4;

//Insert elements
s1.insert(1); s1.insert(2); s1.insert(3); s1.insert(4); s1.insert(5);
s1.insert(6); s1.insert(7); s1.insert(8); s1.insert(9); s1.insert(10);

s2.insert(6); s2.insert(7); s2.insert(8); s2.insert(9); s2.insert(10);
s2.insert(11); s2.insert(12); s2.insert(13); s2.insert(14); s2.insert(15);

//Display set 1
cout << "S1:" << endl;

s1.printset();

//Display set2
cout << "S2:" << endl;

s2.printset();

//Print size
cout << "Size of set 1:" << s1.size() << endl;
cout << "Size of set 2:" << s2.size() << endl << endl << endl;

//Add set 1 and 2
cout << "Adding set 1 and set 2 to demonstrate union functionality:" << endl;

s3 = s1 + s2;
//Display addition outcome
cout << "Union of S1 and S2:" << endl;

s3.printset();

s4 = s1.intersection(s2); //intersection of s1 and s2
cout << "Intersection of S1 and S2:" << endl;
s4.printset();

// test of the += overload
cout << "Testing the += overload:" << endl;
s3 += s1 + s2;

cout << "S1 += S2 result:" << endl;
s3.printset();

//subtracting set 1 and 2 for compliment functionality
cout << "Subtracting set 1 and set 2 from each other to demonstrate compliment functionality:" << endl;

s3 = s1 - s2;

//Display compliment outcome
cout << "The compliment of set S2:" << endl;

s3.printset();

cout << "The compliment of set S1:" << endl;

s3 = s2 - s1;
s3.printset();

// testing the erase function:
cout << "S3 contains the following items at this point:" << endl;

s3.printset();

cout << "Removing 11 from S3 to test the erase function:" << endl;

s3.erase(11);
s3.printset();

//testing "contains" feature

cout << "Checking for the number 15 in S3 to test the "contains" function:" << endl;

s3.printset();

cout << "number of instances of the number 15 in S3: " << s3.contains(15) << endl;

//Pause window
system("pause");

//Stop
return 0;
}


output
------
S1:
Set contains:
1 2 3 4 5 6 7 8 9 10

S2:
Set contains:
6 7 8 9 10 11 12 13 14 15

Size of set 1:10
Size of set 2:10


Adding set 1 and set 2 to demonstrate union functionality:
Union of S1 and S2:
Set contains:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Intersection of S1 and S2:
Set contains:
6 7 8 9 10

Testing the += overload:
S1 += S2 result:
Set contains:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Subtracting set 1 and set 2 from each other to demonstrate compliment functionality:
The compliment of set S2:
Set contains:
1 2 3 4 5

The compliment of set S1:
Set contains:
11 12 13 14 15

S3 contains the following items at this point:
Set contains:
11 12 13 14 15

Removing 11 from S3 to test the erase function:
Deleted Succcessfully
Set contains:
12 13 14 15

Checking for the number 15 in S3 to test the "contains" function:
Set contains:
12 13 14 15

number of instances of the number 15 in S3: 1