Write a template class which represent a mathematical set

Assignment Help Computer Engineering
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.

Reference no: EM132214026

Questions Cloud

Write and test a c implementation of the mobius function : Write and test a C implementation of the Mobius function M(n) defined as follows.
Determine your clients net capital gain or net capital loss : HI6028 Taxation Theory, Practice & Law - Advise Rapid-Heat of its FBT consequences arising out of the above information, including calculation of any FBT
Write the assembly language for the subroutine : Write the assembly language for the subroutine (called by a main program) and simulate using the sample data table: 24, 65, F0.
Write a template function that receives a contact vector : Write a main function to test this function. In the main function create three different vectors of the types, int, double and char.
Write a template class which represent a mathematical set : Write a template class called ValSet, which represents a mathematical set of values.
Determine the federal income tax for 2018 for the deans : Determine the Federal income tax for 2018 for the Deans on a joint return by providing the following information that would appear on Form 1040.
Explain the philosophy of working in partnership : Working in Partnership in Health & Social Care - develop an understanding of the importance of working positively in partnership with others in health
Define costs and in the context of an insurance company : Define cost objects. Give four examples of cost objects from the case above. Explain why managers might be interested in knowing the costs?
What were the key ethical issues : What were the key ethical issues that the accountants/ management team faced and how to you think they should have responded as the fraud unfolded?

Reviews

Write a Review

Computer Engineering Questions & Answers

  How your organization should house its backups

Your organization has approximately 10TB of data, and you need to decide if your organization should have on-site or offsite tape storage.

  Display the largest and smallest numbers entered

After all the numbers have been entered, the program should display the largest and smallest numbers entered.

  Write a program that tests your macro

Write a procedure named Str_concat that concatenates a source string to the end of a target string.

  Draw diagonal lines on picture from top right to bottom left

Draw horizontal and vertical lines on a picture with 10 pixels between the lines. Draw diagonal lines on a picture from top right to bottom left using add Line.

  Create application that uses solid object-oriented principle

Create an application that uses solid object-oriented principles to simulate one retirement party and an accompanying implementation class that will instantiate one instance of the data definition class.

  What are the advantages of thunderbolt

What are the advantages of Thunderbolt? When might one use SCSI to interconnect a peripheral? What is the difference between SCSI and iSCSI?

  Commercial ids systems

Utilizing the Internet, search for the commercial IDS systems. What are the classification systems and descriptions are used.

  Describe about quantum computing

Quantum computing is the latest technological concept in information processing. Your imaginings and musings about where technology may take us after quantum-based computing

  Write a program to play connect four

Write a program to play Connect Four. Connect Four is a two-player connection game in which the players first choose a color and then take turns dropping colored discs from the top into a seven-column.

  Draw the abstract syntax tree and label the nodes

Derive the constraint set and the test set T_bor-mi for the following predicate pr: ab[c + !cd] where a, b, c, d are Boolean variables.

  Write a program to handle student data in an input file

If there is a student last name on a line, you can assume that there will be a complete line of data as described

  Wap to implement the motion of a bouncing ball using dynamic

Write a program to implement the motion of a bouncing ball using dynamics. The motion of the ball is to be governed by a downward gravitational force.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd