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

相关推荐
君顾124 分钟前
上海24小时自助健身房系统开发实战指南:从架构设计到落地部署
java·开发语言·健身房
香菜TTT1 小时前
大模型上下文协议(MCP):AI 应用的“USB-C”接口技术
开发语言·人工智能·经验分享
钓鱼的肝1 小时前
梳理(1-5)
c++·经验分享·笔记·算法·青少年编程
aramae1 小时前
模拟实现strlen()函数 (C语言)
c语言·开发语言·后端
jimy12 小时前
readelf 查看“派生类”的vtable符号
开发语言·c++
蒸蒸yyyyzwd3 小时前
cpp 选手秋招学习笔记 day37 面经学习
c++·八股
ctlover3 小时前
排序与查找算法详解
开发语言·python
YYYing.3 小时前
【C++进阶系列 (七)】C++ RTTI 深度剖析:从 typeid 到 dynamic_cast 的底层之旅
开发语言·c++·c/c++·rtti
nvvas4 小时前
Java 入门(四):数组——数据的集合管理
java·开发语言
钓鱼的肝4 小时前
csp-j-s总结(2)
c++·经验分享·笔记·算法·青少年编程