Question

c++ Please create Set.cpp and Set.h The following information is needed (setinterface.h) Class Set You are...

c++

Please create Set.cpp and Set.h

The following information is needed (setinterface.h)

Class Set

You are given an interface: SetInterfac
This is a public interface and completely specifies what the Set class operations must be.
SetInterface is an abstract class (it has no implementation), so your Set class must inherit from SetInterface and implement all of its methods.
Set differs in the fact that it does not allow duplicates. This also means that add()must check that an element is not in Set already if it is the item will not be added.

Here is the SetInterface:
__________________________________________________________________________________

#ifndef SET_INTERFACE_H_

#define SET_INTERFACE_H_

#include <vector>

template<class ItemType>

class SetInterface

{

public:

   /** Gets the current number of entries in this set.

  @return The integer number of entries currently in the set. */

   virtual int getCurrentSize() const = 0;

   /** Checks whether this set is empty.

  @return True if the set is empty, or false if not. */

   virtual bool isEmpty() const = 0;

   /** Adds a new entry to this set.

  @post  If successful, newEntry is stored in the set and

     the count of items in the set has increased by 1.

  @param newEntry  The object to be added as a new entry.

  @return  True if addition was successful, or false if not. */

   virtual bool add(const ItemType& newEntry) = 0;

   /** Removes a given entry from this set,if possible.

  @post  If successful, anEntry has been removed from the set

     and the count of items in the set has decreased by 1.

  @param anEntry  The entry to be removed.

  @return  True if removal was successful, or false if not. */

   virtual bool remove(const ItemType& anEntry) = 0;

   /** Removes all entries from this set.

  @post  set contains no items, and the count of items is 0. */

   virtual void clear() = 0;

   /** Tests whether this set contains a given entry.

  @param anEntry  The entry to locate.

  @return  True if set contains anEntry, or false otherwise. */

   virtual bool contains(const ItemType& anEntry) const = 0;

   /** Fills a vector with all entries that are in this set.

  @return  A vector containing all the entries in the set. */

   virtual std::vector<ItemType> toVector() const = 0;

}; // end SetfInterface

#endif /* SET_INTERFACE_H_ */

______________________________________________________________________________________________________

Other than the methods specified by the SetInterface, your Set class, similarly to the Bag class, will also have some private data members:

static const int DEFAULT_SET_SIZE = 4; // for testing purposes we will keep the set small

ItemType items_[DEFAULT_SET_SIZE]; // array of set items

int item_count_;   // current count of set items

int max_items_;     // max capacity of the set

Set also has a private member function (a helper function) other than the public methods specified by SetInterface.h
It is used by some of its' public methods to find out where a particular item is

// post: Either return the index of target in the array items_

// or -1 if the array does not contain the target

int getIndexOf(const ItemType& target) const;

Remember, Set is an Abstract Data Type! Since you are doing the work, you want to make it as general as possible... you may need to implement film streaming software tomorrow if something goes wrong with your television set
So Set is templated and can store an arbitrary ItemType.

So please write code for two files Set.cpp (implementation) and Set.h(interface)

If you would write a main function to show that it works. That would be great. But please following the directions for the set class.

Thank you so much.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

set.h

//File set.h

#ifndef SET_H_

#define SET_H_

template<class ItemType>

class Set: public SetInterface{

private:

int item_count_;

static const int DEFAULT_SET_SIZE = 4;

ItemType items_[DEFAULT_SET_SIZE];

int max_items_;

int getIndexOf(const ItemType& target) const;

void copyItems(ItemType* target);

public:

Set(){

max_items_ = DEFAULT_SET_SIZE;

item_count_ = 0;

}

~Set(){

}

virtual int getCurrentSize() const {

return item_count_;

}

virtual bool isEmpty() const {

return !item_count_;

}

virtual bool add(const ItemType& newEntry);

virtual bool remove(const ItemType& anEntry);

virtual void clear();

virtual bool contains(const ItemType& anEntry) const;

virtual std::vector<ItemType> toVector() const;

}

#endif

\huge {\color{Purple} set.cpp}

//File set.cpp

int Set::getIndexOf(const ItemType& target) const{

int count = 1;

for(auto& x: items_){

if(x == target)

return count;

}

return -1;

}

void Set::copyItems(ItemType* target){

for(int i = 0; i< item_count_; i++)

{

target[i] = items_[i];

}

}

bool Set::add(const ItemType& newEntry){

if(max_items_ != item_count_){

items_[item_count_] = newEntry

}

else{

max_items_*=2;

ItemType * array = new ItemType[max_items_];

copyItems(array);

items_ = array;

items_[item_count_] = newEntry;

item_count_++;

}

}

bool Set::remove(const ItemType& anEntry){

int index = getIndexOf(anEntry);

if(index == -1){

return true

}

else{

for(int i = index; i < item_count_; i++)

{

items_[i] == items_[i+1];

}

items_[item_count_ -1] = Null;

item_count_--;

}

}

void Set::clear(){

for(int i = 0; i< item_count_; i++)

{

items_[i] == Null;

}

item_count_ = 0;

}

bool Set::contains(const ItemType& anEntry) const{

if(getIndexOf(anEntry) == -1)

false

else

true

}

std::vector<ItemType> toVector() const{

}

Add a comment
Know the answer?
Add Answer to:
c++ Please create Set.cpp and Set.h The following information is needed (setinterface.h) Class Set You are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Define a class ArraySet using an array that represents a set and implements the ADT Set....

    Define a class ArraySet using an array that represents a set and implements the ADT Set. Make the ArraySet resizeable. Then write a C++ program that adequately demonstrates your implementation. Hi, I wrote the program but I don"t know why it showing the output - 000 000 0 0 My main.cpp file code is this - #include<stdio.h> #include<iostream> #include<fstream> #include "ArraySet.h" #include "ArraySet.cpp" using namespace std; int main(){ ArraySet<int> setA, setB, setC; // adds to setA setA.add(10); setA.add(20); setA.add(30); //...

  • 5. Below i s the class deciaration for the Bag class from your text. Refer to...

    5. Below i s the class deciaration for the Bag class from your text. Refer to this hea he Ted implementation @file Bag.h #ifndet BAG #define BAG template <class ItemType> class Bag private: static const int DEFAULT BAG SIZE 6; Il current count of Bag items /I max capacity of the Bag ItemType items[DEFAULT BAG SIZE]; //array of Bag items int itemCount; int maxitems; /l Returns either the index of the element in the array items that ll contains the...

  • QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

    QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public:    /** Sees whether this stack is empty.    @return True if the...

  • Stack help. I need help with my lab assignment. Complete a method for a class named...

    Stack help. I need help with my lab assignment. Complete a method for a class named Palindrome that evaluates a string phrase to determine if the phrase is a palindrome or not. A palindrome is a sequence of characters that reads the same both forward and backward. When comparing the phrase to the same phrase with the characters in reverse order, an uppercase character is considered equivalent to the same character in lowercase, and spaces and punctuation are ignored. The...

  • Can anyone help me fix this program to make sure use stack and use STL library...

    Can anyone help me fix this program to make sure use stack and use STL library to check the name1 and name2 is palindrome. Thank you stackPalindrome.h #ifndef_STACK_PALINDROME_H #define_STACK_PALINDROME_H template <class StackPalindrome> class StackPalindrome { public:    virtual bool isEmpty() const = 0;    virtual bool push(const ItemType& newEntry) = 0;    virtual bool pop() = 0;    virtual ItemType peek() const = 0; }; #endif stack.cpp #include<iostream> #include<string> #include<new> #include "stackPalindrome.h" using namespace std; template <class ItemType> class OurStack...

  • C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS...

    C++ Error. I'm having trouble with a pointer. I keep getting the error "Thread 1: EXC_BAD_ACCESS (code=1, address=0x18)" The objective of the assignment is to revise the public method add in class template LinkedBag so that the new node is inserted at the end of the linked chain instead of at the beginning. This is the code I have: linkedBag-driver.cpp BagInterface.hpp LinkedBag.hpp Node.hpp int main) LinkedBag<string> bag; cout << "Testing array-based Set:" << endl; cout << "The initial bag is...

  • / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false,...

    / Animal.hpp #ifndef ANIMAL_H_ #define ANIMAL_H_ #include <string> class Animal { public: Animal(); Animal(std::string, bool domestic=false, bool predator=false); std::string getName() const; bool isDomestic() const; bool isPredator() const; void setName(std::string); void setDomestic(); void setPredator(); protected: // protected so that derived class can directly access them std::string name_; bool domestic_; bool predator_; }; #endif /* ANIMAL_H_ */ //end of Animal.h // /////////////////////////////////////////////////////////////////Animal.cpp #include "Animal.h" Animal::Animal(): name_(""),domestic_(false), predator_(false){ } Animal::Animal(std::string name, bool domestic, bool predator): name_(name),domestic_(domestic), predator_(predator) { } std::string Animal::getName() const{ return...

  • Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the...

    Java Write an intersection method for the ResizableArrayBag class. The intersection of two bags is the overlapping content of the bags. Intersections are explained in more detail in Chapter 1, #6. An intersecion might contain duplicates. The method should not alter either bag. The current bag and the bag sent in as a parameter should be the same when the method ends. The method header is: public BagInterface<T> intersection(ResizableArrayBag <T> anotherBag) Example: bag1 contains (1, 2, 2, 3) bag2 contains...

  • This is a c++ class utilizing class templates and linked lists. I need to implement the...

    This is a c++ class utilizing class templates and linked lists. I need to implement the following member function(s) to List.cpp. Node.hpp/cpp should be fine but if you feel like there needs to be a change for compilation or testing, feel free to do so but make sure to comment on why it was done. /** @pre assumes position is valid, if position is > item_count_ it returns an empty List, also assumes that operators <= and >= are defined...

  • Load to the IDEA the remaining classes from the provided Lab02.zip file. Repeat the previous project...

    Load to the IDEA the remaining classes from the provided Lab02.zip file. Repeat the previous project inside the ArraySetWithArray class. As shown in the UML diagram below ArraySetWithArray class does not utilize ResizableArrayBag object as its instance variable, it has setOfEntries defined as an array which should be dynamically resized if more room needed (double the size). displaySet method should check if the set is empty and display appropriate message; if the set is not empty should display the number...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT