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

C++: create a simple staticArray class with templates and iterators. Requirement

ID: 3821810 • Letter: C

Question

C++:

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.

The test should create an array, set every element to contain some value, and print every element. Any loops you write should use an iterator.

Explanation / Answer

Here is the code for the question. Please do rate the answer if it helped. Thank you very much.

#include <iostream>
using namespace std;

template <typename T>
class staticArray
{
private:
static const int MAX = 10; //static const member declared inside class
static T elements[MAX]; //static member to be defined outside class later
public:

class staticArrayIterator //iterator for class staticArray<T>
{
private:
T* pointer;
public:
staticArrayIterator() //default constructor
{
pointer = NULL;
}
staticArrayIterator(T* p) //constructor with pointer to type T
{
pointer = p;
}
staticArrayIterator& operator ++() //pre increment operator
{
pointer ++;
return *this;
}
bool operator ==(staticArrayIterator const &i2 ) //== operator for comparing 2 iterators
{
return (pointer == i2.pointer);
}
bool operator !=(staticArrayIterator const &i2) //!= operator for comparing 2 iterators
{
return (pointer != i2.pointer);
}
const T& operator *() //derencing operator for iterator
{
return *pointer;
}
};

void set(int pos, T value)
{
staticArray<T>::elements[pos] = value;
}

//returns iterator pointing to beginning of the array
staticArrayIterator begin()
{
return staticArrayIterator(staticArray<T>::elements);
}

//iterator pointing to end of array
staticArrayIterator end()
{
return staticArrayIterator(staticArray<T>::elements+MAX);
}
};


//define the static member of staticArray
template <typename T>
T staticArray<T>::elements[MAX];


int main()
{

staticArray<int> arr; //instantiate

//set values into the array
for(int i=0; i<10;i++)
arr.set(i,i*10);

//display hte values...using derencing operator and iterators
cout<<"The array elements are as follows :"<<endl;
for(staticArray<int>::staticArrayIterator it=arr.begin();it!=arr.end();++it)
cout<<" "<<*it;

  
cout<<" done"<<endl;
}

ouput

The array elements are as follows :
0 10 20 30 40 50 60 70 80 90
done

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