Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

please help, need help in C++ _____________________________________________________________________________________ Create a new templated class called SortedCollection that inherits the Collection class. Add the following items to the

please help, need help in C++

_____________________________________________________________________________________

Create a new templated class called SortedCollection<> that inherits the Collection<> class. Add the following items to the SortedCollection class.

Create an additional overload operator for the + operator. This will you practice with overloaded operators. This time we will use it to add and Item to the Collection class (and thus SortedCollection class). When implementing the operator, you are welcome to call the add method that has already been coded.

Create an additional overload operator for the << operator. This time we will use it to add and Item to the Collection class (and thus SortedCollection class). This will NOT be a friend function. Watch out, there will be different parameters than the operater<< used to display the object. When implementing the operator, you are welcome to call the add method that has already been coded.

Overload the three constructors such that you can make a call to SortedCollection using no parameters, a single int parameter to specify the max size, and with a parameter of another SortedCollection to start with a given collection.

Overload the add method such that it adds items in the correct order from lowest to highest. For example, given a collection with the following values 2 5 9, add(3) would produce 2 3 5 9.

__________________________________________________

Collection.h

#ifndef COLLECTION_H #define COLLECTION_H #include #include #include #include

using namespace std; template class Collection;

template ostream& operator<<(ostream&, const Collection& c);

template class Collection { public: Collection(); Collection(int size); Collection(Collection& a); virtual int size(); virtual Item get(int ndx) const; virtual void add(Item e); virtual void removeEnd(); virtual Collection& operator= (Collection& a); virtual Item operator [](int ndx); virtual void operator-(int num); friend ostream& operator << <>(ostream& out, const Collection& c); protected: int capacity; int curSize; static const int INITIAL_CAPACITY = 8; void expand(); unique_ptr elements; }; template Collection::Collection() { capacity = INITIAL_CAPACITY; curSize = 0; elements = make_unique(capacity); } template Collection::Collection(int size) { capacity = size; curSize = 0; } template Collection::Collection(Collection& a) { capacity = a.capacity; curSize = a.curSize; //deep copy of the class elements = make_unique(capacity); for (int i = 0; i < curSize; i++) { elements[i] = a.elements[i]; } }

template int Collection::size() { return curSize; } template Item Collection::get(int ndx) const { return elements[ndx]; }

template void Collection::add(Item e) { if (curSize == capacity) { expand(); } elements[curSize] = e; curSize++; }

template void Collection::removeEnd() { curSize--; if (curSize < 0) { throw runtime_error("Empty List"); } }

template Collection& Collection:: operator=(Collection& a) { auto newElements = make_unique(a.capacity); for (int i = 0; i < a.curSize; i++) { newElements[i] = a.elements[i]; } elements = move(newElements); capacity = a.capacity; curSize = a.curSize;

return *this; }

template Item Collection::operator [](int ndx) { return get(ndx); }

template void Collection::operator-(int num) { for (int i = 0; i < num; i++) { removeEnd(); } }

template void Collection::expand() { auto newElements = make_unique(capacity * 2); for (int i = 0; i < capacity; i++) { newElements[i] = elements[i]; } elements = move(newElements); capacity *= 2; }

template ostream& operator<<(ostream& out, const Collection& c) { for (int i = 0; i < c.curSize; i++) { out << c.elements[i]; if (i < c.curSize - 1) out << " "; } return out; }

#endif

_________________________________________________________

You can test your project out with the following test cases

SortedCollectionTesting.cpp

#include #include "SortedCollection.h"using namespace std;void TestSortedCollection();void TestSortedCollectionConstructors();void TestAddToOperatorCollection();void TestRemoveCollection();bool checkCase(string name, bool condition);int main(){ TestSortedCollection(); TestSortedCollectionConstructors(); TestAddToOperatorCollection(); TestRemoveCollection(); return 0;}void TestSortedCollection(){ SortedCollectionone; one << 19 << 9 << 2 << 8 << 7 << 12 << 17 << 0 << 11 << 6 << 3 << 1; checkCase("In Order 1", one[0] == 0); checkCase("In Order 2", one[11] == 19); checkCase("Check Size", (one << 13).size()== 13);}void TestSortedCollectionConstructors(){ SortedCollectionone(10); for(double i = 0.0; i < 5; i+=.5){ one << i; } SortedCollectiontwo(one); checkCase("Constructor Test", two[0] == 0);}void TestAddToOperatorCollection(){ SortedCollection one; one + 19 + 9 + 2 + 8 + 7 + 12 + 17 + 0 + 11 + 6 + 3 + 1; checkCase("Add In Order 1", one[0] == 0); checkCase("Add In Order 2", one[11] == 19); checkCase("Add Check Size", (one << 13).size()== 13);}void TestRemoveCollection(){ SortedCollection one; one + 19 + 9 + 2 + 8 ; checkCase("Original Size", one.size() == 4); one -2; checkCase("Remove 2 Size", one.size() == 2); checkCase("Still In Order", one[1] == 8); bool exception_caught = true; try{ one - 3; exception_caught = false; } catch (exception& a){ exception_caught = true; }

checkCase("Exception Caught", exception_caught);}bool checkCase(string name, bool condition){ if(!condition){ cout << "Failed: " << name << endl; } else{ cout << name << ": passed" << endl; } return condition;}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

4-8 Products help us to retrieve memories from our past.

Answered: 1 week ago

Question

LOQ 14-4: What developmental stages did Freud propose?

Answered: 1 week ago