Permalink
Cannot retrieve contributors at this time
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?
4003CEM-templates_orig/stack.h
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
58 lines (47 sloc)
1.11 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |