首页 > > 详细

C++辅导 Write a class called IntegerSet讲解C/C++


Introduction

MyInteger.hpp MyInteger.cpp
IntegerSet.hpp IntegerSet.cpp
Write a class called IntegerSet, which represents a mathematical
set of integer values. The IntegerSet will store the values in an
array, which will need to grow if it gets full. IntegerSet should
have the following data members:
• an int-pointer that points to a dynamically allocated array (of
int) 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 IntegerSet 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 IntegerSet
• the isEmpty() method should return true if the IntegerSet
contains no values, and return false otherwise
• the contains() method should take an int parameter and
return true if that value is in the IntegerSet, but return false
otherwise
• the add() method should take an int parameter and add that
value to the IntegerSet (if that value is not already in the
IntegerSet) - 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 an int parameter and
remove it from the IntegerSet (if that value is in the
IntegerSet) 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 ints) that
contains all of the values in the IntegerSet and only those
values. Order doesn’t matter.
• an overloaded + operator that returns a new IntegerSet that
is the union of its two operands (contains all and only those
values that were in either IntegerSet)
• an overloaded operator that returns a new IntegerSet that
is the intersection of its two operands (contains all and only
those values that were in both IntegerSets)
• an overloaded / operator that returns a new IntegerSet that
is the symmetric difference of its two operands (contains all
and only those values that were in one IntegerSet or the
other, but not both)
One short example of how the IntegerSet class could be used:
IntegerSet mySet1;
mySet1.add(-30);
mySet1.add(13);
mySet1.add(100);
mySet1.remove(-30);
mySet1.add(44);
int size = mySet1.size();
IntegerSet mySet2 = mySet;
bool check1 = mySet2.contains(13);
bool check2 = mySet2.contains(44);
IntegerSet mySet3 = mySet1 mySet2;
The files must be named: IntegerSet.hpp and IntegerSet.cpp

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!