c++程序设计活动策划类设计

2023-12-26 18:55:28
#include <iostream>
#include <string>

using namespace std;

class Activity {
public:
    string name;
    string date;

    Activity(string n="", string d = "") : name(n), date(d) {}

};

class ActivityManager {
private:
    static const int MAX = 10; // 设置最大活动数
    Activity activities[MAX];
    int num;

public:
    ActivityManager():num(0){}

    void addActivity(const Activity& activity) {
        if (num < MAX) {
            activities[num++] = activity;
            cout << "活动增加: " << activity.name << endl;
        }
        else {
            cout << "活动已经超过最大数量!" << endl;
        }
    }

    void displayActivities() {
        if (num == 0) {
            cout << "活动为空" << endl;
        }
        else {
            cout << "活动:" << endl;
            for (int i = 0; i < num; ++i) {
                cout << "活动名: " << activities[i].name << ", 活动日期: " << activities[i].date << endl;
            }
        }
    }
};

int main() {
    ActivityManager manager;

    // 添加活动
    Activity activity1("活动一", "2023-01-01");
    manager.addActivity(activity1);

    Activity activity2("活动二", "2023-02-15");
    manager.addActivity(activity2);

    // 显示所有活动
    manager.displayActivities();

    return 0;
}

文章来源:https://blog.csdn.net/toptopniba/article/details/135226294
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。