(c++) Below is code of staticArray Class and s taticArraryIterator Class using t
ID: 3821411 • Letter: #
Question
(c++) Below is code of staticArray Class and staticArraryIterator Class using templates. Now create a test client (.cpp) by using this code that create an array, set every element to containsome value, and print every element. Any loops you write should use an iterator.
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());
}
};
Explanation / Answer
#include <iostream>
#include <vector>
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());
}
};
int main()
{
//creating object
staticArray stArr;
vector<int> dataVec;
//copyting to vector
copy(&stArr.arr[0], &stArr.arr[10], back_inserter(dataVec));
for (std::vector<int>::iterator it = dataVec.begin() ; it != dataVec.end(); ++it){
*it = 5;
cout<<*it;
}
//copying back to array
std::copy(dataVec.begin(), dataVec.end(), stArr.arr);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.