// @topic T060830 Stack of integers v3
// @brief Stack class with overloaded assignment operator
#include "Stack.h"
Stack::Stack()
{
std::cout << __FUNCTION__ << " invoked\n";
stack_memory = new int[ DEFAULT_CAPACITY ];
capacity = DEFAULT_CAPACITY;
top_pos = 0;
}
Stack::Stack( int initial_capacity )
{
std::cout << __FUNCTION__ << " invoked\n";
stack_memory = new int[initial_capacity];
capacity = initial_capacity;
top_pos = 0;
}
// copy constructor
Stack::Stack(Stack const& other)
{
std::cout << __FUNCTION__ << " invoked\n";
stack_memory = new int[other.capacity];
for (int idx = 0; idx < other.size(); ++idx) {
stack_memory[idx] = other.stack_memory[idx];
}
}
Stack& Stack::operator= (Stack const& other)
{
if (this == &other) {
// guard against self-assignment
return *this;
}
delete[] stack_memory;
stack_memory = new int[other.capacity];
for (int idx = 0; idx < other.size(); ++idx) {
stack_memory[idx] = other.stack_memory[idx];
}
return *this;
}
Stack::~Stack()
{
std::cout << __FUNCTION__ << " invoked\n";
delete[] stack_memory;
}
void Stack::push( int value )
{
stack_memory[ top_pos ] = value;
++top_pos;
}
void Stack::pop()
{
--top_pos;
}
int Stack::top() const
{
return stack_memory[ top_pos ];
}
int& Stack::top()
{
return stack_memory[ top_pos ];
}
int Stack::size() const
{
return top_pos;
}