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.
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 <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.
Get Answers For Free
Most questions answered within 1 hours.