C++作业

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、异常处理的代码重新写一遍

相关推荐
Wang's Blog2 分钟前
Java 项目实战: 外卖平台优化-YApi接口管理平台与文档导入导出
java·开发语言·yapi
jimy19 分钟前
基类没有虚析构函数,基类指针删除派生类对象,行为是未定义的
开发语言·c++
迅猛龙办公室18 分钟前
python获取星期字符串
开发语言·python
xlq2232240 分钟前
全面复习2
c++
无名猿41 分钟前
移动构造与移动赋值:把资源偷过来
c++·性能优化·内存管理·现代c++·语法基础
泡海椒1 小时前
JQuick-Excel dateFormat 转换实战:日期值与 FORMAT 显示格式的边界
开发语言·python·excel
All for pursuit.1 小时前
【回溯-7】494.目标和
数据结构·c++·算法·leetcode
白杨尚青1 小时前
C++入门篇(十三):vector(上)——动态数组:构造、空间增长与迭代器失效
开发语言·c++·笔记·算法·stl
江江爱编程2 小时前
java零基础保姆级教学01
java·开发语言
沐晓时光2 小时前
C语言入门,深入理解指针(2)
c语言·开发语言