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

相关推荐
她说..44 分钟前
Java 默认值设置方式
java·开发语言·后端·springboot
忧郁的紫菜1 小时前
基础实现:单篇 Markdown 转 Word
开发语言·c#·word
甜美的小天鹅1 小时前
Swifter C#之inline还是不inline,这是个问题
开发语言·c#
小短腿的代码世界1 小时前
Qt Bluetooth源码深度解析:从HCI协议到跨平台API的完整架构
开发语言·qt·架构
_olone1 小时前
Luogu P2704 [NOI2001] 炮兵阵地
c++·算法·状压dp
c238562 小时前
互斥锁高频面试题全解:从基础概念到底层实现,一文通关
java·c++·面试·职场和发展
北冥you鱼2 小时前
Go 语言读取链上数据:从基础到实战
开发语言·后端·golang
nianniannnn2 小时前
c++复习自存--继承
开发语言·c++
叩码以求索3 小时前
经典算法实例分析:写字符串需要的行数
开发语言·javascript·ecmascript
我是唐青枫3 小时前
Kotlin 运算符重载详解:为什么 a += b 有时改对象,有时换对象?
开发语言·kotlin