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

相关推荐
z落落21 分钟前
C#WinForm控件实战:Panel与单选框动态创建
开发语言·c#
ptc学习者22 分钟前
python 中描述符@property property 大概的样子
开发语言·python
zmzb010324 分钟前
Python课后习题训练记录Day129
开发语言·python
张忠琳32 分钟前
【Go 1.26.4】Golang Map 深度解析
开发语言·后端·golang
Vertira33 分钟前
如何对QT开发的软件进行打包[已解决]
开发语言·qt
AI人工智能+电脑小能手35 分钟前
【大白话说Java面试题 第110题】【并发篇】第10题:CAS 存在哪些问题?
java·开发语言·面试
石一峰69943 分钟前
C 语言函数设计模式实战经验
c语言·开发语言·设计模式
sitellla1 小时前
Pydub:用 Python 处理音频,不写废话
开发语言·python·其他·音视频
xingyuzhisuan1 小时前
缓存命中率提升方案:从 30% 优化至 82% 全流程优化记录
java·开发语言·缓存·ai
郑洁文1 小时前
基于Python的恶意流量监测系统的设计与实现
开发语言·python