I don't know why it is doing this. I passed it as const and in the function I don't make any changes to the viable.
The "intersectionOfSets" function changes the class instance "empty"'s array "set"'s first position from 0 to 5383480.
Please correct me if my vocab is off, thanks for the help!
main.cpp
Code:
#include <iostream>
#include "IntegerSet.h"
using namespace std;
int main()
{
const int arraySize = 10;
int array[10] = {0,1,1,0,1,0,0,1,1,0};
IntegerSet five (10);
IntegerSet empty (10);
IntegerSet Union(10);
IntegerSet intersection(10);
IntegerSet arraySet (array, arraySize);
cout << "Empty Set:\n";
empty.printSet();
five.insertElement(5);
cout << "\n\nEmpty set with 5 added to set:\n";
five.printSet();
cout << "\n\nUnion of empty set and set with 5 in it:\n";
five.unionOfSets(empty,Union);
Union.printSet();
cout << "\n\n***Empty before:\n";
empty.printSet();
cout << "\n\nIntersection of empty set and set with 5 in it\n";
five.intersectionOfSets(empty, intersection); // problem ith empty changing here
intersection.printSet();
cout << "\n\n***Empty after:\n";
empty.printSet();
cout << "\n\nChecks if the set with 5 in it is equal to the empty set: (0 = false, 1 = true)\n";
cout << five.isEqual(empty);
five.deleteElement(5);
cout << "\n\nDeleted element 5:\n";
five.printSet();
cout << "\n\nChecks if the previous set with 5 with 5 just being taken away (empty) is equal to the empty set:(0 = false, 1 = true)\n";
cout << five.isEqual(empty);
cout << "\n\nOutput integer set that is predefined with an array {0,1,1,0,1,0,0,1,1,0}:\n";
arraySet.printSet();
return 0;
}
IntegerSet.cpp
Code:
#include <iostream>
#include "IntegerSet.h"
IntegerSet::IntegerSet(int tempSize)
{
size = tempSize;
set = new int[size];
for(int a = 0; a < size; a++)
set[a] = 0;
}
IntegerSet::IntegerSet(int arraySet[], int tempArraySize)
{
size = tempArraySize;
set = new int[size];
for(int a = 0; a < size; a++)
set[a] = arraySet[a];
}
IntegerSet::~IntegerSet()
{
delete [] set;
}
void IntegerSet::unionOfSets(const IntegerSet tempB, IntegerSet &newSet)
{
if(size == tempB.size)
{
for(int a = 0; a < size; a++)
{
if(set[a] == 1 || tempB.set[a] == 1)
newSet.set[a] = 1;
else
newSet.set[a] = 0;
}
}
else
std::cout << "Error. Sets being compared are not the same length.";
}
void IntegerSet::intersectionOfSets(const IntegerSet tempB, IntegerSet &newSet)
{
if(size == tempB.size)
{
for(int a = 0; a < size; a++)
{
if(set[a] == 1 && tempB.set[a] == 1)
newSet.set[a] = 1;
else
newSet.set[a] = 0;
}
}
else
std::cout << "Error. Sets being compared are not the same length.";
}
void IntegerSet::insertElement(int element)
{
if(element < size && element > -1 )
set[element] = 1;
else
std::cout << "Error. Inserted element is not in scope.\n";
}
void IntegerSet::deleteElement(int element)
{
if( element < size && element > -1 )
set[element] = 0;
else
std::cout << "Error. Inserted element is not in scope.\n";
}
void IntegerSet::printSet() const
{
for(int a = 0; a < size; a++)
{
if(set[a] == 1)
std::cout << a << " ";
else
std::cout << set[a] << " ";
}
}
bool IntegerSet::isEqual(const IntegerSet tempB) const
{
for(int a = 0; a < size; a++)
{
if(set[a] != tempB.set[a])
return false;
}
return true;
}
IntegerSet.h
Code:
class IntegerSet
{
private:
int *set;
int size;
public:
IntegerSet(int);
IntegerSet(int[], int);
~IntegerSet();
void unionOfSets(const IntegerSet tempB, IntegerSet &newSet);
void intersectionOfSets(const IntegerSet tempB, IntegerSet &newSet);
void insertElement(int);
void deleteElement(int);
void printSet() const;
bool isEqual(const IntegerSet tempB) const;
};