用C++编写社团管理系统的模板
时间: 2024-06-14 17:05:59 浏览: 131
以下是一个用C++编写的社团管理系统的模板:
```cpp
#include <iostream>
#include <vector>
using namespace std;
// 学生类
class Student {
private:
string name;
int age;
public:
Student(string n, int a) {
name = n;
age = a;
}
string getName() {
return name;
}
int getAge() {
return age;
}
};
// 社团类
class Club {
private:
string name;
vector<Student> members;
public:
Club(string n) {
name = n;
}
string getName() {
return name;
}
void addMember(Student s) {
members.push_back(s);
}
void removeMember(Student s) {
for (int i = 0; i < members.size(); i++) {
if (members[i].getName() == s.getName() && members[i].getAge() == s.getAge()) {
members.erase(members.begin() + i);
break;
}
}
}
void displayMembers() {
cout << "Club: " << name << endl;
cout << "Members: " << endl;
for (int i = 0; i < members.size(); i++) {
cout << "Name: " << members[i].getName() << ", Age: " << members[i].getAge() << endl;
}
}
};
int main() {
// 创建社团对象
Club club("Programming Club");
// 创建学生对象
Student student1("Alice", 20);
Student student2("Bob", 21);
// 添加学生到社团
club.addMember(student1);
club.addMember(student2);
// 显示社团成员
club.displayMembers();
// 删除学生
club.removeMember(student1);
// 再次显示社团成员
club.displayMembers();
return 0;
}
```
阅读全文