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

相关推荐
2401_884602272 小时前
程序人生-Hello’s P2P
c语言·c++
初中就开始混世的大魔王2 小时前
2 Fast DDS Library概述
c++·中间件·信息与通信
MediaTea2 小时前
Python:collections.Counter 常用函数及应用
开发语言·python
LawrenceLan2 小时前
37.Flutter 零基础入门(三十七):SnackBar 与提示信息 —— 页面反馈与用户交互必学
开发语言·前端·flutter·dart
李昊哲小课2 小时前
Python json模块完整教程
开发语言·python·json
易醒是好梦2 小时前
Python flask demo
开发语言·python·flask
娇娇yyyyyy3 小时前
C++基础(6):extern解决重定义问题
c++
Neteen3 小时前
【数据结构-思维导图】第二章:线性表
数据结构·c++·算法