Question: I'm working in C++. I need to be able to implement the intArray::remove file from the header function. I've included the header file and the
I'm working in C++. I need to be able to implement the intArray::remove file from the header function. I've included the header file and the implementation file. I need to be able to test it with a list.
// Header File
#pragma once #ifndef __INTARRAY_H__ #define __INTARRAY_H__
// An array that grows in size class IntArray { public: IntArray(); // Default constructor: Required for dynamic memory class IntArray(const IntArray&); // Copy Constructor: Required for dynamic memory class ~IntArray(); // Destructor: Required for dynamic memory class IntArray& operator=(const IntArray&); // Assignment op: Required for dynamic memory class
void append(int value); // Add new value at end int& operator[](int index); // Access IntArray like an array: a[index] = i int operator[](int index) const; // Read-only IntArray access i = a[index] int getSize() const; // Get size of array void remove(int index); // TODO: Implement private: int size; int *data; };
#endif
// Implementation File
// Implement the intArray::remove function from the class
#include "int_array.h"
// Default constructor: Required for dynamic memory class IntArray::IntArray() { size = 0; data = nullptr; // NULL works in most compilers (not newer g++) } // Copy Constructor: Required for dynamic memory class IntArray::IntArray(const IntArray& a) { size = 0; data = nullptr; *this = a; // Forces use of operator= }
// Destructor: Required for dynamic memory class IntArray::~IntArray() { // Clean up and delete array if(data != nullptr) { delete [] data; } }
// Assignment op: Required for dynamic memory class IntArray& IntArray::operator=(const IntArray& a) { // Check for self assignment (e.g., a = a) if(this == &a) { // Always return *this return *this; } // Create new chunk of memory (if needed) int newSize = a.size; int *newData; if(newSize > 0) { newData = new int[newSize]; } else { newData = nullptr; } // Copy all newSize elements over for(int i = 0; i < newSize; ++i) { newData[i] = a.data[i]; } // Delete old & copy over to this instance if(data != nullptr) { delete [] data; } size = newSize; data = newData; // Always return *this return *this; }
// Add new value at end void IntArray::append(int value) { // Create new chunk of memory int newSize = size + 1; int *newData = new int[newSize]; // Copy [We are copying the old size of elemens] for(int i = 0; i < size; ++i) { newData[i] = data[i]; } // Delete old & copy over to this instance if(data != 0) { delete [] data; } size = newSize; data = newData; // Place new value data[size-1] = value; }
// Access IntArray like an array: a[index] = i int& IntArray::operator[](int index) { if(index >= 0 && index < size) { return data[index]; } else { // We should throw an exception, but instead we'll just return -1 return -1; } }
// Access IntArray like an array: i = a[index]i int IntArray::operator[](int index) const { if(index >= 0 && index < size) { return data[index]; } else { // We should throw an exception, but instead we'll just return -1 return -1; } }
// Get size of array int IntArray::getSize() const { return size; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
