Question: Hi ! Could someone please take a look at the following C + + Collection . cpp and Collection.h files about creating a dynamic array?

Hi!Could someone please take a look at the following C++Collection.cpp and Collection.h files about creating a dynamic array? Specifically I'm having trouble with the addFront method (where the runtime error is being thrown regardless)which needs to follow the below instructions:
void addFront(double 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 runtime_exception.
Collection.cpp:
--
#include "Collection.h"
#include
#include
//Default constructor
Collection::Collection(){
arr =new double[5]; //initialize double array to fixed capacity of 5elements
size =0;
capacity =5;
}
//Overloaded constructor with initial capacity as an argument
Collection::Collection(int size){
arr =new double[size]; //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::Collection(const Collection& other): size(other.size),capacity(other.capacity){
arr =new double[capacity];
//creating deep copy
for (int i =0; i <size; ++i){
arr[i]=other.arr[i];
}
}
//Destructor to delete dynamically allocated array
Collection::~Collection(){
delete[]arr;
}
//Overloaded assignment operator for assigning one object to another existing object
Collection& Collection::operator=(const Collection& other){
//check for self-assignment
if (this !=&other){
//delete existing array
delete[]arr;
//create deep copy
size =other.size;
capacity =other.capacity;
arr =new double[capacity];
for (int i =0; i <size; ++i){
arr[i]=other.arr[i];
}
}
return *this;
}
//Method to get current number of elements in the array
int Collection::getSize()const {
return size;
}
//Method to get the current max capacity of the array
int Collection::getCapacity()const {
return capacity;
}
//Method to add a value to the end of the array
void Collection::add(double value){
//throw runtime error if new item exceeds array capacity
if (size >=capacity){
throw std::runtime_error("List Full.");
}
//if list is not full, add item to back of array
arr[size++]=value;
}
//Method to add a value to the front of the array
void Collection::addFront(double value){
//throw runtime error if new item exceeds array capacity
if (size >=capacity){
throw std::runtime_error("List Full.");
}
else {
//shift elements to the right
for (int i =size; i >0; i--){
arr[i]=arr[i -1];
}
//insert new value and increment size
arr[0]=value;
size++;
}
}
//Method to get the value at a specific position
double Collection::get(int ndx){
//throw out_of_range error if index is outside of array bounds
if (ndx <0||ndx >=size){
throw std::out_of_range("Index is outside the bounds of the array.");
}
return arr[ndx];
}
//Method to get the first value in the array
double Collection::getFront(){
//throw out_of_range error if array is empty
if (size ==0){
throw std::out_of_range("Array is empty.");
}
return arr[0];
}
//Method to get the last value in the array
double Collection::getEnd(){
//throw out_of_range error if array is empty
if (size ==0){
throw std::out_of_range("Array is empty.");
}
return arr[size -1];
}
//Method to find a specific value in the array
int Collection::find(double needle){
//Return position of needle if it exists in array
for (int i =0; i <size; i++){
if (arr[i]==needle){
return i;
}
}
//Not found
return -1;
}
//Overload of extraction operator for easy display of the collection
std::ostream& operator<<(std::ostream& out, const Collection& c){
for (int i =0; i <c.size; i++){
out <<c.arr[i];
//add space delimiter
if (i <c.size -1)out <<"";
}
return out;
}
Collection.h:
--
#ifndef COLLECTION_H
#define COLLECTION_H
#include
#include
class Collection {
private:
double*arr{nullptr }; //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
Collection(int size);
//copy constructor
Collection(const Collection& other);
//destructor
~Collection();
//assignment operator
Collection& operator=(const Collection& other);
//get the number of elements in the array
int getSize()const;
//get the maximum number of elements that the array can hold
int getCapacity()const;
//add a value to the end of the array
void add(double value);
//add a value to the front of the array
void addFront(double value);
//get the value at the specified position in the array
double get(int 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 find(double needle);
//overload the extraction operator to display the list
friend std::ostream& operator<<(std::ostream& out, const Collection& c);
};
#endif

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 Programming Questions!