Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I need help designing a class that works with some test code that I have bee

ID: 3632312 • Letter: H

Question

Hi, I need help designing a class that works with some test code that I have been provided. The class should be a proper type, to include default and 1-argument constructor, copy constructor, assignment operator, and destructor. The constructor argument sets the maximum size of unsigned integers that can belong to the set. Default maximum element size is 64. The following functions, properties and global operators are to be implemented in the .cpp file as long as declared in a separate .h file. Below this I have provided the test.cpp and bitvector.cpp files that my final code is supposed to compile work with. I pretty much completed the class header file except I am not sure how to set the default maximum element size to 64. I will be working on the .cpp file myself as well but any help would be appreciated very much. PM me if you require any more information, thanks in advance.

Services :
void Insert ( unsigned long n ) // inserts n into set
void Remove ( unsigned long n ) // removes n from set
void Clear () // makes set empty
bool Member ( unsigned long n ) const // returns true iff n is in set

bool Empty () const; // true iff set is empty
size_t Size () const; // returns number of elements in set
size_t Range () const; // returns upper bound of range/universe [0,ub)

UIntSet& operator = (const UIntSet& s); // set = s (assignment operator)
UIntSet& operator += (const UIntSet& s); // set = set union s
UIntSet& operator *= (const UIntSet& s); // set = set intersection s
UIntSet& operator -= (const UIntSet& s); // set = set difference s

Properties :
Constructable: objects can be declared as ordinary variables, max size may be specified
Assignable: objects can be assigned one to another
Passable: objects can be passed by value to and returned as values from functions

Private variables:
size_t size_; // the size of the current set
fsu::BitVector bv_; // bit vector representing set

Global operators:
UIntSet operator + (const UIntSet& s1, const UIntSet& s2); // returns s1 union s2
UIntSet operator * (const UIntSet& s1, const UIntSet& s2); // returns s1 intersection s2
UIntSet operator - (const UIntSet& s1, const UIntSet& s2); // returns s1 difference s2
std::ostream& operator << (std::ostream& os, const UIntSet& s); // output operator

/**
test.cpp

test harness for UIntSet
*/

#include <cstdlib>
#include <iostream>
#include <uintset.h>
// #include <uintset.cpp> // in lieu of makefile
// #include <bitvect.cpp> // in lieu of makefile

void ShowSetInfo (const UIntSet& s)
{
std::cout << " Range() = " << s.Range()
<< ", Size() = " << s.Size()
<< ", Empty() = " << (s.Empty() ? "true" : "false") << ' ';
}

int main()
{
UIntSet s1(21), s2(21), s3(21);
std::cout << " sets after declaration: ";
std::cout << " s1 = " << s1 << ' '; ShowSetInfo(s1);
std::cout << " s2 = " << s2 << ' '; ShowSetInfo(s2);
std::cout << " s3 = " << s3 << ' '; ShowSetInfo(s3);

for (size_t i = 0; i < 21; ++i)
s1.Insert(i);
for (size_t i = 0; i < 21; i+=2)
s2.Insert(i);
for (size_t i = 0; i < 21; i+=3)
s3.Insert(i);

std::cout << " sets after insertions: ";
std::cout << " s1 = " << s1 << ' '; ShowSetInfo(s1);
std::cout << " s2 = " << s2 << ' '; ShowSetInfo(s2);
std::cout << " s3 = " << s3 << ' '; ShowSetInfo(s3);
s1 -= s3;
std::cout << " after s1 -= s3: ";
std::cout << " s1 = " << s1 << ' '; ShowSetInfo(s1);
std::cout << " s3 = " << s3 << ' '; ShowSetInfo(s3);

std::cout << " set operations: ";
std::cout << " s2 + s3 = " << s2 + s3 << ' ';
std::cout << " s2 * s3 = " << s2 * s3 << ' ';
std::cout << " s2 - s3 = " << s2 - s3 << ' ';

std::cout << " assignments: ";
s1 = s2 + s3; std::cout << " s1 = s2 + s3: " << s1 << ' '; ShowSetInfo(s1);
s1 = s2 * s3; std::cout << " s1 = s2 * s3: " << s1 << ' '; ShowSetInfo(s1);
s1 = s2 - s3; std::cout << " s1 = s2 - s3: " << s1 << ' '; ShowSetInfo(s1);

for (size_t i = 0; i < 9; ++i)
{
s1.Remove(i);
s2.Remove(i);
s3.Remove(i);
}
std::cout << " after removing 0 - 8: ";
std::cout << " s1 = " << s1 << ' '; ShowSetInfo(s1);
std::cout << " s2 = " << s2 << ' '; ShowSetInfo(s2);
std::cout << " s3 = " << s3 << ' '; ShowSetInfo(s3);

s1.Clear(); s2.Clear(); s3.Clear();
std::cout << " after Clear(): ";
std::cout << " s1 = " << s1 << ' '; ShowSetInfo(s1);
std::cout << " s2 = " << s2 << ' '; ShowSetInfo(s2);
std::cout << " s3 = " << s3 << ' '; ShowSetInfo(s3);
}

/*
bitvect.cpp
08/10//11

BitVector class implementation - array of unsigned char version

*/

#ifndef _BITVECT_CCP
#define _BITVECT_CCP

#include <iostream>
#include <bitvect.h>

namespace fsu
{

std::ostream& operator << (std::ostream& os, const BitVector& bv)
{
for (size_t i = 0; i < bv.Size(); ++i)
os << bv.Test(i);
return os;
}

//----------------------------------
// BitVector
//----------------------------------

// public methods

BitVector::BitVector (size_t numbits) // constructor
{
byteArraySize_ = (numbits + 7)/8;
byteArray_ = new unsigned char [byteArraySize_];
if (byteArray_ == 0)
{
std::cerr << "** BitVector memory allocation failure -- terminating program. ";
exit (EXIT_FAILURE);
}
for (size_t i = 0; i < byteArraySize_; ++i)
byteArray_[i] = 0x00;
}

BitVector::BitVector (const BitVector& bv) // copy constructor
{
byteArraySize_ = bv.byteArraySize_;
byteArray_ = new unsigned char [byteArraySize_];
if (byteArray_ == 0)
{
std::cerr << "** BitVector memory allocation failure -- terminating program. ";
exit (EXIT_FAILURE);
}
for (size_t i = 0; i < byteArraySize_; ++i)
byteArray_[i] = bv.byteArray_[i];
}

BitVector::~BitVector () // destructor
{
delete [] byteArray_;
}

BitVector& BitVector::operator = (const BitVector& bv)
// assignment operator
{
if (this != &bv)
{
if (byteArraySize_ != bv.byteArraySize_)
{
delete [] byteArray_;
byteArraySize_ = bv.byteArraySize_;
byteArray_ = new unsigned char [byteArraySize_];
if (byteArray_ == 0)
{
std::cerr << "** BitVector memory allocation failure -- terminating program. ";
exit (EXIT_FAILURE);
}
}
for (size_t i = 0; i < byteArraySize_; ++i)
byteArray_[i] = bv.byteArray_[i];
byteArray_[i] = bv.byteArray_[i];
}
return *this;
}

size_t BitVector::Size() const
// return size of bitvector
{
return 8 * byteArraySize_;
}

void BitVector::Set ()
// make all bits = 1
{
for (size_t i = 0; i < byteArraySize_; ++i)
byteArray_[i] = 0xFF;
}

void BitVector::Set (size_t index)
// make bit = 1: OR with mask
{
byteArray_[ByteNumber(index)] |= Mask(index);
}

void BitVector::Unset ()
// make all bits = 0
{
for (size_t i = 0; i < byteArraySize_; ++i)
byteArray_[i] = 0x00;
}
void BitVector::Unset (size_t index)
// make bit = 0: AND with inverted mask
{
byteArray_[ByteNumber(index)] &= ~ Mask(index);
}

void BitVector::Flip ()
// change all bit values
{
for (size_t i = 0; i < byteArraySize_; ++i)
byteArray_[i] ^= 0xFF;
}

void BitVector::Flip (size_t index)
// change bit value: XOR with mask
{
byteArray_[ByteNumber(index)] ^= Mask(index);
}

bool BitVector::Test (size_t index) const
// return bit value
{
return 0 != (byteArray_[ByteNumber(index)] & Mask(index));
}

// private methods

size_t BitVector::ByteNumber (size_t index) const
{
// return index / 8
// shift right 3 is equivalent to, and faster than, dividing by 8
index = index >> 3;
if (index >= byteArraySize_)
{
std::cerr << "** BitVector error: index out of range ";
exit (EXIT_FAILURE);
}
return index;
}

unsigned char BitVector::Mask (size_t index)
{
// return mask for index % 8
// the low order 3 bits is the remainder when dividing by 8
size_t shiftamount = index & (size_t)0x07; // low order 3 bits
return (unsigned char)0x01 << shiftamount;
}

} // namespace fsu

#endif

Explanation / Answer

#ifndef __J2K__BitVector_CPP__#define __J2K__BitVector_CPP__#include // A byte with the high bit set:const unsigned char highbit = 1
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote