Basically. Use new to create the new array, copy the existing elements over, adjust the capacity, and delete the old array. Make sure the class's internal pointer is pointing to the new one as well.
Yes, if the array is full and needs to be extended. You could make it a separate function if you liked, so the user could extend the capacity manually if they wanted.
If you are allowed to use C++ STL, then a vector class will solve your problem. It will handle auto-expand to take in more items once it reach the limit.
My suggestion was referring to the push function instead.
Yes you do a myCapacity += 10; But some implementations like to do a myCapacity*=2; to give more space to prevent extending of your internal array from occurring too often. If you see the code, the operations are not cheap whenever you do a extend operation. In exchange for wasting some memory, you gain better performance for your Stack class.
this was the way i implemednt your code on push stack, but it didnt work. when i type (for example) 123 as a capacity, then i type 124 as element it says stack full, but it does not increase 10 more capacity
then i type 124 as element it says stack full, but it does not increase 10 more capacity
How do you know it does not increase 10 more capacity ? There is one more code missing from above.
1 2 3 4 5 6 7
temp = new StackElement[myCapacity + 10];
for(int i=0;i<myTop;i++)
temp[i] = myArray[i]; //assume StackElement has proper assignment operator defined
delete [] myArray;
myArray = temp;
myArray[myTop] = value; //add this line in
myCapacity = myCapacity + 10; //change operator a bit here
Still one more thing. myTop wasn't incremented. It looks like it should be incremented before the assignment on line 6. A pre-increment should do it. myArray[++myTop] = value; //add this line in
Edit: Isn't the for loop upper limit one short? myTop = highest index value.
But his code implies 1-based indexing and that is increment myTop FIRST and then assignment. Yet where he check it is if (myTop < myCapacity - 1) which seem to implies he is doing the check based on 0-based indexing approach but seems funny why < myCapacity - 1?
If 0-based the check should be if (myTop < myCapacity)
If 1-based the check should be if (myTop <= myCapacity)
So I am not sure the OP want 0-based or 1-based or he want BOTH ?!?!