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

相关推荐
Tyler_TXZ11 分钟前
C++C语言之——二叉树
c语言·开发语言·数据结构·c++·二叉树
不会代码的小猴35 分钟前
C++新增关键字
开发语言·c++·笔记
yaoxin52112339 分钟前
497. Java 反射 - 使用反射读取注解
java·开发语言·python
2019一路前行1 小时前
Python 函数式编程
开发语言·python
liulilittle2 小时前
并发与内存安全术语:定义、分类与关联
c++·安全·并发·术语
_wyt0012 小时前
从图到树:9道洛谷基础题带你入门树形结构
c++·
冯汉栩2 小时前
Swift Control View,Label渐变颜色(源码)
开发语言·ios·swift
Titan20243 小时前
Linux网络基础知识
linux·服务器·网络·c++·学习
从小就看凹凸曼^o^3 小时前
Python基础5 - 字典与集合:(1)字典
开发语言·python
Herbert_hwt3 小时前
第七章 Java深入理解枚举类型
java·开发语言·算法