Reference no: EM132214026
Question :
Write a template class called ValSet, which represents a mathematical set of values. For convenience I will refer to the generic type of these values as "T". The ValSet will store the values in an array, which will need to grow if it gets full. ValSet should have the following data members:
a pointer-to-T that points to a dynamically allocated array (of T) that holds the values that are part of the set
an int that holds the current size of the array - it will need to be updated whenever the add() method creates a larger array
an int that holds the number of values that are currently part of the set - it will need to be updated in the add() and remove() methods
The ValSet class should have the following methods:
a default constructor that initializes the pointer data member to point to an array of size 10, initializes the variable that stores the size of the array to 10, and initializes the variable that stores the number of values in the set to zero
a copy constructor that initializes the pointer data member to point to an array of the same size as the one being copied, copies over the array values, and also copies the values for the size of the array and the number of values in the set
an overloaded assignment operator that initializes the pointer data member to point to an array of the same size as the one being copied, copies over the array values, copies the values for the size of the array and the number of values in the set, and returns a reference to the object pointed to by the this pointer
a destructor that deallocates the dynamically allocated array
the size() method should return the number of values currently in the ValSet
the isEmpty() method should return true if the ValSet contains no values, and return false otherwise
the contains() method should take a parameter of type T and return true if that value is in the ValSet, and return false otherwise
the add() method should take a parameter of type T and add that value to the ValSet (if that value is not already in the ValSet) - if the array is currently full and you need to add another value, then you must first increase the size of the array by allocating a new array that is twice as large, copying the contents of the old array into the new array, redirecting the data member pointer to the new array, and deallocating the old array (avoid memory leaks - order matters)
the remove() method should take a parameter of type T and remove it from the ValSet (if that value is in the ValSet) by shifting over all of the subsequent elements of the array - it's okay if values that are no longer part of the set are still in the array, so long as you do the right bookkeeping
the getAsVector method should return a vector (of type T) that contains all of the values in the ValSet and only those values. Order doesn't matter.
One short example of how the ValSet class could be used:
ValSet<char> mySet; mySet.add('A'); mySet.add('j'); mySet.add('&'); mySet.remove('j'); mySet.add('#'); int size = mySet.size(); ValSet<char> mySet2 = mySet; bool check1 = mySet2.contains('&'); bool check2 = mySet2.contains('j');
The files must be named: ValSet.hpp and ValSet.cpp
At the end of the ValSet.cpp file, you must put the following lines:
template class ValSet<int>; template class ValSet<char>; template class ValSet<std::string>;
ValSet.hpp
#include <iostream>
#include <vector>
#include <string>
#ifndef VALSET_H
#define VALSET_H
using std::cout;
using std::endl;
using std::vector;
using std::string;
template <class T>
class ValSet {
private:
T *array; //pointer to T that points to dynamicall allocated array.
int arraySize; //holds current size of the array
int numOfValues; //holds the number of values that part of the set.
public:
ValSet<T>(); //default constructor
ValSet<T>(const ValSet<T> &ele); //define copy constructor
ValSet<T>&operator=(const ValSet<T> &oldSet); //overloaded assignment operator
~ValSet<T>(); //destructor
int size(); //returns number of values in ValSet
bool isEmpty(); //return true if ValSet contains no values, returns false otherwise.
bool contains(T newT); //returns true if value is in ValSet, return false otherwise.
void add(T newT); //add values
void remove(T newT); //removes values
vector<T> getAsVector(); //return vector of Type T contains all values in ValSet.
void printArray();
};
#endif // !VALSET_H
Valset.cpp
#include "ValSet.hpp"
#include <cstdlib>
template <class T>ValSet<T>::ValSet()
{
array = new T[10];
//set array size
arraySize = 10;
//set elements to zero
numOfValues = 0;
}
template <class T>
ValSet<T>::ValSet(const ValSet<T> &ele)
{
delete[] array;
array = new T[ele.arraySize];
for (int i = 0; i < ele.numOfValues; i++)
{
array[i] = ele.array[i];
}
arraySize = ele.arraySize;
numOfValues = ele.numOfValues;
}
template <class T>
ValSet<T>& ValSet<T>::operator=(const ValSet<T> &oldSet)
{
if (this != &oldSet)
{
delete[] array;
//initialize pointer member to point to array of same size
array = new T[oldSet.arraySize];
for (int i = 0; i < oldSet.numOfValues; i++)
{
array[i] = oldSet.array[i]; //copy array values
}
arraySize = oldSet.arraySize;
numOfValues = oldSet.numOfValues;
}
return *this; //returns reference to object pointed to by this pointer.
}
template <class T>
ValSet<T>::~ValSet()
{
//Deallocate memory
delete[] array;
arraySize = 0;
numOfValues = 0;
}
template <class T>
int ValSet<T>::size()
{
return numOfValues; //return size of array
}
template <class T>bool ValSet<T>::isEmpty()
{
if (numOfValues == 0)
return true;
else
return false; //check if empty
}
template <class T>
bool ValSet<T>::contains(T newT)
{
for (int i = 0; i < numOfValues; i++)
{
if (newT == array[i])
return true;
}
return false; //check to see if that value is in ValSet.
}
template <class T>
void ValSet<T>::add(T newT)
{
if (!contains(newT)) //check if value is in the set
{
if (arraySize == numOfValues)
{
numOfValues = 2 * numOfValues; //double size of array
T *newArray = new T[2 * numOfValues];
for (int i = 0; 1 < arraySize; i++) //copy old array to new array
{
newArray[i] = array[i];
}
array = newArray; //redirect pointer
delete[] newArray; //deallocate old array.
}
array[numOfValues] = newT;
arraySize++;
}
}
template <class T>
void ValSet<T>::remove(T newT)
{
int position; //declare variables
if (!contains(newT))
{
bool found = false;
for (int n = 0; n < numOfValues && !found; n++) //loop to find given value
{
if (array[n] == newT) //if value is found
{
for (int i = n; i < numOfValues - 1; i++) //remove the item by shifting over elements in array
{
array[i] = array[i + 1];
}
found = true;
}
}
numOfValues--;
}
}
template <class T>
vector<T> ValSet<T>::getAsVector()
{
vector<T> valueVector;
//copy values into the vector
for (int i = 0; i < numOfValues; i++)
{
valueVector.push_back(array[i]);
}
//return vector values
return valueVector;
}
template <class T>
void ValSet<T>::printArray()
{
for (int i = 0; i < arraySize; i++)
{
cout << array[i] << " ";
cout << endl;
}
}
template class ValSet<int>; //per instructor
template class ValSet<char>; //per instructor
template class ValSet<std::string>; //per instructor
Test.cpp
#include "ValSet.hpp"
int main() {
ValSet<char> mySet;
mySet.add('A');
mySet.add('j');
mySet.add('&');
cout << "First ValSet:" << endl;
mySet.printArray();
mySet.remove('j');
cout << "Second array after removal of j" << endl;
mySet.printArray();
mySet.add('#');
int size = mySet.size();
ValSet<char> mySet2 = mySet;
cout << "\nAfter adding several values, the ValSet mySet2:" << endl;
bool check1 = mySet2.contains('&');
if (check1)
cout << "ValSet has '&'" << endl;
else
cout << "error" << endl;
bool check2 = mySet2.contains('j');
if (check2)
cout << "ValSet has 'j'" << endl;
else
cout << "error" << endl;
mySet.printArray();
mySet2.printArray();
return 0;
}
File doesn't compile correctly :( I think it relates to my add function not working properly. Help!! Can't implement malloc function.