Skip to content
Permalink
c95fb77080
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
58 lines (47 sloc) 1.11 KB
#ifndef STACK_H
#define STACK_H
#include <exception>
#include <memory>
template<typename T>
class Stack
{
private:
const int maxSize;
int numValues;
std::unique_ptr<T[]> stack;
public:
class Full: public std::exception
{
public:
virtual const char* what() const throw();
};
class Empty: public std::exception
{
public:
virtual const char* what() const throw();
};
/** Initialise the stack, maxSize is the maximum number of
values that can be stored in the stack at any time */
Stack( const int _maxSize );
// etcetera
};
template<typename T>
const char* Stack<T>::Full::what() const throw()
{
return "Stack is full";
}
template<typename T>
const char* Stack<T>::Empty::what() const throw()
{
return "Stack is empty";
}
template<typename T>
Stack<T>::Stack( const int _maxSize ) : maxSize(_maxSize)
{
/* this is going to dynamically allocate an array of chars
I'm doing it with a smart pointer to be absolutely certain
there's no memory leak */
stack = std::make_unique<T[]>( maxSize );
numValues = 0;
}
#endif