diff --git a/OOP/12/Optional01/main.cpp b/OOP/12/Optional01/main.cpp index e80b671..943a6f6 100755 --- a/OOP/12/Optional01/main.cpp +++ b/OOP/12/Optional01/main.cpp @@ -53,13 +53,19 @@ int main() { cout << "\n----------\n" << endl; Set aSet; - aSet.show(); + output(aSet); - aSet.add(2); - aSet.add(3); - aSet.show(); + aSet.append(2); + aSet.append(3); + output(aSet); + try { + aSet.append(2); + } + catch(DuplicateError) { + cout << "Duplicate Element!" << endl; + } aSet.remove(2); - aSet.show(); + output(aSet); return 0; } \ No newline at end of file diff --git a/OOP/12/Optional01/setE.h b/OOP/12/Optional01/setE.h index bc49859..2e4e8cd 100644 --- a/OOP/12/Optional01/setE.h +++ b/OOP/12/Optional01/setE.h @@ -1,69 +1,21 @@ +#pragma once + #include "ListExceptions.h" #include "listE.h" -template class Set { -private: - List _list; - +template class Set : public List { public: - Set(); - Set(const E &_newItem); - Set(const E *_newItem, const int count); - // Add an element. - Set &add(const E &_newItem); - // Remove an element. - Set &remove(const E &target); - // Clear all elements. - Set &clear(); - - int size(); - void show(); -}; - -template Set::Set() : _list(){}; - -template Set::Set(const E &_newItem) : _list(_newItem){}; - -template Set::Set(const E *_newItem, const int count) : _list() { - for (int i = 0; i < count; i++) { - try { - this->add(_newItem[i]); - } - catch (DuplicateError) { - continue; + List &append(const E &_newItem) { + if (this->contains(_newItem)) { + throw(DuplicateError()); } + List::append(_newItem); + return *this; } + + List &insert(int index, const E &_newItem) = delete; + + E pop(int index) = delete; + + int index(const E &target) = delete; }; - -template Set &Set::add(const E &_newItem) { - if (_list.contains(_newItem)) { - throw(DuplicateError()); - } - - _list.append(_newItem); - return *this; -}; - -template Set &Set::remove(const E &target) { - _list.remove(target); - return *this; -} - -template Set &Set::clear() { - _list.clear(); - return *this; -} - -template int Set::size() { - return _list.length(); -} - -template void Set::show() { - for (int i = 0; i < _list.length(); i++) { - std::cout << _list[i] << ' '; - } - if (_list.length() == 0) { - std::cout << "(Empty)"; - } - std::cout << std::endl; -} \ No newline at end of file