Question
C++ : For defining a template class, you will put the function definitions in the .hpp file - not inside the class declaration, but after
C++ : For defining a template class, you will put the function definitions in the .hpp file - not inside the class declaration, but after it in the same file, as in the example on pages 1009-1010 (from starting out with c++ with early objs 9e).
**Please note: this question includes 4 REQUIRED overloaded operators. Provide code for EACH**
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: (Please provide code with all methods/operators included)
-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.
-an overloaded + operator that returns a new ValSet that is the union of its two operands (contains all and only those values that were in either ValSet)
-an overloaded * operator that returns a new ValSet that is the intersection of its two operands (contains all and only those values that were in both ValSets)
-an overloaded / operator that returns a new ValSet that is the symmetric difference of its two operands (contains all and only those values that were in one ValSet or the other, but not both)
One short example of how the ValSet class could be used:
ValSet mySet;
mySet.add('A');
mySet.add('j');
mySet.add('&');
mySet.remove('j');
mySet.add('#');
int size = mySet.size();
ValSet mySet2 = mySet;
bool check1 = mySet2.contains('&');
bool check2 = mySet2.contains('j');
ValSet myUnion = mySet + mySet2;
The file must be named: ValSet.hpp
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started