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

相关推荐
写代码的【黑咖啡】7 分钟前
Python 中的 Requests 库:轻松进行 HTTP 请求
开发语言·python·http
BD_Marathon7 分钟前
MyBatis各种查询功能
java·开发语言·mybatis
研☆香9 分钟前
JavaScript 特点介绍
开发语言·javascript·ecmascript
Howrun77711 分钟前
虚幻引擎_AController_APlayerController_AAIController
开发语言·c++·游戏引擎·虚幻
曹牧18 分钟前
C#:ToDouble
开发语言·c#
小林rr19 分钟前
深入探索 C++:现代特性、工程实践与性能优化全解
java·c++·性能优化
袁袁袁袁满21 分钟前
Python读取doc文件打印内容
开发语言·python·python读取doc文件
zcfeng53033 分钟前
PHP升级
开发语言·php
m0_7482523836 分钟前
Ruby 模块(Module)的基本概念
开发语言·python·ruby
羊小猪~~39 分钟前
【QT】-- QT基础类
开发语言·c++·后端·stm32·单片机·qt