Instructions Create a new templated class called Sorted Collection that inherits the Collection class. Add the...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
Instructions Create a new templated class called Sorted Collection<> that inherits the Collection<> class. Add the following items to the Sorted Collection 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 Sorted Collection 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 Sorted Collection 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 Sorted Collection using no parameters, a single int parameter to specify the max size, and with a parameter of another Sorted Collection 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 59. 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& a); virtual int size(); virtual Item get(int ndx) const; virtual void add(ltem e); virtual void removeEnd(); virtual Collection& operator= (Collection& a); virtual Item operator [](int ndx); virtual void operator-(int num); 1 friend ostream& operator << <>(ostream& out, const Collection& c); protected: int capacity; int curSize; Collection(); Collection(int size); }; template { Collection::Collection() capacity = INITIAL_CAPACITY; curSize = 0; elements = make_unique(capacity); } template { static const int INITIAL_CAPACITY = 8; void expand(); unique_ptr elements; 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 { } } template void Collection::removeEnd() { } == expand(); } elements [curSize] = e; curSize++; } template = Collection & Collection:: operator=(Collection& a) { auto newElements = make_unique(a.capacity); for (int i = 0; i < a.curSize; i++) { new Elements[i] = a.elements[i]; } elements move(new Elements); 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++) { Elements[i] = elements[i]; new } elements move(new Elements); capacity *= 2; capacity) curSize--; if (curSize < 0) { throw runtime_error("Empty List"); = #endif template ostream& operator<<(ostream& Collection& c) { for (int i = 0; i < c.curSize; i++) { out << c.elements[i]; if (i < c.curSize - 1) out << " "; } return out; out, const You can test your project out with the following test cases\ Sorted CollectionTesting.cpp (this is finished.) #include #include "Sorted Collection.h"using namespace std;void TestSorted Collection(); void TestSorted Collection Constructors(); void TestAddToOperatorCollection(); void TestRemoveCollection(); bool checkCase(string name, bool condition); int main(){ TestSorted Collection(); TestSorted Collection Constructors(); TestAddToOperatorCollection(); TestRemoveCollection(); return 0;}void TestSortedCollection(){ Sorted Collectionone; 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 TestSorted Collection Constructors(){ Sorted Collectionone(10); for(double i = 0.0; i < 5; i+=.5){ one << i; } SortedCollectiontwo(one); checkCase("Constructor Test", two[0] == 0);}void TestAddToOperatorCollection(){ Sorted Collection one; one +19+9+2+8+7 + 1; checkCase("Add + 12 + 17 + 0 + 11 + 6 + 3 In Order one[0] 0); checkCase("Add In 1", == Order 2", one[11] == 19); checkCase("Add Check Size", (one << 13).size()== 13);}void one[1] == TestRemoveCollection(){ Sorted Collection 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", 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;} Instructions Create a new templated class called Sorted Collection<> that inherits the Collection<> class. Add the following items to the Sorted Collection 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 Sorted Collection 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 Sorted Collection 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 Sorted Collection using no parameters, a single int parameter to specify the max size, and with a parameter of another Sorted Collection 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 59. 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& a); virtual int size(); virtual Item get(int ndx) const; virtual void add(ltem e); virtual void removeEnd(); virtual Collection& operator= (Collection& a); virtual Item operator [](int ndx); virtual void operator-(int num); 1 friend ostream& operator << <>(ostream& out, const Collection& c); protected: int capacity; int curSize; Collection(); Collection(int size); }; template { Collection::Collection() capacity = INITIAL_CAPACITY; curSize = 0; elements = make_unique(capacity); } template { static const int INITIAL_CAPACITY = 8; void expand(); unique_ptr elements; 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 { } } template void Collection::removeEnd() { } == expand(); } elements [curSize] = e; curSize++; } template = Collection & Collection:: operator=(Collection& a) { auto newElements = make_unique(a.capacity); for (int i = 0; i < a.curSize; i++) { new Elements[i] = a.elements[i]; } elements move(new Elements); 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++) { Elements[i] = elements[i]; new } elements move(new Elements); capacity *= 2; capacity) curSize--; if (curSize < 0) { throw runtime_error("Empty List"); = #endif template ostream& operator<<(ostream& Collection& c) { for (int i = 0; i < c.curSize; i++) { out << c.elements[i]; if (i < c.curSize - 1) out << " "; } return out; out, const You can test your project out with the following test cases\ Sorted CollectionTesting.cpp (this is finished.) #include #include "Sorted Collection.h"using namespace std;void TestSorted Collection(); void TestSorted Collection Constructors(); void TestAddToOperatorCollection(); void TestRemoveCollection(); bool checkCase(string name, bool condition); int main(){ TestSorted Collection(); TestSorted Collection Constructors(); TestAddToOperatorCollection(); TestRemoveCollection(); return 0;}void TestSortedCollection(){ Sorted Collectionone; 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 TestSorted Collection Constructors(){ Sorted Collectionone(10); for(double i = 0.0; i < 5; i+=.5){ one << i; } SortedCollectiontwo(one); checkCase("Constructor Test", two[0] == 0);}void TestAddToOperatorCollection(){ Sorted Collection one; one +19+9+2+8+7 + 1; checkCase("Add + 12 + 17 + 0 + 11 + 6 + 3 In Order one[0] 0); checkCase("Add In 1", == Order 2", one[11] == 19); checkCase("Add Check Size", (one << 13).size()== 13);}void one[1] == TestRemoveCollection(){ Sorted Collection 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", 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;}
Expert Answer:
Answer rating: 100% (QA)
template class SortedCollection public Collection public Additional constructor overloads SortedCollection Collection SortedCollectionint size Collectionsize SortedCollectionSortedCollection a Collect... View the full answer
Related Book For
College Mathematics for Business Economics Life Sciences and Social Sciences
ISBN: 978-0321614001
12th edition
Authors: Raymond A. Barnett, Michael R. Ziegler, Karl E. Byleen
Posted Date:
Students also viewed these programming questions
-
The following m/z peaks appeared on a mass spectrum for a peptide. Reconstruct the peptide sequence. (Please show all calculation details and explain your reasoning where appropriate. The more...
-
Best Rentals charges a daily fee plus a mileage fee for renting its cars. Barney was charged $102 for 3 days and 300 miles, while Mary was charged $189 for 5 days and 600 miles. What does Best Rental...
-
This Mini Practice Set will help you review all the key concepts of the account- ing cycle for a merchandising company along with the integration of payroll. Betty Loeb took over the business now...
-
Suppose in a given area there are three power plants, each of which emits SO 2 with different intensities. The abatement cost functions for each firm j are: (a) Set up the conditions for the socially...
-
The dynamics of a controlled submarine are significantly different from those of an aircraft, missile, or surface ship. This difference results primarily from the moment in the vertical plane due to...
-
Steam flows through a nozzle from inlet conditions at 200 psia and 800 F to an exit pressure of 30 psia. The flow is reversible and adiabatic. For a flow rate of 10 lb m /s, determine the exit area...
-
Examination of Fig. 6.5 shows that a relatively abrupt threshold in values of \(\tilde{\lambda}_{n}\) occurs as \(n\) is varied. In particular, as a rough approximation, \[ \tilde{\lambda}_{n}...
-
Superb Drive-Ins borrowed money by issuing $6,000,000 of 4% bonds payable at 97.5. Requirements 1. How much cash did Superb receive when it issued the bonds payable? 2. How much must Superb pay back...
-
Steam to drive an old-fashioned steam locomotive is supplied at a constant gauge pressure of 1.9110 6 N/m 2 to a piston with a 0.156-m radius. By calculating PV, find the work done by the steam when...
-
Olde World Windows and Doors is a manufacturer of steel windows and doors for both residential and commercial applications. The following problems are decisions that senior management faces in...
-
Online Students: Initial posts are due by 11:59 p.m. Wednesday, CT (Central Time). After posting your initial responses, engage with your fellow classmates by responding with 2 or more replies to...
-
The molar volume, in [cm3/mol], of a binary mixture of ethanol (1) and ethylene glycol (2) at 25C is given in the following table. Using the graphical method, determine the partial molar volume of...
-
A wave on a rope is given by \(f(x, t)=(0.750 \mathrm{~cm}) \cos \pi\left[\left(4.00 \mathrm{~m}^{-1} ight) x-\left(4.50 \mathrm{~s}^{-1} ight) t ight]\) Calculate the (a) wavelength, (b) angular...
-
After graduating from college in 2000, Justin Gold spent a year in California before settling in Boulder, Colorado. He waited tables to pay the rent and enjoyed outdoor activities including hiking,...
-
This chapter introduced the families of processes with schematics of some of them. Good images of many processes can be found on the internet. Use the internet to find images for the following: a....
-
You are watching a large ripple tank and observed that the waves generated by a mechanical motor have a period of \(3.5 \mathrm{~s}\) and the distance between two adjacent crests is \(15...
-
Springfield Corporation reported the following selected items at February 28, 2024: Prepare the current assets section of Springfield's statement of financial position. (List Current Assets in order...
-
The figure shows six containers, each of which is filled from the top. Assume that water is poured into the containers at a constant rate and each container is filled in 10 seconds. Assume also that...
-
The theoretical probability of an event is less than or equal to its empirical probability. In Problem, discuss the validity of each statement. If the statement is always true, explain why. If not,...
-
Refer to Problem 68. Find Pk for k = 2,4, 8,... . Can you identify a matrix Q that the matrices Pk are approaching? If so, how is Q related to the results you discovered in Problem 68? Use a graphing...
-
The total cost (in dollars) of printing x dictionaries is C(x) = 20,000 + 10x. (A) Find the average cost per unit if 1.000 dictionaries are produced. (B) Find the average value of the cost function...
-
The trial balance of Mumbai Co. at 31 December was as follows: The following information is applicable. (a) Inventory on 31 December is 850,000. (b) The cumulative allowance for doubtful debts is to...
-
The trial balance of Penang Co. on 31 July 20X7 was as follows. The following additional information is available: 1. Inventory at 31 July 20X6 is valued at 1,361,000. 2. Depreciation for the year is...
-
The trial balance of Shanghai Co. on 31 March 20X7 is given below. The following information is also relevant: 1. Closing inventory is valued at 133m. 2. Electricity accrued is estimated to be 5m. 3....
Study smarter with the SolutionInn App