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

(C++) Goal: create a simple staticArray class with templates and iterators. Requ

ID: 3818861 • Letter: #

Question

(C++)
Goal: create a simple staticArray class with templates and iterators.
Requirements for the staticArray class:
1.Create two private member variables:
a.A static const int MAX set to 10 (why static const?)
b.A static array of MAX elements (type from template). Assume the static array will only hold numeric types.
2.Create a set method that sets an array element to a given value.
a.Parameters: the position (int), the value which must be in range 0 to 100 (type from template).
b.Do not create a get method, you will use iterators to access the data.
3.Create a begin method that returns an iterator.
a.Parameters: none.
b.Returns: the staticArrayIterator pointing to the first element.
4.Create an end method that returns an iterator.
a.Parameters: none.
b.Returns: the staticArrayIterator pointing to the last element.
5.Create a default constructor that sets all 10 elements to 0.
Requirements for the staticArrayIterator class:
1.Tip: look at the iterator class Malik created for linkedList.h for guidance.
2.The class will have a single private member: a pointer (type from template). This will be used to point to an element of the staticArray.
3.Create a default constructor that sets the pointer to NULL (or nullptr).
4.Create a parameterized constructor that takes a pointer (type from template) and sets the private member variable to that pointer.
5.Create a dereferencing operator that returns the de-referenced pointer.This will be used to access array elements.
6.Create an overloaded pre-increment operator that increments the pointer (i.e., so it points to the next array element).
7.Create overloaded == and != operators to compare iterators. Iterators are equal if they reference/point to the same place in memory. (C++)
Goal: create a simple staticArray class with templates and iterators.
Requirements for the staticArray class:
1.Create two private member variables:
a.A static const int MAX set to 10 (why static const?)
b.A static array of MAX elements (type from template). Assume the static array will only hold numeric types.
2.Create a set method that sets an array element to a given value.
a.Parameters: the position (int), the value which must be in range 0 to 100 (type from template).
b.Do not create a get method, you will use iterators to access the data.
3.Create a begin method that returns an iterator.
a.Parameters: none.
b.Returns: the staticArrayIterator pointing to the first element.
4.Create an end method that returns an iterator.
a.Parameters: none.
b.Returns: the staticArrayIterator pointing to the last element.
5.Create a default constructor that sets all 10 elements to 0.
Requirements for the staticArrayIterator class:
1.Tip: look at the iterator class Malik created for linkedList.h for guidance.
2.The class will have a single private member: a pointer (type from template). This will be used to point to an element of the staticArray.
3.Create a default constructor that sets the pointer to NULL (or nullptr).
4.Create a parameterized constructor that takes a pointer (type from template) and sets the private member variable to that pointer.
5.Create a dereferencing operator that returns the de-referenced pointer.This will be used to access array elements.
6.Create an overloaded pre-increment operator that increments the pointer (i.e., so it points to the next array element).
7.Create overloaded == and != operators to compare iterators. Iterators are equal if they reference/point to the same place in memory. (C++)
Goal: create a simple staticArray class with templates and iterators.
Requirements for the staticArray class:
1.Create two private member variables:
a.A static const int MAX set to 10 (why static const?)
b.A static array of MAX elements (type from template). Assume the static array will only hold numeric types.
2.Create a set method that sets an array element to a given value.
a.Parameters: the position (int), the value which must be in range 0 to 100 (type from template).
b.Do not create a get method, you will use iterators to access the data.
3.Create a begin method that returns an iterator.
a.Parameters: none.
b.Returns: the staticArrayIterator pointing to the first element.
4.Create an end method that returns an iterator.
a.Parameters: none.
b.Returns: the staticArrayIterator pointing to the last element.
5.Create a default constructor that sets all 10 elements to 0.
Requirements for the staticArrayIterator class:
1.Tip: look at the iterator class Malik created for linkedList.h for guidance.
2.The class will have a single private member: a pointer (type from template). This will be used to point to an element of the staticArray.
3.Create a default constructor that sets the pointer to NULL (or nullptr).
4.Create a parameterized constructor that takes a pointer (type from template) and sets the private member variable to that pointer.
5.Create a dereferencing operator that returns the de-referenced pointer.This will be used to access array elements.
6.Create an overloaded pre-increment operator that increments the pointer (i.e., so it points to the next array element).
7.Create overloaded == and != operators to compare iterators. Iterators are equal if they reference/point to the same place in memory.

Explanation / Answer

using namespace std;

template <class T> class staticArrayIterator

{

    //pointer to an element of the staticArray

    T *arrPointer;

    public:

    //default constructor that sets the pointer to NULL

    staticArrayIterator()

    {

        arrPointer=NULL;

    }

    //parameterized constructor that takes a pointer (type from template) and sets the private member variable to that pointer.

    staticArrayIterator(T *temp)

    {

        arrPointer=temp;

    }

    //dereferencing operator that returns the de-referenced pointer

  T& operator* ()

    {

        return *arrPointer;

    }

    T* operator-> ()

    {

        return arrPointer;

    }

    //overloaded pre-increment operator that increments the pointer

    T operator++()

    {

        return arrPointer++;

    }

    //overloaded == and != operators to compare iterators

    bool operator==(staticArrayIterator& rhs)const

    {

        if (this->arrPointer==rhs.arrPointer)

        return true;

        return false;

    }

    bool operator!=(staticArrayIterator& rhs)const

    {

        if (this->arrPointer!=rhs.arrPointer)

        return true;

        return false;

    }

};

template <class T> class staticArray

{

    static const int MAX=10;

    static T arr[MAX];

    public:

    //default constructor that sets all 10 elements to 0

    staticArray()

    {

        for(int i=0;i<MAX;i++)

        {

            arr[i]=0;

        }

    }

    // Sets an array element to given value

    void setElement(int position,T value)

    {

        arr[position]=value;

    }

    //Returns iterator pointing to first Element

     staticArrayIterator<T> begin()

     {

        return staticArrayIterator<T>(arr.begin());

     }

     //Returns iterator pointing to last Element

     staticArrayIterator<T> end()

     {

         return staticArrayIterator<T>(arr.end());

     }

};