Question: Write a C++ program to modify the SimpleVector class template presented in this chapter to include the member functions push_back and pop_back. These functions should

Write a C++ program to modify the SimpleVector class template presented in this chapter 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 Table 7-4 on page 448) The push_back function should accept an argument and insert its value as the last element in the vector. Note: Array size need to be adjusted to hold one extra element.

The pop_back function should accept no argument and remove the last element from the array. Again the size of the array need to be updated.

Test the class with a driver program. Define vectors of Integer datatype

Double datatype

String datatype

For each vector:

1) Add element(s) using push_back() function, then display the vector to verify the result

2) Remove element(s) using pop_back() function, then display the vector to verify that the element(s) are removed.

3) Demonstrate size(), getElementat(), and operator[] member functions

SimpleVector.h

// SimpleVector class template

#ifndef SIMPLEVECTOR_H

#define SIMPLEVECTOR_H

#include

#include // Needed for bad_alloc exception

#include // Needed for the exit function

using namespace std;

template

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

SimpleVector::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

SimpleVector::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);

// aptr[count] = obj.aptr[count];

}

//**************************************

// Destructor for SimpleVector class. *

//**************************************

template

SimpleVector::~SimpleVector()

{

if (arraySize > 0)

delete [] aptr;

}

//*******************************************************

// memError function. Displays an error message and *

// terminates the program when memory allocation fails. *

//*******************************************************

template

void SimpleVector::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

void SimpleVector::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

T SimpleVector::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

T &SimpleVector::operator[](const int &sub)

{

if (sub < 0 || sub >= arraySize)

subError();

return aptr[sub];

}

#endif

----------------------------------------------------------------

Make your own driver program, You can use a menu to (1) insert a number (use push_back function) (2) remove last number (use pop_back function) (3) display size of a SimpleVector object (4) display the number at certain location (use getElementAt function) (5) test [ ] operator.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!