Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ :Please provide complete code, especially regarding the overloading operators. For defining a template class, you will put the function definitions in the .hpp file

C++ :Please provide complete code, especially regarding the overloading operators.

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 includes4 REQUIREDoverloaded operators. Provide code for EACH**

Write a template class calledValSet, 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)

-adefault constructorthat 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

-acopy constructorthat 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

-anoverloaded assignment operatorthat 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 thethispointer

-adestructorthat deallocates the dynamically allocated array

-thesize()method should return the number of values currently in the ValSet

-theisEmpty()method should return true if the ValSet contains no values, and return false otherwise

-thecontains()method should take a parameter of type T and return true if that value is in the ValSet, and return false otherwise

-theadd() methodshould 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)

-theremove() methodshould 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

-thegetAsVector methodshould return a vector (of type T) that contains all of the values in the ValSet and only those values. Order doesn't matter.

-anoverloaded + operatorthat returns a new ValSet that is the union of its two operands (contains all and only those values that were in either ValSet)

-anoverloaded * operatorthat returns a new ValSet that is the intersection of its two operands (contains all and only those values that were in both ValSets)

-anoverloaded / operatorthat 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Online Systems For Physicians And Medical Professionals How To Use And Access Databases

Authors: Harley Bjelland

1st Edition

1878487442, 9781878487445

More Books

Students also viewed these Databases questions

Question

Question What are the advantages of a written bonus plan?

Answered: 1 week ago