This question is about improving my C++ coding skills. I was asked to implement a simple static integer stack in C++ as an assignment. I've come up with the following code:
class myStaticIntStack
{
int stackSize;
int storedElements;
int *elements;
public:
myStaticIntStack();
myStaticIntStack( int aNumber );
~myStaticIntStack();
int peek();
int pop();
void push( int element );
};
myStaticIntStack::myStaticIntStack()
{
this->stackSize = 1;
this->elements = new int(0);
this->storedElements = 0;
}
myStaticIntStack::myStaticIntStack( int stackSize )
{
this->stackSize = stackSize;
this->elements = new int[ stackSize ];
this->storedElements = 0;
}
myStaticIntStack::~myStaticIntStack()
{
if( this->elements != NULL )
{
if( stackSize > 1 )
delete[] this->elements;
else
delete this->elements;
}
}
void myStaticIntStack::push( int newElement )
{
if( this->storedElements == this->stackSize )
cout << "Stack is full, you must POP an element before PUSHing a new one!" << endl;
else
{
this->elements[ (this->stackSize - 1) - this->storedElements ] = newElement;
this->storedElements++;
}
}
int myStaticIntStack::pop()
{
if( this->storedElements == 0 )
{
cout << "Stack is empty, you must PUSH an element before POPping one!" << endl;
return -1;
}
else
{
storedElements--;
return this->elements[ (this->stackSize - 1) - this->storedElements ];
}
}
int myStaticIntStack::peek()
{
if( this->storedElements == 0 )
{
cout << "Stack is empty, you must PUSH an element before PEEKing one!" << endl;
return -1;
}
else
{
return this->elements[ this->stackSize - this->storedElements ];
}
}
int main()
{
myStaticIntStack aStack(3);
cout << "Popped Element: " << aStack.pop() << endl;
aStack.push(1);
cout << "Stack Top is: " << aStack.peek() << endl;
aStack.push(2);
cout << "Stack Top is: " << aStack.peek() << endl;
aStack.push(3);
cout << "Stack Top is: " << aStack.peek() << endl;
aStack.push(4);
cout << "Stack Top is: " << aStack.peek() << endl;
cout << "Popped Element: " << aStack.pop() << endl;
cout << "Stack Top is: " << aStack.peek() << endl;
cout << "Popped Element: " << aStack.pop() << endl;
cout << "Stack Top is: " << aStack.peek() << endl;
cout << "Popped Element: " << aStack.pop() << endl;
cout << "Stack Top is: " << aStack.peek() << endl;
return 0;
}
The code is compiling and running correctly, this is standard output:
Stack is empty, you must PUSH an element before POPping one!
Popped Element: -1
Stack Top is: 1
Stack Top is: 2
Stack Top is: 3
Stack is full, you must POP an element before PUSHing a new one!
Stack Top is: 3
Popped Element: 3
Stack Top is: 2
Popped Element: 2
Stack Top is: 1
Popped Element: 1
Stack is empty, you must PUSH an element before PEEKing one!
Stack Top is: -1
However I was given a B due to the following reasons:
- There was a better way of implementing it.
- I didn't manage well the case in which I'm trying to POP an element from an empty stack, since it's confusing that
pop()
returns a value even if Stack is empty.
Can you please help me understand how I could improve my code?