Question: In C++; follow the requirements and use the code below if possible: 1. Collection class Create a class named Collection that will store double values

In C++; follow the requirements and use the code below if possible:

1. Collection class

Create a class named Collection that will store double values in an array. You will need to implement the following public methods.

  • Default constructor.
  • Single-argument constructor that takes and integer parameter and uses it to set the initial capacity of the array
  • getSize - returns the number of elements in the array. Unlike c-strings where we had a null-terminator to mark the end of the array, in this case you will need a variable to keep track of the number of elements currently in the array.
  • getCapacity - returns the maximum number of elements allowed in the current array. Again you will probably want to store this value in a variable and update it each time you grow the array to a new size.
  • add - Adds the double to the end of the array. If the array is full it should create a new array that is twice as large and copy all of the existing elements over.
  • get - Gets the value stored at the specified position. Throws and out_of_range exception if the index is outside the bounds of the array.
  • find - Returns the index of the specified value in the array or -1 if the value is not found.
  • getFront - Returns the first value in the array. Throws an out_of_range exception if the array is empty.
  • getEnd - Returns the last value in the array. Throws and out_of_range exception if the array is empty.
  • removeFront - Removes the first value in the array and moves everything else over. If the array is empty this method has no effect.
  • removeEnd - Removes the last value in the array. If the array is empty this method has no effect.
  • remove - Removes the specified value from the array. If there are multiple instances of the same value in the array it removes the first instance. If the value does not occur in the array this method has no effect.
  • operator[] - Allows the class to be accessed like an array (i.e. returns the value stored in the specifed position as a reference.
  • operator- - Removes the specified number of items from the end of the array. if the number of items to remove is more than there are items in the array all the existing items in the array are removed. Returns a reference to this instance so that the operator can be chained.
  • operator+ - Adds the double to the end of the array. If the array is full it should create a new array that is twice as large and copy all of the existing elements over. Returns a reference to this instance so that the operator can be chained.
  • operator+(const Collection& other) - Adds all of the elements from the other Collection. When the array fills up, it will need to be resized and copied like always. Returns a reference to this instance so that the operator can be chained.
  • operator<< - Adds the double to the end of the array. If the array is full it should create a new array that is twice as large and copy all of the existing elements over. Returns a reference to this instance so that the operator can be chained.
  • friend operator<<(std::ostream& out, const Collection& other) - Outputs the values in the array to the provided output stream.

2. Collection Class Memory-Management

In addition the to public methods required to pass the test cases, you will also need to implement a copy-constructor, overloaded assignment operator, and destructor to perform deep-copies of the storage array.

3. SortedCollection Class with Inheritance

Finally, you will need to create a SortedCollection class which inherits from the Collection class. It will need to override any methods needed to make the class store values in sorted order (i.e. from smallest to largest). The key here is to realize that the only methods which need to change are those in which new items are added to the array. All the other methods behave identically in both classes and thus do not need to be re-implemented but can simply be inherited.

#include "Collection.h"

Collection::Collection(){

} Collection::Collection(int size){

} int Collection::getSize() const{ return -1; } int Collection::getCapacity() const{ return -1; } double Collection::get(int ndx) const{ return 0.0; } double Collection::getFront() const{ return 0.0; } double Collection::getEnd() const{ return 0.0; } int Collection::find(double needle) const{ return -1; } void Collection::add(double item){ } void Collection::removeFront(){ } void Collection::removeEnd(){ } void Collection::remove(double item){ } double& Collection::operator[](int ndx){ //TODO:remove tmp and return a refernce //to the actual value in the collection double* tmp = new double(-1.0); return *tmp; }

Collection& Collection::operator-(int count){ return *this; } Collection& Collection::operator+(double item){ return *this; } Collection& Collection::operator+(const Collection& other){ return *this; } Collection& Collection::operator<<(double item){ return *this; } std::ostream& operator<<(std::ostream& out, const Collection& other){ return out; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!