Use C++ Convert your Set class in Lab #23 to the template-based class. That is, the template-based set must be able to store data of any data type, not just integers. Write the main function to test your class by creating different sets of different data types. main.cpp: #include #include "Set.h" using namespace std; int main() { Set s1; Set s2; s1.insert(1); s1.insert(2); s2.insert(2); s2.insert(3); s2.insert(4); Set result = s1 + s2; result.show(); s1 += s2; s1.show(); s1 = s1; s1.show(); cout << s1.getSize() << endl; cout << s1.getCapacity() << endl; s1.remove(2); s1.show(); s1.remove(5); s1.show(); return 0; } Set.h: #ifndef SET_H #define SET_H #include "Set.h" Set::Set() { size = 0; capacity = 10; items = new int[capacity]; } Set::Set(int cap) { size = 0; capacity = cap; items = new int[capacity]; } Set::Set(const Set &source) { size = source.size; capacity = source.capacity; items = new int[capacity]; for (int i = 0; i < size; i++) { items[i] = source.items[i]; } } Set::~Set() { delete[] items; } void Set::insert(int item) { if (size == capacity) { cout << "Set is full" << endl; } else { bool flag = false; for (int i = 0; i < size; i++) { if (items[i] == item) { flag = true; } } if (flag == true) { cout << "Duplicate data" << endl; } else { items[size] = item; size++; } } } void Set::remove(int item) { bool flag = false; int j; for (int i = 0; i < size; i++) { if (items[i] == item) { flag = true; j = i; } } if (flag == true) { for (int i = j; i < size - 1; i++) { items[i] = items[i + 1]; } size--; } else { cout << "No such data" << endl; } } bool Set::full() { if (size == capacity) { return true; } else { return false; } } bool Set::exist(int item) const { for (int i = 0; i < size; i++) { if (items[i] == item) { return true; } } return false; } void Set::operator+=(const Set &source) { for (int i = 0; i < source.size; i++) { bool flag = false; for (int j = 0; j < size; j++) { if (source.items[i] == items[j]) { flag = true; } } if (flag == false) { items[size] = source.items[i]; size++; } } } void Set::operator=(const Set &source) { if (this == &source) { return; } delete[] items; size = source.size; capacity = source.capacity; items = new int[capacity]; for (int i = 0; i < size; i++) { items[i] = source.items[i]; } } void Set::show() const { for (int i = 0; i < size; i++) { cout << items[i] << " "; } cout << endl; } int Set::getSize() const { return size; } int Set::getCapacity() const { return capacity; } Set operator+(const Set &s1, const Set &s2) { Set result; for (int i = 0; i < s1.size; i++) { result.insert(s1.items[i]); } for (int i = 0; i < s2.size; i++) { result.insert(s2.items[i]); } return result; } #endif

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
icon
Concept explainers
Question
100%

Use C++

Convert your Set class in Lab #23 to the template-based class. That is, the template-based set must be able to store data of any data type, not just integers. Write the main function to test your class by creating different sets of different data types.

main.cpp:

#include <iostream>
#include "Set.h"
using namespace std;
int main() {
Set s1;
Set s2;
s1.insert(1);
s1.insert(2);
s2.insert(2);
s2.insert(3);
s2.insert(4);
Set result = s1 + s2;
result.show();
s1 += s2;
s1.show();
s1 = s1;
s1.show();
cout << s1.getSize() << endl;
cout << s1.getCapacity() << endl;
s1.remove(2);
s1.show();
s1.remove(5);
s1.show();
return 0;
}

Set.h:

#ifndef SET_H
#define SET_H
#include "Set.h"

Set::Set() {
size = 0;
capacity = 10;
items = new int[capacity];
}

Set::Set(int cap)
{
size = 0;
capacity = cap;
items = new int[capacity];
}

Set::Set(const Set &source)
{
size = source.size;
capacity = source.capacity;
items = new int[capacity];
for (int i = 0; i < size; i++)
{
items[i] = source.items[i];
}
}

Set::~Set()
{
delete[] items;
}

void Set::insert(int item)
{
if (size == capacity)
{
cout << "Set is full" << endl;
}
else
{
bool flag = false;
for (int i = 0; i < size; i++)
{
if (items[i] == item)
{
flag = true;
}
}
if (flag == true)
{
cout << "Duplicate data" << endl;
}
else
{
items[size] = item;
size++;
}
}
}

void Set::remove(int item)
{
bool flag = false;
int j;
for (int i = 0; i < size; i++)
{
if (items[i] == item)
{
flag = true;
j = i;
}
}
if (flag == true)
{
for (int i = j; i < size - 1; i++)
{
items[i] = items[i + 1];
}
size--;
}
else
{
cout << "No such data" << endl;
}
}

bool Set::full() {
if (size == capacity)
{
return true;
}
else
{
return false;
}
}

bool Set::exist(int item) const
{
for (int i = 0; i < size; i++)
{
if (items[i] == item)
{
return true;
}
}
return false;
}

void Set::operator+=(const Set &source)
{
for (int i = 0; i < source.size; i++)
{
bool flag = false;
for (int j = 0; j < size; j++)
{
if (source.items[i] == items[j])
{
flag = true;
}
}
if (flag == false)
{
items[size] = source.items[i];
size++;
}
}
}

void Set::operator=(const Set &source)
{
if (this == &source)
{
return;
}
delete[] items;
size = source.size;
capacity = source.capacity;
items = new int[capacity];
for (int i = 0; i < size; i++)
{
items[i] = source.items[i];
}
}

void Set::show() const {
for (int i = 0; i < size; i++)
{
cout << items[i] << " ";
}
cout << endl;
}

int Set::getSize() const
{
return size;
}

int Set::getCapacity() const
{
return capacity;
}

Set operator+(const Set &s1, const Set &s2)
{
Set result;
for (int i = 0; i < s1.size; i++)
{
result.insert(s1.items[i]);
}
for (int i = 0; i < s2.size; i++)
{
result.insert(s2.items[i]);
}
return result;
}

#endif

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 4 images

Blurred answer
Knowledge Booster
Types of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-engineering and related others by exploring similar questions and additional content below.
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY