//Header file section
#include<iostream>
using namespace std;
template <class T>
class SimpleVector
{
private:
T *aptr; //To point to the allocated array
int arraySize; //Number of elements in the array
void memError(); //Handles memory allocation erro
void subError(); //Handles subcripts out of range
//Accessor to return the array size
int size() const
{
return arraySize;
}
//Accessor to return a specific element
T getElementAt(int position);
//Overload [] operator declaration
T &operator[](const int &);
};
//**************************************************************
// Constructor for SimpleVector class. Sets the size of the *
// array and allocates memory for it. *
//**************************************************************
template <class T>
SimpleVector<T>::SimpleVector(int s)
{
arraySize = s;
//Allocate memory for the array.
try
{
aptr = new T[s];
}
catch (bad_alloc)
{
memError();
}
// Initialize the array.
for (int count = 0; count < arraySize; count++)
*(aptr + count) = 0;
}
//******************************************
//Copy constructor for SimpleVector class. *
//******************************************
//Allocate memory for the array.
aptr = new T[arraySize];
if (aptr == 0)
memError();
//Copy the element of obj's array.
for (int count = 0; count < arraySize; count++)
*(aptr + count) = *(obj.aptr + count);
}
//************************************
//Destructor for SimpleVector class. *
//************************************
template<class T>
SimpleVector<T>::~SimpleVector()
{
if (arraySize > 0)
delete[] aptr;
//******************************************************
//memError function. Display an error message and *
//terminates the program when memory allocation fails. *
//******************************************************
//*********************************************************
//subError function. Display an error message and *
//terminate the program when a subscript is out of range *
//*********************************************************
template<class T>
void SimpleVector<T>::subError()
{
cout << "Error: subscript out of range. \n";
exit(EXIT_FAILURE);
}
//*********************************************************
//getElementAt function. The argument is a subscript. *
// This function return the value stores at the sub- *
//cript in the array. *
//*********************************************************
template<class T>
T SimpleVector<T>::getElementAt(int sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
//******************************************************
//overloaded[] operator. THe argument is a subscript. *
//This fnction returns a reference to the element *
// in the array indexed by the subscript. *
//******************************************************
template <class T>
T &SimpleVector<T>::operator[](const int &sub)
{
if (sub < 0 || sub >= arraySize)
subError();
return aptr[sub];
}
template<class T>
class SearchableVector : public SimpleVector<T>
{
public:
//Default constructor
SearchableVector(): SimpleVector<T>()
{}
If you can edit your post, highlight the code portion, then click the <> button in the Format palette at the right side of the post, that'll format your code and make it much easier to read :)
1 2 3 4 5 6 7 8 9 10 11 12
//************************************
//Destructor for SimpleVector class. *
//************************************
template<class T>
SimpleVector<T>::~SimpleVector()
{
if (arraySize > 0)
delete[] aptr;
//******************************************************
1 2 3 4 5 6 7 8 9 10 11 12 13 14
template<class T>
void SearchableVector<T>::SortItems()
{
for (int i = 0; i <= this->size(); i++)
{
for (int j = 0;j < this->size() - 1; j++)
{
if (getElementAt(j)> getElementAt(j + 1))
{
T temp = this->operator [](j);
this->operator [](j)=this->operator[](j + 1);
this->operator [](j+1=temp);
int main()