Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Needs to be done in C++ Develop the generic object Array that creates and manage

ID: 3769639 • Letter: N

Question

Needs to be done in C++

Develop the generic object Array that creates and manages an array of generic type by the use of pointers and templates. The Array object will implement a number of useful manipulation of arrays including insert, remove, etc. These functions will limit the use of an array to proper handling. For example these functions will eliminate the possibility of overstepping the boundaries of an array.

Your object should satisfy the following criteria:

1:Should be named Array

2:Should contain at least all of the member functions and data that are listed in Array.h file listed in this page.

3:The parameter that holds the size of the array should be private.

4:Should contain a size() method that will return the size of the array.

5:Should contain a set(index, value) method that will set the data[index] = value.

6: Should contain a get(index) method that will return data[index].

7: Both set and get methods should exit the program if index is outside of the array boundaries.

8: Should contain an overloaded assignment operator (=) .

9: Should contain an overloaded equality operator (==) similar to the equal(intArray anIntArray) method that will return true if two arrays are equal in size and content.

10: Should contain an overloaded addition and subtraction operator (+, -) .

11: Should contain overloaded post and pre-increment operators.

12: Should contain overloaded unary - operator. S

13: Should contain overloaded I/O-insertion ( << ) and I/O-extraction ( >> ) operators. Examples: cout << intArray should result in printing of the array to the stdout. cin >> intArray should prompt the user to entry values for every element of the array. Both operations need to be cascade enabled.

14: The copy, add and subtract methods above should exit the program if size incompatibility is identified.

15: Should contain an insert(index, value) method that will insert the value at the index place of an array. The value should be inserted and not replaced.

16: Should contain a remove(index) method that will delete the index item of the array. Your method should then return the remove value.

17: Both insert and remove should exit the program if the index is invalid, and should expand/shrink the array appropriately.

18: Should also contain the following overloaded operators: +=, -=, [] on both left and right sides of the assignment operator.

19: Your object should be template based to allow for user defined array types.

20: There should be no memory leaks.

Please use the following .h file.

#ifndef ARRAY_H
#define   ARRAY_H
#include
using namespace std;

class Array {
public:
    Array();
    Array(int arraySize, int value = 0);
    Array(int anArray[], int arraySize);
    Array(const Array &orig);
    virtual ~Array();

    const Array & operator=(const Array & rhs);
    const Array operator+(const Array & rhs) const;
    const Array operator-(const Array & rhs) const;
    const Array & operator+=(const Array & rhs);
    const Array & operator-=(const Array & rhs);
  
    const Array & operator++();
    const Array & operator++(int dummy);
    const Array operator-() const;
  
    bool operator==(const Array & rhs) const;
    int operator[](int index) const; // Equivalent to get
    int & operator[](int index); // Equivalent to set

    void Print(void) const;
    void set(int index, int value);
    int get(int index) const;
    int size();
    void insert(int index, int value);
    int remove(int index);

    friend ostream & operator<<(ostream & outStream, const Array & rhs);
    friend istream & operator>>(istream & inStream, Array & rhs);


private:
    int Size;
    int *Data;
};

#endif   /* ARRAY_H */

Explanation / Answer

#include "Array.h"

Array::Array()
{}
Array::Array(int arraySize, int value = 0)
{
   size=arraySize;
   }
Array::Array(int anArray[], int arraySize)
{
   size=arraySize;
   for(int i=0;i<size;i++)
   Data[i]=anArray[i];
   }
Array::Array(const Array &orig)
{
   Data=&orig;
   }
   virtual Array::~Array();
const Array & operator=(const Array & rhs)
{
   Data=&rhs;
   }
const Array operator+(const Array & rhs) const
{
   Data[i]=Data[i]+rhs[i];
   }
const Array operator-(const Array & rhs) const
{
   Data[i]=Data[i]-rhs[i];
   }
const Array & operator+=(const Array & rhs)
{
   Data[i]+=rhs[i];
   return Data;
   }
const Array & operator-=(const Array & rhs)
{
   Data[i]-=rhs[i];
   return Data;
   }
  
const Array & operator++()
{
   Data[i]=Data[i]+1;
   return Data;
   }
const Array & operator--()
{
   Data[i]=Data[i]-1;
   return Data;
   }
  
bool operator==(const Array & rhs) const
{
   bool op=true;
   if(size== rhs.size())
   {
       for(int i=0;i<size;i++)
       {
       if(Data[i]!=rhs[i])
       {
           op=false;
           break;
           }
           }
       }
       else
           op=false;
       return op;
   }
int operator[](int index) const// Equivalent to get
{
   if(index<0 ||index>size)
   {
       cout<<endl<<"Array index is out of it boundaries";
       exit(0);  
       }
       else
   return Data[index];
   }
int & operator[](int index) // Equivalent to set
{
   if(index<0 ||index>size)
   {
       cout<<endl<<"Array index is out of it boundaries";
       exit(0);  
       }
       else
   Data[index]=value;
   return Data[index];
   }
void Array::Print(void) const
{
   for(int i=0;i<size;i++)
       cout<<" "<<Data[i];
   }
void Array::set(int index, int value)
{
   if(index<0 ||index>size)
   {
       cout<<endl<<"Array index is out of it boundaries";
       exit(0);  
       }
       else
   Data[index]=value;
   }
int Array::get(int index) const
{
   if(index<0 ||index>size)
   {
       cout<<endl<<"Array index is out of it boundaries";
       exit(0);  
       }
       else
   return Data[index];
   }
int Array::size()
{
       return size;
   }
void Array::insert(int index, int value)
{
   if(index<0 ||index>size)
   {
       cout<<endl<<"Array index is out of it boundaries";
       exit(0);  
       }
       // shifting the items to create space for new item
       for(int i=index;i<size;i++)
       {
           Data[i+1]=Data[i];
       }
       //placing value
       Data[index]=value;
       size++;
   }
int Array::remove(int index)
{
   if(index<0 ||index>size)
   {
       cout<<endl<<"Array index is out of it boundaries";
       exit(0);  
       }
       int v=Data[index];
       // shifting the items to adjust the space after deletion
       for(int i=index;i<size;i++)
       {
           Data[i]=Data[i+1];
       }
       size--;
       //returning value at index
       return v;
   }
friend ostream & operator<<(ostream & outStream, const Array & rhs)
{
   cout<<endl<<"Contents of the Array "
   for(int i=index;i<rhs.size();i++)
       {
           outStream<<" "<<rhs[i];
       }
   }
friend istream & operator>>(istream & inStream, Array & rhs)
{
   cout<<endl<<"Reading Array elements "
   for(int i=index;i<rhs.size();i++)
       {
           inStream>>rhs[i];
       }
   }
int main()
{
   //write your testing code
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote