采用C++面向对象编程技术(代码中必须有class,不能只是面向过程),开发GUI程序;试设计一选修课程系统完整代码
时间: 2024-03-07 13:50:20 浏览: 21
好的,让我来为您编写一个选修课程系统的完整代码:
```c++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// 课程类
class Course {
public:
Course(string name, int credit, string teacher)
: name(name), credit(credit), teacher(teacher) {}
string getName() { return name; }
int getCredit() { return credit; }
string getTeacher() { return teacher; }
private:
string name;
int credit;
string teacher;
};
// 学生类
class Student {
public:
Student(string name, string id)
: name(name), id(id) {}
string getName() { return name; }
string getId() { return id; }
void addCourse(Course& course) { courses.push_back(course); }
vector<Course> getCourses() { return courses; }
private:
string name;
string id;
vector<Course> courses;
};
// 界面类
class GUI {
public:
void showMenu() {
cout << "1. 添加学生" << endl;
cout << "2. 添加课程" << endl;
cout << "3. 选课" << endl;
cout << "4. 查询选课情况" << endl;
cout << "5. 退出" << endl;
}
void showPrompt(string prompt) {
cout << prompt << endl;
}
Student getStudentInfo() {
string name, id;
cout << "请输入学生姓名:";
cin >> name;
cout << "请输入学号:";
cin >> id;
return Student(name, id);
}
Course getCourseInfo() {
string name, teacher;
int credit;
cout << "请输入课程名称:";
cin >> name;
cout << "请输入课程学分:";
cin >> credit;
cout << "请输入任课教师:";
cin >> teacher;
return Course(name, credit, teacher);
}
void showCourseList(vector<Course> courses) {
cout << "课程列表:" << endl;
for (int i = 0; i < courses.size(); i++) {
Course course = courses[i];
cout << i + 1 << ". " << course.getName() << "(" << course.getCredit() << "学分,任课教师:" << course.getTeacher() << ")" << endl;
}
}
};
// 程序类
class Program {
public:
void run() {
GUI gui;
vector<Student> students;
vector<Course> courses;
while (true) {
gui.showMenu();
int choice;
cin >> choice;
if (choice == 1) {
Student student = gui.getStudentInfo();
students.push_back(student);
gui.showPrompt("添加成功!");
} else if (choice == 2) {
Course course = gui.getCourseInfo();
courses.push_back(course);
gui.showPrompt("添加成功!");
} else if (choice == 3) {
gui.showCourseList(courses);
int courseIndex, studentIndex;
cout << "请选择课程编号:";
cin >> courseIndex;
cout << "请选择学生编号:";
cin >> studentIndex;
if (courseIndex <= 0 || courseIndex > courses.size() || studentIndex <= 0 || studentIndex > students.size()) {
gui.showPrompt("输入有误!");
} else {
Student& student = students[studentIndex - 1];
Course& course = courses[courseIndex - 1];
student.addCourse(course);
gui.showPrompt("选课成功!");
}
} else if (choice == 4) {
int studentIndex;
cout << "请选择学生编号:";
cin >> studentIndex;
if (studentIndex <= 0 || studentIndex > students.size()) {
gui.showPrompt("输入有误!");
} else {
Student& student = students[studentIndex - 1];
vector<Course> courses = student.getCourses();
cout << student.getName() << "选修的课程有:" << endl;
for (int i = 0; i < courses.size(); i++) {
Course course = courses[i];
cout << i + 1 << ". " << course.getName() << endl;
}
}
} else if (choice == 5) {
break;
} else {
gui.showPrompt("输入有误!");
}
}
}
};
int main() {
Program program;
program.run();
return 0;
}
```
这个程序使用了面向对象编程的技术,采用了C++语言开发,实现了一个选修课程系统,包括添加学生、添加课程、选课和查询选课情况等功能。界面使用了命令行方式,用户可以通过命令选择需要执行的功能。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)