Answer:
See explaination for code
Step-by-step explanation:
#ifndef SIMPLEVECTOR_H
#define SIMPLEVECTOR_H
#include <iostream>
#include <new> // Needed for bad_alloc exception
#include <cstdlib> // Needed for the exit function
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 errors
void subError(); // Handles subscripts out of range
public:
// Default constructor
SimpleVector()
{ aptr = 0; arraySize = 0;}
// Constructor declaration
SimpleVector(int);
// Copy constructor declaration
SimpleVector(const SimpleVector &);
// Destructor declaration
~SimpleVector();
// Accessor to return the array size
int size() const
{ return arraySize; }
// Accessor to return a specific element
T getElementAt(int position);
// Overloaded [] 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. *
//*******************************************
template <class T>
SimpleVector<T>::SimpleVector(const SimpleVector &obj)
{
// Copy the array size.
arraySize = obj.arraySize;
// Allocate memory for the array.
aptr = new T [arraySize];
if (aptr == 0)
memError();
// Copy the elements 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. Displays an error message and *
// terminates the program when memory allocation fails. *
//************************************************************
template <class T>
void SimpleVector<T>::memError()
{
cout << "ERROR:Cannot allocate memory.\\";
exit(EXIT_FAILURE);
}
//************************************************************
// subError function. Displays an error message and *
// terminates the program when a subscript is out of range. *
//************************************************************
template <class T>
void SimpleVector<T>::subError()
{
cout << "ERROR: Subscript out of range.\\";
exit(EXIT_FAILURE);
}
//*******************************************************
// getElementAt function. The argument is a subscript. *
// This function returns the value stored at the *
// subcript in the array. *
//*******************************************************
template <class T>
T SimpleVector<T>::getElementAt(int sub)
//********************************************************
// Overloaded [] operator. The argument is a subscript. *
// This function returns a reference to the element *
// in the array indexed by the subscript. *
//********************************************************
template <class T>
T &SimpleVector<T>::operator[](const int &sub)
template <class T>
class SearchableVector : public SimpleVector<T>{
public:
SearchableVector() : SimpleVector<T>(){};
SearchableVector(int a) : SimpleVector<T>(a){};
int findElement(T a){
for (int i = 0; i<SimpleVector<T>::size(); i++){
if (SimpleVector<T>::getElementAt(i) == a){
return i;
}
}
return -1;
}
};
int main(){
SimpleVector<int> s;
SimpleVector<int> s1(2);
SimpleVector<int> s3(20);
SimpleVector<int> s4(s3);
SearchableVector<int> s5(3);
cout << "Size:" << s.size() << endl;
cout << "Size:" << s1.size() << endl;
cout << "Size:" << s4.size() << endl;
s1[0] = 2;
s1[1] = 3;
s5[0] = 1;
s5[1] = 2;
s5[2] = 3;
cout << s1.getElementAt(0) << endl;
int a = s5.findElement(2);
if (a != -1)
cout << "Found at " << a << " postion\\";
else
cout << "Not Found\\";
return 0;
}
#endif