Convert this header file to use templates. // FILE: set.h // CLASS PROVIDED: set
ID: 3726660 • Letter: C
Question
Convert this header file to use templates.
// FILE: set.h
// CLASS PROVIDED: set (part of the namespace main_savitch_5)
//
// TYPEDEFS for the set class:
// set::value_type
// is the data type of the items in the set. It may be any
// of the C++ built-in types (int, char, etc.), or a class with a default
// constructor, a copy constructor, an assignment
// operator, and a test for equality (x == y).
//
// set::size_type
// is the data type of any variable that keeps track of how many items are
// in a set
//
// CONSTRUCTOR for the set class:
// set( )
// Postcondition: The set is empty.
//
// MODIFICATION MEMBER FUNCTIONS for the set class:
//
// bool erase(const value_type& target)
// Postcondition: If target was in the set, then one copy of target has
// been removed from the set; otherwise the set is unchanged. A true
// return value indicates that one copy was removed; false indicates that
// nothing was removed.
//
// bool insert(const value_type& entry)
// Postcondition: The entry is inserted if not already present in the set.
// A true is return if insertion successful otherwise false.
//
// void operator +=(const set& addend)
// Postcondition: Each item in addend , if not already present has been added to this set.
//
// CONSTANT MEMBER FUNCTIONS for the set class:
// size_type size( ) const
// Postcondition: Return value is the total number of items in the set.
//
// size_type count(const value_type& target) const
// Postcondition: Return value is number of times target is in the set.
//
//
// value_type grab( ) const
// Precondition: size( ) > 0.
// Postcondition: The return value is a randomly selected item from the set.
//
// NONMEMBER FUNCTIONS for the set class:
// set operator +(const set& b1, const set& b2)
// Postcondition: The set returned is the union of b1 and b2.
//
// set operator -(const set& b1, const set& b2)
// Postcondition: The set returned is the complement i.e. b1- b2 (all elements in b1 that are not in b2)
// VALUE SEMANTICS for the set class:
// Assignments and the copy constructor may be used with set objects.
//
// DYNAMIC MEMORY USAGE by the set:
// If there is insufficient dynamic memory, then the following functions throw
// bad_alloc: The constructors, insert, operator +=, operator +, and the
// assignment operator.
#ifndef MAIN_SAVITCH_set_H
#define MAIN_SAVITCH_set_H
#include <cstdlib> // Provides size_t and NULL
#include "node1.h" // Provides node class
namespace main_savitch_5
{
class set
{
public:
// TYPEDEFS
typedef std::size_t size_type;
typedef node::value_type value_type;
// CONSTRUCTORS and DESTRUCTOR
set( );
set(const set& source);
~set( );
// MODIFICATION MEMBER FUNCTIONS
bool erase(const value_type& target);
bool insert(const value_type& entry);
set intersect(const set& set2);
void operator +=(const set& addend);
void operator -=(const set& set2);
void operator =(const set& source);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const { return many_nodes; }
bool isPresent(const value_type& target) const;
value_type grab( ) const;
void display() ;
private:
node *head_ptr; // List head pointer
size_type many_nodes; // Number of nodes on the list
};
// NONMEMBER FUNCTIONS for the set class:
set operator +(const set& b1, const set& b2);
set operator -(const set& b1, const set& b2);
}
#endif
Explanation / Answer
//set.h
#ifndef MAIN_SAVITCH_set_H
#define MAIN_SAVITCH_set_H
#include <cstdlib> // Provides size_t and NULL
#include "node1.h" // Provides node class
namespace main_savitch_5
{
//template<class T>
class set
{
public:
// TYPEDEFS
typedef std::size_t size_type;
typedef node::value_type value_type;
// CONSTRUCTORS and DESTRUCTOR
set( );
set(const set& source);
~set( );
// MODIFICATION MEMBER FUNCTIONS
bool erase(const T& target);
bool insert(const T& entry);
set intersect(const set<T>& set2);
void operator +=(const set<T>& addend);
void operator -=(const set<T>& set2);
void operator =(const set<T>& source);
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const { return many_nodes; }
bool isPresent(const T& target) const;
T grab( ) const;
void display() ;
private:
node *head_ptr; // List head pointer
size_type many_nodes; // Number of nodes on the list
};
// NONMEMBER FUNCTIONS for the set class:
set<T> operator +(const set& b1, const set& b2);
set<T> operator -(const set& b1, const set& b2);
}
#endif
//Template class source file
//set.cpp
#include "set.h"
namespace main_savitch_5
{
set::size_type len;
set::value_type v;
template<class T>
set::set()
{
len=0;
}
template<class T>
set::set(const set& source)
{
v=source.v;
len++;
}
template<class T>
set::~set()
{
//delete v;
}
template<class T>
bool set<T>::erase(const T& target)
{
if(target==v)
{
len--;
return 1;
}
else
return 0;
}
template<class T>
bool set<T>::insert(const T& target)
{
if(target!=v)
{
len++;
return 1;
}
else
return 0;
}
template<class T>
set set<T>::intersect(const set& set2)
{
set t;
if(v==set2.v)
t.v=v;
return t;
}
template<class T>
void set<T>::operator +=(const set& addend)
{
if (v!=addend.v)
{
v+=addend.v;
}
}
template<class T>
void set<T>::operator -=(const set& set2)
{
if (v!=addend.v)
{
v-=addend.v;
}
}
template<class T>
void set<T>::operator =(const set& source)
{
if (v!=addend.v)
{
v-=addend.v;
}
}
// CONSTANT MEMBER FUNCTIONS
template<class T>
size_type set<T>::size( ) const
{
return len;
}
template<class T>
bool set<T>::isPresent(const T& target) const
{
if(target==v)
return 1;
else
return 0;
}
template<class T>
T set<T<::grab( ) const
{
if(len>0)
return v;
}
template<class T>
void set<T>::display()
{
cout<<v;
}
template<class T>
friend set<T> operator +(const set& b1, const set& b2)
{
set t;
t.v=b1.v+b2.v;
return t;
}
template<class T>
set<T> operator -(const set& b1, const set& b2)
{
set t=b1.v-b2.v;
return t;
}
#endif