In C Modify the SimpleVector class template presented in thi

In C++ Modify the SimpleVector class template presented in this lesson 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 16-5) The push_back function 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 that uses your ProductionWorker class by creating a SimpleVector of ProductionWorker class. Add 3 production workers to the SimpleVector using push_back() and display the contents of the vector after each push_back(). Then remove workers using pop_back() and display the contents of the vector after each pop_back(). Use the SimpleVector\'s test for emptiness to show that the vector is empty after the pop_back()s are complete.

Solution

template SimpleVector::~SimpleVector() { delete [] aptr; // if aptr is NULL, this will have no effect. } plate void SimpleVector::push_back(const Type& x) { Type* temp = new Type[arraySize + 1]; for(int index = 0; index < arraySize; index++) { temp[index] = aptr[index]; } temp[arraySize++] = x; delete [] aptr; aptr = temp; } template void SimpleVector::pop_back() { if(arraySize == 0 || aptr == NULL) { std::cout<<\"cannot pop back! \ \"; return; } Type* temp = new Type[arraySize]; for(int index = 0; index < arraySize; index++) { temp[index] = aptr[index]; } temp[arraySize--]; delete [] aptr; aptr = temp; }
In C++ Modify the SimpleVector class template presented in this lesson to include the member functions push_back and pop_back. These functions should emulate th

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site