Problem

What is wrong if line 79 in Listing ImprovedStack.h is replaced by delete old;LISTING Impr...

What is wrong if line 79 in Listing ImprovedStack.h is replaced by delete old;

LISTING ImprovedStack.h

1 #ifndef IMPROVEDSTACK_H2 #define IMPROVEDSTACK_H34 template <typename T>5 class Stack6 {7 public: 8  Stack();9  Stack(const Stack&);10 ~Stack();11 bool empty() const ;12 T peek() const;13 void push(T value);14 T pop();15 int getSize() const;1617 private: 18  T* elements;19  int size;20  int capacity;21  void ensureCapacity();22 };2324 template <typename T>25 Stack::Stack(): size(0), capacity(16)26 {27 elements = new T[capacity];28 }2930 template 31 Stack::Stack(const Stack& stack)32 {33 elements = new T[stack.capacity];34 size = stack.size;35 capacity = stack.capacity;36 for (int i = 0 ; i < size; i++)37 {38  elements[i] = stack.elements[i];39 }40 }4142 template 43 Stack::~Stack()44 {45 delete [] elements;46 }4748 template 49 bool Stack::empty() const50 {51 return size == 0 ;52 }5354 template 55 T Stack::peek() const 56 {57 return elements[size - 1];58 }5960 template 61 void Stack::push(T value)62 {63 ensureCapacity();64 elements[size++] = value;65 }6667template 68 void Stack::ensureCapacity()69 {70 if (size >= capacity)71 {72 T* old = elements;73 capacity = 2 * size;74 elements = new T[size * 2];7576 for (int i = 0 ; i < size; i++)77 elements[i] = old[i];7879 delete [] old;80 }81 }8283template 84 T Stack::pop()85 {86 return elements[−−size];87 }8889template 90 int Stack::getSize() const91 {92 return size;93 }9495 #endif

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT