1、思维导图:
2、模板类的课上练习
cpp
#include <iostream>
#include <stdexcept>
using namespace std;
template <typename T, int MAXSIZE>
class Stack {
private:
T data[MAXSIZE];
int top;
public:
Stack() : top(-1) {}
bool isEmpty() const {
return top == -1;
}
void push(const T& x) {
if (top == MAXSIZE - 1) {
cout<<"stack full"<<endl;
}
data[++top] = x;
}
void pop() {
if (isEmpty()) {
cout<<"stack empty"<<endl;
}
top--;
}
};
int main() {
Stack<int, 10> s;
try {
s.push(1);
s.push(2);
s.push(3);
} catch (const exception& e) {
cout << "Error: " << e.what() << endl;
}
return 0;
}
3、异常处理的代码重新写一遍