用定义一个课程类 course,要求所有数据成员定义成 private,理解类的定义和使用。 (1) 使用#define 定义课程容量 MAXCAP (2) 定义课程类的属性:课程号 id、课程名 name、教室 loc(可选)、课程容量 cap、 教师 tea、学生 stu、已有学生数量 stucnt 和节次 time(可选); (提示:教室 loc 和教师 tea 使用前面定义类的对象,学生 stu 使用学生类数组指 针) (3) 定义总课程数 crscnt (提示:使用静态变量,在构造函数中加 1) (4) 定义无参和有参的构造函数;(提示:初始化 stu 数组,长度为 cap) (5) 定义拷贝构造函数(提示:初始化 stu 数组,长度为 cap,并将参数的数据成员值 复制到数据成员中) (6) 定义析构函数,delete 释放 stu 数组资源,输出课程的简单信息; (7) 定义课程类的功能: 增加选课的学生 bool addstu(const student &s);(提示:给数组元素赋值,增加 stucnt) 录入课程内所有学生的分数 bool setcoursescore();(提示:访问 stu 数组,通过 cin 录入所有选课学生的分数) 获取班级平均分 double averagescore(); 求班级最高分的同学 student topstu(); 成绩排序 void sort_bubble(); 输出班级所有同学的信息和成绩 void stuinfo(); 输出班级的课程信息 void courseinfo(); (8) 在 main 函数,继续声明两个 course 的变量,使用这些成员函数进行计算。
时间: 2023-12-03 07:47:11 浏览: 143
以下是课程类 course 的代码实现:
```cpp
#include <iostream>
#include <cstring>
#include "student.h" // 学生类的头文件
#define MAXCAP 50 // 定义课程容量
class course {
private:
int id; // 课程号
char name[20]; // 课程名
char loc[10]; // 教室
int cap; // 课程容量
char tea[20]; // 教师
student *stu; // 学生指针
int stucnt; // 已有学生数量
int time; // 节次
static int crscnt; // 总课程数
public:
course() : id(0), cap(MAXCAP), stucnt(0), time(0) {
stu = new student[cap]; // 初始化 stu 数组
crscnt++; // 总课程数加 1
}
course(int _id, const char *_name, const char *_loc, int _cap, const char *_tea, int _time)
: id(_id), cap(_cap), stucnt(0), time(_time) {
strcpy(name, _name);
strcpy(loc, _loc);
strcpy(tea, _tea);
stu = new student[cap]; // 初始化 stu 数组
crscnt++; // 总课程数加 1
}
course(const course &c) : id(c.id), cap(c.cap), stucnt(c.stucnt), time(c.time) {
strcpy(name, c.name);
strcpy(loc, c.loc);
strcpy(tea, c.tea);
stu = new student[cap]; // 初始化 stu 数组
for (int i = 0; i < stucnt; i++) {
stu[i] = c.stu[i]; // 将参数的数据成员值复制到数据成员中
}
crscnt++; // 总课程数加 1
}
~course() {
delete[] stu; // 释放 stu 数组资源
std::cout << "Course " << id << " has been deleted." << std::endl;
}
bool addstu(const student &s) {
if (stucnt >= cap) { // 判断是否已满
return false;
}
stu[stucnt++] = s; // 给数组元素赋值,增加 stucnt
return true;
}
bool setcoursescore() {
for (int i = 0; i < stucnt; i++) {
std::cout << "Please input the score of student " << i + 1 << ": ";
std::cin >> stu[i].score; // 通过 cin 录入所有选课学生的分数
}
return true;
}
double averagescore() {
double sum = 0;
for (int i = 0; i < stucnt; i++) {
sum += stu[i].score;
}
return sum / stucnt; // 求班级平均分
}
student topstu() {
int maxidx = 0;
for (int i = 1; i < stucnt; i++) {
if (stu[i].score > stu[maxidx].score) {
maxidx = i;
}
}
return stu[maxidx]; // 求班级最高分的同学
}
void sort_bubble() {
for (int i = 0; i < stucnt - 1; i++) {
for (int j = 0; j < stucnt - 1 - i; j++) {
if (stu[j].score < stu[j + 1].score) {
student temp = stu[j];
stu[j] = stu[j + 1];
stu[j + 1] = temp;
}
}
}
}
void stuinfo() {
for (int i = 0; i < stucnt; i++) {
std::cout << "Student " << i + 1 << ": ";
stu[i].printinfo(); // 输出班级所有同学的信息和成绩
}
}
void courseinfo() {
std::cout << "ID: " << id << std::endl;
std::cout << "Name: " << name << std::endl;
std::cout << "Location: " << loc << std::endl;
std::cout << "Capacity: " << cap << std::endl;
std::cout << "Teacher: " << tea << std::endl;
std::cout << "Time: " << time << std::endl; // 输出班级的课程信息
}
static int getcrscnt() { // 获取总课程数
return crscnt;
}
};
int course::crscnt = 0; // 初始化总课程数为 0
```
在 main 函数中,可以使用以下方式声明两个 course 的变量,并使用成员函数进行计算:
```cpp
int main() {
// 声明两个 course 的变量
course c1(1, "Math", "101", 50, "Mr. Wang", 2);
course c2(2, "English", "102", 40, "Ms. Li", 3);
// 添加学生并录入分数
c1.addstu(student(1, "Tom", 18));
c1.addstu(student(2, "Jerry", 19));
c1.addstu(student(3, "Alice", 20));
c1.setcoursescore();
// 计算班级平均分和班级最高分的同学
std::cout << "The average score of class 1 is: " << c1.averagescore() << std::endl;
std::cout << "The top student of class 1 is: ";
c1.topstu().printinfo();
// 对成绩进行排序并输出学生信息
c1.sort_bubble();
std::cout << "The sorted students of class 1 are: " << std::endl;
c1.stuinfo();
// 输出课程信息和总课程数
std::cout << "The information of course 1 is: " << std::endl;
c1.courseinfo();
std::cout << "The total number of courses is: " << course::getcrscnt() << std::endl;
return 0;
}
```
阅读全文
相关推荐


















