Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In C++ Specialized Templates In this chapter, the section Specialized Templates within Section 16.4 describes how to design templates that are specialized for one particular

In C++

Specialized Templates In this chapter, the section Specialized Templates within Section 16.4 describes how to design templates that are specialized for one particular data type. The section introduces a method for specializing a version of the SimpleVector class template so it will work with strings. Complete the specialization for both the SimpleVector and SearchableVector templates. Demonstrate them with a simple driver program.

Section 16.4

// SimpleVector class template 2 #ifndef SIMPLEVECTOR_H 3 #define SIMPLEVECTOR_H 4 #include 5 #include // Needed for bad_alloc exception 6 #include // Needed for the exit function 7 using namespace std; 8 9 template 10 class SimpleVector 11 { 12 private: 13 T *aptr; // To point to the allocated array 14 int arraySize; // Number of elements in the array 15 void memError(); // Handles memory allocation errors 16 void subError(); // Handles subscripts out of range 17 18 public: 19 // Default constructor 20 SimpleVector() 21 { aptr = 0; arraySize = 0;} 22 23 // Constructor declaration 24 SimpleVector(int); 25 26 // Copy constructor declaration 27 SimpleVector(const SimpleVector &); 28 29 // Destructor declaration 30 ~SimpleVector(); 31 32 // Accessor to return the array size 33 int size() const 34 { return arraySize; } 35 36 // Accessor to return a specific element 37 T getElementAt(int position); 38 39 // Overloaded [] operator declaration 40 T &operator[](const int &); 41 }; 42 43 //************************************************************ 44 // Constructor for SimpleVector class. Sets the size of the * 45 // array and allocates memory for it. * 46 //*************************************************************** template 49 SimpleVector::SimpleVector(int s) 50 { 51 arraySize = s; 52 // Allocate memory for the array. 53 try 54 { 55 aptr = new T [s]; 56 } 57 catch (bad_alloc) 58 { 59 memError(); 60 } 61 62 // Initialize the array. 63 for (int count = 0; count < arraySize; count++) 64 *(aptr + count) = 0; 65 } 66 67 //******************************************* 68 // Copy Constructor for SimpleVector class. * 69 //******************************************* 70 71 template 72 SimpleVector::SimpleVector(const SimpleVector &obj) 73 { 74 // Copy the array size. 75 arraySize = obj.arraySize; 76 77 // Allocate memory for the array. 78 aptr = new T [arraySize]; 79 if (aptr == 0) 80 memError(); 81 82 // Copy the elements of obj's array. 83 for(int count = 0; count < arraySize; count++) 84 *(aptr + count) = *(obj.aptr + count); 85 } 86 87 //************************************** 88 // Destructor for SimpleVector class. * 89 //************************************** 90 91 template 92 SimpleVector::~SimpleVector() 93 { 94 if (arraySize > 0) 95 delete [] aptr; 96 } 97 98 //******************************************************** 99 // memError function. Displays an error message and * 100 // terminates the program when memory allocation fails. * 101 //************************************************************ template 104 void SimpleVector::memError() 105 { 106 cout << "ERROR:Cannot allocate memory. "; 107 exit(EXIT_FAILURE); 108 } 109 110 //************************************************************ 111 // subError function. Displays an error message and * 112 // terminates the program when a subscript is out of range. * 113 //************************************************************ 114 115 template 116 void SimpleVector::subError() 117 { 118 cout << "ERROR: Subscript out of range. "; 119 exit(EXIT_FAILURE); 120 } 121 122 //******************************************************* 123 // getElementAt function. The argument is a subscript. * 124 // This function returns the value stored at the * 125 // subcript in the array. * 126 //******************************************************* 127 128 template 129 T SimpleVector::getElementAt(int sub) 130 { 131 if (sub < 0 || sub >= arraySize) 132 subError(); 133 return aptr[sub]; 134 } 135 136 //******************************************************** 137 // Overloaded [] operator. The argument is a subscript. * 138 // This function returns a reference to the element * 139 // in the array indexed by the subscript. * 140 //******************************************************** 141 142 template 143 T &SimpleVector::operator[](const int &sub) 144 { 145 if (sub < 0 || sub >= arraySize) 146 subError(); 147 return aptr[sub]; 148 }

REPORTING FORMAT: Source Code & Testing Results

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Visual Basic 6 Databases

Authors: Charles Williams

1st Edition

1861002025, 978-1861002020

More Books

Students also viewed these Databases questions