C++ examples for Class:Operator Overload
Create set class to hold integer
#include <iostream> #include <vector> class IntegerSet { public://w w w . ja v a 2s .com IntegerSet(); IntegerSet(int[], int); // COMMON SET OPERATIONS IntegerSet unionOfSets(IntegerSet&) const; IntegerSet intersectionOfSets(IntegerSet&) const; bool operator==(const IntegerSet&) const; bool operator!=(const IntegerSet&) const; // UTILITY FUNCTIONS void insertElement(int); void deleteElement(int); bool elementExists(int) const; bool isEmpty() const; friend std::ostream& operator<<(std::ostream& out, IntegerSet& is) { return is.printSet(out); } std::ostream& printSet(std::ostream&) const; private: static const int _limit = 101; std::vector<bool> _integerSet; void reset(); }; #include <stdexcept> IntegerSet::IntegerSet() { reset(); } IntegerSet::IntegerSet(int arr[], int size) { reset(); for (int i = 0; i < size; ++i) { if (!elementExists(arr[i])) { insertElement(arr[i]); } } } IntegerSet IntegerSet::unionOfSets(IntegerSet& is) const { std::vector<int> tmp; int count = 0; for (int i = 0; i <= _limit; ++i) { if (is.elementExists(i) || elementExists(i)) { tmp.push_back(i); ++count; } } return IntegerSet(&tmp[0], count); } IntegerSet IntegerSet::intersectionOfSets(IntegerSet& is) const { std::vector<int> tmp; int count = 0; for (int i = 0; i < _limit; ++i) { if (!is.elementExists(i) || !elementExists(i)) { continue; } else { tmp.push_back(i); ++count; } } return IntegerSet(&tmp[0], count); } // COMPARISON OPERATORS bool IntegerSet::operator==(const IntegerSet& is) const { for (int i = 0; i < _limit; ++i) { if (is.elementExists(i) != elementExists(i)) { return false; } } return true; } bool IntegerSet::operator!=(const IntegerSet& is) const { for (int i = 0; i < _limit; ++i) { if (is.elementExists(i) != elementExists(i)) { return true; } } return false; } // print IntegerSet std::ostream& IntegerSet::printSet(std::ostream& out) const { if (isEmpty()) { out << "---"; } else { for (int i = 0; i <= _limit; ++i) { if (elementExists(i)) { out << i << " "; } } } return out; } void IntegerSet::reset() { for (int i = 0; i < _limit; ++i) { _integerSet.push_back(false); } } void IntegerSet::insertElement(int el) { if (!elementExists(el)) { _integerSet[el] = true; } else { throw std::invalid_argument("element already exists in set"); } } // deletes an element void IntegerSet::deleteElement(int el) { if (elementExists(el)) { _integerSet[el] = false; } else { throw std::invalid_argument("element does not exist in set"); } } // checks for existance of element in _integerSet bool IntegerSet::elementExists(int el) const { return _integerSet[el]; } // checks whether _integerSet is empty bool IntegerSet::isEmpty() const { for (int i = 0; i < _limit; ++i) { if (elementExists(i)) { return false; } } return true; } int main(int argc, const char *argv[]) { IntegerSet set1; IntegerSet set2; IntegerSet set3; for (int i = 0; i <= 100; i += 3) { set1.insertElement(i); set3.insertElement(i); } for (int i = 0; i <= 100; i += 2) { set2.insertElement(i); } std::cout << "set1: " << set1; std::cout << "\nset2: " << set2; std::cout << "\nset3: " << set3; std::cout << "\nset1 == set2 : " << ((set1 == set2) ? "true" : "false"); std::cout << "\nset1 == set3 : " << ((set1 == set3) ? "true" : "false"); std::cout << "\nset2 != set3 : " << ((set2 != set3) ? "true" : "false"); set3.deleteElement(6); std::cout << " \nset3: " << set3; IntegerSet set4 = set1.unionOfSets(set2); std::cout << "\n\nunionOfSets (set1 + set2): " << set4; set4 = set1.intersectionOfSets(set2); std::cout << "\n\nintersectionOfSets (set1 + set2): " << set4; IntegerSet set5; std::cout << "\n\nset5: " << set5; std::cout << "\nset5.isEmpty() = " << ((set5.isEmpty()) ? "true" : "false") << std::endl; return 0; }