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

Complete the SimpleVector class template below to include the member functions p

ID: 3574607 • Letter: C

Question

Complete the SimpleVector class template below to include the member functions push_back and pop_back. These functions should emulate the STL vector class member functions of the same name.
(See http://www.cplusplus.com/reference/stl/vector/vector.html )
The push_back functon should accept an argument, and insert its value at the end of the array. The pop_back function should accept no argument, and remove the last element from the array. Test the class with a driver program.
// SimpleVector class template *****************************************************
#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 &);
void push_back(T); // New push_back member
T pop_back(); // New pop_back member
};
//***********************************************************
// 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 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 function 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];
}
//***********************************************************
// The push_back function pushes its argument onto the back *
// of the vector. *
//***********************************************************
template <class T>
void SimpleVector<T>::push_back(T val)
{
}
//***********************************************************
// The pop_back function removes the last element *
// of the vector. It also returns that value. *
//***********************************************************
template <class T>
T SimpleVector<T>::pop_back()
{
}

Explanation / Answer

PROGRAM CODE:

/*
* SimpleVector.cpp
*
* Created on: 08-Dec-2016
* Author: kasturi
*/

// SimpleVector class template *****************************************************
#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
int currentIndex; // variable to hold the indexes stored so far
void MemError(); // Handles memory allocation errors
void SubError(); // Handles subscripts out of range
public:
// Default constructor
SimpleVector()
{ Aptr = 0; ArraySize = 0; currentIndex = 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 &);
void push_back(T); // New push_back member
T pop_back(); // New pop_back member
};
//***********************************************************
// 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 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 function 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];
}
//***********************************************************
// The push_back function pushes its argument onto the back *
// of the vector. *
//***********************************************************
template <class T>
void SimpleVector<T>::push_back(T val)
{
   if(currentIndex < 0 || currentIndex >= ArraySize)
       SubError();
   *(Aptr+currentIndex) = val;
   currentIndex++;
}
//***********************************************************
// The pop_back function removes the last element *
// of the vector. It also returns that value. *
//***********************************************************
template <class T>
T SimpleVector<T>::pop_back()
{
   T val = *(Aptr + currentIndex-1);
   *(Aptr + currentIndex-1) = 0;
   currentIndex--;
   return val;
}

int main()
{
   SimpleVector<int> vector(5);
   cout<<"Adding values - 1,2,3,4,5 ";
   vector.push_back(1);
   vector.push_back(2);
   vector.push_back(3);
   vector.push_back(4);
   vector.push_back(5);
   cout<<"Getting the element at index 2 ";
   cout<<vector.getElementAt(2)<<endl;
   cout<<"Removing the last element ";
   cout<<vector.pop_back()<<endl;
   return 0;
}

OUTPUT:

Adding values - 1,2,3,4,5

Getting the element at index 2

3

Removing the last element

5

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