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

相关推荐
清水白石0082 分钟前
构建企业级 Python 服务:配置、日志、指标与追踪的稳健之道
开发语言·python·elasticsearch
lsx2024062 分钟前
特效(Effect)
开发语言
那小子、真烦11 分钟前
Hermes Agent Chat 方法分析
java·开发语言
爱喝水的鱼丶14 分钟前
SAP-ABAP:变量、常量、结构与内表声明(10篇博客合集) 第六篇:ABAP 7.40+新特性:声明语法的简化写法与兼容注意事项
运维·服务器·开发语言·学习·算法·sap·abap
上海合宙LuatOS15 分钟前
Air8000低功耗指南
开发语言·物联网·php·lua
happymaker062624 分钟前
SpringBoot使用Thymeleaf模板引擎,前端的基本语法
开发语言·python
01_ice27 分钟前
Java抽象类和接口
java·开发语言
RuiZN1 小时前
UE5 UObject类详解
c++·ue5
小糯米6011 小时前
C语言 自定义类型:结构体 与 联合体
c语言·开发语言·数据结构
jieyucx1 小时前
Go 语言 JSON 序列化与反序列化
开发语言·golang·json·序列化