Question

How to use C++ figure this question? Collection and Sorted Collection An array is great for...

How to use C++ figure this question?

Collection and Sorted Collection

An array is great for storing a list of values. However, one challenge when working with arrays is they have a fixed size. If you declare an array of size 10 and later need to actually store 11 elements you have to create a new array and copy everything over. C++ (and other langauges) create classes that handle this and other operations behind the scenes. In this assignment, we will be creating our own class to manage an array of doubles. The class shoule allow the array to grow everytime more space is needed. It should automatically create larger arrays as needed and copy over the existing values. We will also be creating a subclass that keeps the values in "sorted-order" as they are inserted so that the smallest value is first, the next smallest is second and so on.

Goals:

The purpose of this project is to continue working with C++ classes, incorporate dynamic memory management, and use inheritance to add new functionality to a class.

Requirements:

Code construction can be divided into three primary tasks:

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.

Homework Answers

Answer #1

#include <iostream>

using namespace std;

class Collection

{

public:

int capacity;

int size;

double *arr;

Collection()

{

capacity = 10;

arr = new double[capacity];

size = 0;

}

Collection(int size)

{

capacity = size;

arr = new double[capacity];

this->size = 0;

}

Collection(const Collection & other)

{

this->size = 0;

this->capacity = other.capacity;

this->arr = new double[this->capacity];

for (int i = 0; i < other.size; i++)

{

this->add(other.arr[i]);

}

}

int getSize()

{

return size;

}

int getCapacity()

{

return capacity;

}

void resize()

{

double temp[capacity];

for (int i = 0; i < capacity; i++)

{

temp[i] = arr[i];

}

delete[] arr;

capacity = capacity * 2;

arr = new double[capacity];

for (int i = 0; i < size; i++)

{

arr[i] = temp[i];

}

}

void add(double x)

{

if (size == capacity)

{

resize();

}

else

{

arr[size] = x;

size++;

}

}

double get(int pos)

{

try

{

if (pos > size)

{

throw 0;

}

}

catch (int e)

{

cout << "Exception: Position given out of range!" << endl;

}

return arr[pos];

}

int find(double value)

{

int pos = -1;

int i = 0;

int x = 0;

while (i < size && x == 0)

{

if (arr[i] == value)

{

x = 1;

pos = i;

}

i++;

}

return pos;

}

double getFront()

{

try

{

if (size == 0)

{

throw 0;

}

}

catch (int e)

{

cout << "Exception: The array is empty!" << endl;

}

return arr[0];

}

double getEnd()

{

try

{

if (size == 0)

{

throw 0;

}

}

catch (int e)

{

cout << "Exception: The array is empty!" << endl;

}

return arr[size];

}

void removeFront()

{

if (size != 0)

{

for (int i = 0; i < size-1; i++)

{

arr[i] = arr[i+1];

}

size--;

}

}

void removeEnd()

{

if (size != 0)

{

size--;

}

}

void remove(double value)

{

int j = 0;

int done = 0;

double temp[size-1];

for (int i = 0; i < size; i++)

{

if (arr[i] == value && done == 0)

{

temp[j] = arr[i];

j++;

done++;

}

if (arr[i] != value)

{

temp[j] = arr[i];

j++;

}

}

delete[] arr;

size--;

arr = new double[capacity];

for (int i = 0; i < size; i++)

{

arr[i] = temp[i];

}

}

double operator[] (int pos)

{

return arr[pos];

}

Collection operator- (int number)

{

for (int i = 0; i < number; i++)

{

removeEnd();

}

return *this;

}

Collection operator+ (double value)

{

add(value);

return *this;

}

Collection operator+ (const Collection & other)

{

for (int i = 0; i < other.size; i++)

{

this->add(other.arr[i]);

}

return *this;

}

Collection operator << (double value)

{

this->add(value);

return *this;

}

friend void operator << (ostream & out, const Collection & other)

{

for (int i = 0; i < other.size; i++)

{

out << other.arr[i] << " ";

}

out << endl;

}

Collection & operator = (const Collection & other)

{

this->size = 0;

this->capacity = other.capacity;

delete[] this->arr;

this->arr = new double[capacity];

for (int i = 0; i < other.size; i++)

{

this->add(other.arr[i]);

}

return *this;

}

~Collection()

{

delete[] arr;

}

};

class SortedCollection: public Collection

{

public:

SortedCollection()

{

}

void sort()

{

int temp;

for (int i = 0; i < size; i++)

{

for (int j = 0; j < size - 1 - i; j++)

{

if (arr[i] > arr[i+1])

{

temp = arr[i];

arr[i] = arr[i+1];

arr[i+1] = temp;

}

}

}

}

void add(double x)

{

if (size == capacity)

{

resize();

}

else

{

arr[size] = x;

size++;

}

sort();

}

~SortedCollection()

{

}

};

int main(int argc, char** argv)

{

SortedCollection s;

for (int i = 0; i < 5; i++)

{

s.add((double)i);

}

for (int i = 0; i < 5; i++)

{

cout << s;

}

return 0;

}

COMMENT DOWN FOR ANY QUERIES AND,

LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You are asked to implement a C++ class to model a sorted array of unsigned integers....
You are asked to implement a C++ class to model a sorted array of unsigned integers. The class is to be used in an embedded application that cannot assume the presence of the STL. The array has to be dynamically allocated in such a way that allows programmers using it to specify the required size. Your class should should: (1) provide the appropriate constructors and destructor; (2) provide methods for updating, and showing numbers in/to the array (e.g., to be...
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements....
TestQueue.java Design a class named Queue for storing integers. Like a stack, a queue holds elements. But in a queue, the elements are retrieved in a first-in first-out fashion. The class contains: An int[] data field named elements that stores the int values in the queue. A data field named size that stores the number of elements in the queue. A constructor that creates a Queue object with default capacity 8. The method enqueue(int v) that adds v into the...
IntNode class I am providing the IntNode class you are required to use. Place this class...
IntNode class I am providing the IntNode class you are required to use. Place this class definition within the IntList.h file exactly as is. Make sure you place it above the definition of your IntList class. Notice that you will not code an implementation file for the IntNode class. The IntNode constructor has been defined inline (within the class declaration). Do not write any other functions for the IntNode class. Use as is. struct IntNode { int data; IntNode *next;...
c++ just one file for all of it. You are to implement a 'list' class to...
c++ just one file for all of it. You are to implement a 'list' class to handle a list with general operations. That means you can insert and delete any element anywhere in the list. your list will be an array. the list has no order, except for the order you insert or delete. The methods you are to implement are as given: constructor methods (two, default and copy constructor, a list to a newly defined list, ie 'list listA(listB)')...
Goal:   Manage the inventory of objects sold in an online or brick and mortar store. If...
Goal:   Manage the inventory of objects sold in an online or brick and mortar store. If you can't implement all of the features of Project described in this document, implement what you can. Start by implementing the StockItem class, and its derived classes. Then add in the InventoryManager class later. In each case, the test files must be in separate classes. UML Diagram: Use Violet or other drawing software to draw the UML diagrams of each class that you use...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the...
Complete this in C++ and explain what is being done. 1      Introduction The functions in the following subsections can all go in one big file called pointerpractice.cpp. 1.1     Basics Write a function, int square 1(int∗ p), that takes a pointer to an int and returns the square of the int that it points to. Write a function, void square 2(int∗ p), that takes a pointer to an int and replaces that int (the one pointed to by p) with its...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template...
Data Structures using C++ Consider the classes QueueADT and ArrayQueueType: QueueADT: #ifndef QUEUEADT_H #define QUEUEADT_H template <class ItemType> class QueueADT { public:        // Action responsibilities        virtual void resetQueue() = 0;           // Reset the queue to an empty queue.           // Post: Queue is empty.        virtual void add(const ItemType& newItem) = 0;           // Function to add newItem to the queue.           // Pre: The queue exists and is not full.          ...
Lab 02, Data Abstractions and Structures, CSE 274, Fall 2019 Linked Bag In this lab, we...
Lab 02, Data Abstractions and Structures, CSE 274, Fall 2019 Linked Bag In this lab, we will implement the Linked Bag. The bag will contain a sequence of strings. First, you need to design the Node class (Node.java). It will contain an integer data and a reference to the next Node. The constructor of Node class receives an integer and assigns it to the data field. It also has a default constructor. Then we will design another class named LinkedBag...
Trace the C++ program below showing all output in the order that it is displayed. If...
Trace the C++ program below showing all output in the order that it is displayed. If anything happens that makes it impossible to accomplish an operation or the results of so doingare unpredictable, describe what happens and abort the program at that point. Assume the Queue class is fully defined in an appropriate header and implementation file. The effect of the functions (methods) is as follows - Constructor - creates an empty queue #include <iostream> #include using namespace std; #include...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private...
My assignment: Triplet Template Class Directions: Define a template class for a generic triplet. The private data member for the triplet is a generic array with three elements. The triplet ADT has the following functions:  default constructor  explicit constructor: initialize the data member using parameters  three accessors (three get functions) which will return the value of each individual element of the array data member  one mutator (set function) which will assign values to the data member...