Question: Hi ! Could someone please take a look at the following C + + Collection . cpp and Collection.h files about creating a dynamic array?
HiCould someone please take a look at the following CCollectioncpp and Collection.h files about creating a dynamic array? Specifically Im having trouble with the addFront method where the runtime error is being thrown regardlesswhich needs to follow the below instructions:
void addFrontdouble value: This will add an item to the front of the list.You will need to shift all the existing items. If the new item exceeds the max size,throw a runtimeexception.
Collection.cpp:
#include "Collection.h
#include
#include
Default constructor
Collection::Collection
arr new double; initialize double array to fixed capacity of elements
size ;
capacity ;
Overloaded constructor with initial capacity as an argument
Collection::Collectionint size
arr new doublesize; initialize double array to capacity of user's choice
Copy constructor, used for creating a new object as a copy of an existing object
Collection::Collectionconst Collection& other: sizeothersizecapacityothercapacity
arr new doublecapacity;
creating deep copy
for int i ; i size; i
arriotherarri;
Destructor to delete dynamically allocated array
Collection::~Collection
deletearr;
Overloaded assignment operator for assigning one object to another existing object
Collection& Collection::operatorconst Collection& other
check for selfassignment
if this &other
delete existing array
deletearr;
create deep copy
size othersize;
capacity othercapacity;
arr new doublecapacity;
for int i ; i size; i
arriotherarri;
return this;
Method to get current number of elements in the array
int Collection::getSizeconst
return size;
Method to get the current max capacity of the array
int Collection::getCapacityconst
return capacity;
Method to add a value to the end of the array
void Collection::adddouble value
throw runtime error if new item exceeds array capacity
if size capacity
throw std::runtimeerrorList Full.";
if list is not full, add item to back of array
arrsizevalue;
Method to add a value to the front of the array
void Collection::addFrontdouble value
throw runtime error if new item exceeds array capacity
if size capacity
throw std::runtimeerrorList Full.";
else
shift elements to the right
for int i size; i ; i
arriarri ;
insert new value and increment size
arrvalue;
size;
Method to get the value at a specific position
double Collection::getint ndx
throw outofrange error if index is outside of array bounds
if ndx ndx size
throw std::outofrangeIndex is outside the bounds of the array.";
return arrndx;
Method to get the first value in the array
double Collection::getFront
throw outofrange error if array is empty
if size
throw std::outofrangeArray is empty.";
return arr;
Method to get the last value in the array
double Collection::getEnd
throw outofrange error if array is empty
if size
throw std::outofrangeArray is empty.";
return arrsize ;
Method to find a specific value in the array
int Collection::finddouble needle
Return position of needle if it exists in array
for int i ; i size; i
if arrineedle
return i;
Not found
return ;
Overload of extraction operator for easy display of the collection
std::ostream& operatorstd::ostream& out, const Collection& c
for int i ; i csize; i
out carri;
add space delimiter
if i csize out ;
return out;
Collection.h:
#ifndef COLLECTIONH
#define COLLECTIONH
#include
#include
class Collection
private:
doublearrnullptr ; pointer to dynamically allocated array
int size; number of elements in the array
int capacity; maximum number of elements that the array can hold
public:
default constructor
Collection;
argument constructor
Collectionint size;
copy constructor
Collectionconst Collection& other;
destructor
~Collection;
assignment operator
Collection& operatorconst Collection& other;
get the number of elements in the array
int getSizeconst;
get the maximum number of elements that the array can hold
int getCapacityconst;
add a value to the end of the array
void adddouble value;
add a value to the front of the array
void addFrontdouble value;
get the value at the specified position in the array
double getint ndx;
get the first value in the array
double getFront;
get the last value in the array
double getEnd;
find the position of a value in the array
int finddouble needle;
overload the extraction operator to display the list
friend std::ostream& operatorstd::ostream& out, const Collection& c;
;
#endif
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
