Declaration: related images and content based on www.bilibili.com/video/av886…
Written in separate Documents (1)
Classes are declared and defined separately
Stack.h
#pragma once
const int MAX_SIZE = 100;
template <class T>
class Stack {
private:
int top;
T* point;
int size;
public:
Stack();
Stack(int s);
~Stack();
void push(T elem);
T pop();
T getTop();
bool isEmpth();
bool isFull();
void setNULL();
class Full {};
class Empty {};
};
//typedef Stack<char> CharStack;
Copy the code
Stack.cpp
#include "Stack.h" template <class T> Stack<T>::Stack() { top = -1; size = MAX_SIZE; point = new T[MAX_SIZE]; } template <class T> Stack<T>::Stack(int s) { top = -1; size = s; point = new T[size]; } template<class T> Stack<T>::~Stack() { delete[] point; } template<class T> void Stack<T>::push(T ch) { if (isFull()) throw Stack<T>::Full(); Else point[++top] = ch; } template<class T> T Stack<T>::pop() { if (isEmpth()) throw Stack<T>::Empty(); else return point[top--]; // Notice top--, if return is separated from top--, } template<class T> T Stack<T>::getTop() {if (isEmpth()) throw Stack<T>::Empty(); else return point[top]; } template<class T> bool Stack<T>::isEmpth() { if (top == -1) return true; return false; } template<class T> bool Stack<T>::isFull() { if (top == size - 1) return true; return false; } template<class T> void Stack<T>::setNULL() { top = -1; } template class Stack<char>; // Class template sub file must add this sentenceCopy the code
main.cpp
#include<iostream> #include"Stack.h" using namespace std; int main() { Stack<char> s1(2); / / or CharStack s1 (2); char ch; try { s1.push('a'); s1.push('a'); s1.push('a'); } catch (Stack<char>::Empty) {// or catch (CharStack::Empty) cout << "Empty "; } catch (char>::Full) {// or catch (CharStack::Full) cout << "Full "; } return 0; }Copy the code
Written in separate Documents (2)
Class declarations and definitions are placed together in the.hpp file (common practice)
Stack.hpp
const int MAX_SIZE = 100; template <class T> class Stack { private: int top; T* point; int size; public: Stack(); Stack(int s); ~Stack(); void push(T elem); T pop(); T getTop(); bool isEmpth(); bool isFull(); void setNULL(); class Full {}; class Empty {}; }; //typedef Stack<char> CharStack; template <class T> Stack<T>::Stack() { top = -1; size = MAX_SIZE; point = new T[MAX_SIZE]; } template <class T> Stack<T>::Stack(int s) { top = -1; size = s; point = new T[size]; } template<class T> Stack<T>::~Stack() { delete[] point; } template<class T> void Stack<T>::push(T ch) { if (isFull()) throw Stack<T>::Full(); else point[++top] = ch; } template<class T> T Stack<T>::pop() { if (isEmpth()) throw Stack<T>::Empty(); else return point[top--]; } template<class T> T Stack<T>::getTop() { if (isEmpth()) throw Stack<T>::Empty(); else return point[top]; } template<class T> bool Stack<T>::isEmpth() { if (top == -1) return true; return false; } template<class T> bool Stack<T>::isFull() { if (top == size - 1) return true; return false; } template<class T> void Stack<T>::setNULL() { top = -1; } //template class Stack<char>; // HPP files can be omittedCopy the code
main.cpp
#include<iostream> #include" stack.hpp" int main() { Stack<char> s1(2); / / or CharStack s1 (2); char ch; try { s1.push('a'); s1.push('a'); s1.push('a'); } catch (Stack<char>::Empty) {// or catch (CharStack::Empty) cout << "Empty "; } catch (char>::Full) {// or catch (CharStack::Full) cout << "Full "; } return 0; }Copy the code