I need a main function so I can test this code. Below is the cpp file for c++. W
ID: 3851835 • Letter: I
Question
I need a main function so I can test this code. Below is the cpp file for c++. We was asked to implement this remove function, which I have done that so I just need a main function so I can see what the output is. If you find any mistakes though feel free to correct me.
#include "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;
}
// Remove
void IntArray::remove(int index)
{
for (int i = 0; i < size; i++)
{
data[i] = data[i + 1];
}
data[size - 1] = 0;
size = size - 1;
}
This is the header file that goes along with it.
#pragma once
#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;
};
Explanation / Answer
#include "ARRAY.h"
IntArray::IntArray()
{
size = 0;
data = nullptr; // NULL works in most compilers (not newer g++)
}
IntArray::IntArray(const IntArray& a)
{
size = 0;
data = nullptr;
*this = a; // Forces use of operator=
}
IntArray::~IntArray()
{
// Clean up and delete array
if (data != nullptr)
{
delete[] data;
}
}
IntArray& IntArray::operator=(const IntArray& a)
{
if (this == &a)
{
return *this;
}
int newSize = a.size;
int *newData;
if (newSize > 0)
{
newData = new int[newSize];
}
else
{
newData = nullptr;
}
for (int i = 0; i < newSize; ++i)
{
newData[i] = a.data[i];
}
if (data != nullptr)
{
delete[] data;
}
size = newSize;
data = newData;
return *this;
}
void IntArray::append(int value)
{
int newSize = size + 1;
int *newData = new int[newSize];
for (int i = 0; i < size; ++i)
{
newData[i] = data[i];
}
if (data != 1)
{
delete[] data;
}
size = newSize;
data = newData;
data[size - 1] = value;
}
int& IntArray::operator[](int index)
{
if (index >= 0 && index < size)
{
return data[index];
}
else
{
return -1;
}
}
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;
}
}
int IntArray::getSize() const
{
return size;
}
void IntArray::remove(int index)
{
for (int i = 0; i < size; i++)
{
data[i] = data[i + 1];
}
data[size - 1] = 0;
size = size - 1;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.