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

相关推荐
这儿有一堆花28 分钟前
python视觉开发
开发语言·python
oioihoii1 小时前
现代C++:一场静默的革命,告别“C with Classes”
c语言·jvm·c++
Jonathan Star1 小时前
JavaScript 中,原型链的**最顶端(终极原型)只有一个——`Object.prototype`
开发语言·javascript·原型模式
普通网友1 小时前
C++中的组合模式
开发语言·c++·算法
q***61501 小时前
PHP进阶-在Ubuntu上搭建LAMP环境教程
开发语言·ubuntu·php
Dneccc1 小时前
Qt5配置MSVC2017
开发语言·qt
江公望2 小时前
Qt QByteArray类型,10分钟讲清楚
开发语言·c++·qt
小灰灰搞电子2 小时前
Qt Sensors 传感器框架详解
开发语言·qt
LNN20222 小时前
Qt 5.8 中的 Qt Test:轻松实现自动化测试
开发语言·qt
2501_941111462 小时前
C++中的组合模式变体
开发语言·c++·算法