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

A bag can contain more than one copy of an item. For example, the chapter descri

ID: 3848517 • Letter: A

Question

A bag can contain more than one copy of an item. For example, the chapter describes a bag that contains the number 4 and two cop ies of the number 8. This bag behavior is different from a set, which can contain only a single copy of any given item. Write a new container class called set, which is similar to a bag, except that a set can contain only one copy of any given item. You'll need to change the interface a bit. For example, in- stead of the bag's count function, you'll want a con- stant member function such as this: bool set contains (const value type& target) const; Postcondition: The return value is true if target is in the set, otherwise the return value is false. Make an explicit statement ofthe invariant of the set class. Do a time analysis for each operation. At this

Explanation / Answer

Hi, Please find my sample implementation of Set class.

Please let me know in case of any issue.

// File: Set.h

#ifndef SET_H
#define SET_H

// Specification of the class
template <class value_type>
class Set
{
private:
value_type arr[50];
int count;

public:
Set(); // Constructor
void add(const value_type &i); // Add number to a set
bool contains(const value_type &i) const; // Checks if a number belongs to a set
};


#endif

#########

#include "Set.h"

template <class value_type>
Set<value_type>::Set(){ // Constructor
count = 0;
};

template <class value_type>
void Set<value_type>::add(const value_type &i) // Add integer i to a set
{
if (!contains(i) && count < 50){
   arr[count] = i;
   count++;
}
}

bool Set::contains(const value_type &target) const // Checks if integer i belongs to a set. Returns true if yes
{
   for(int i=0; i<count; i++)
       if(arr[i] == target)
           return true;
   return false;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote