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

Write a class template named SortableVector. The class should be derived from th

ID: 3907391 • Letter: W

Question

Write a class template named SortableVector. The class should be derived from the SimpleVector class presented in this chapter. It should have a member function that sorts the array elements in ascending order. (Use the sorting algorithm of your choice.)
Test the template in a driver program.

SimpleVector class code:

// SimpleVector class template

#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() const; // Handles memory allocation errors

void subError() const; // 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) const;

// 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;

// aptr[count] = 0;

}

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

// Copy Constructor for SimpleVector class. *

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

template <class T>

SimpleVector<T>::SimpleVector(const SimpleVector &obj)

{

if (aptr) {

delete[] aptr; // recycle if *this has an array

}

// Copy the array size.

arraySize = obj.arraySize;

// Allocate memory for the array.

try

{

aptr = new T [arraySize];

}

catch (bad_alloc)

{

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 <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() const

{

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() const

{

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) const

{

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];

}

#endif

Explanation / Answer

#include <iostream>

#include "SortableVector.h"

using namespace std;

int main()

{

const int SIZE = 10;

SortableVector<int> intTable(SIZE);

SortableVector<double> doubleTable(SIZE);

int intArray[SIZE] = {4, 1, 9, 2, 5, 20, 14, 3, 5, 17};

double doubleArray[SIZE] = {12.4, 30.1, 9.14, 2.35, 17.9, 1.4, 25.4, 8.4, 20.6, 11.7};

// Store values in the vectors

for (int x = 0; x < SIZE; x++)

{

intTable[x] = intArray[x];

doubleTable[x] = doubleArray[x];

}

// Display the values in the vector

cout << "These values are in intTable: ";

for (int x = 0; x < SIZE; x++)

cout << intTable[x] << " ";

cout << endl;

cout << "These values are in doubleTable: ";

for (int x = 0; x < SIZE; x++)

cout << doubleTable[x] << " ";

cout << endl;

cout << " Call function sortAscending on Tables. ";

intTable.sortAscending();

doubleTable.sortAscending();

// Display the values in the vector

cout << "These values are in intTable: ";

for (int x = 0; x < SIZE; x++)

cout << intTable[x] << " ";

cout << endl;

cout << "These values are in doubleTable: ";

for (int x = 0; x < SIZE; x++)

cout << doubleTable[x] << " ";

cout << endl;

return 0;

}

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