定义一个组合类课程类Lesson,其数据成员有:课号id,课程名name,任课老师teacher,选课学生s,其中学生信息定义称学生类的数组。成员函数有:AddStudent增加选课学生信息,GetPassRate计算课程的及格率,ShowInfo显示课程信息。学生类包括学号、姓名及该门课的成绩
时间: 2024-03-07 20:51:56 浏览: 173
好的,这是一个关于组合类课程类Lesson的问题,下面是代码实现:
```c++
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
Student() {}
Student(int id, string name, double grade) : id(id), name(name), grade(grade) {}
int getId() { return id; }
string getName() { return name; }
double getGrade() { return grade; }
private:
int id;
string name;
double grade;
};
class Lesson {
public:
Lesson() {}
Lesson(int id, string name, string teacher) : id(id), name(name), teacher(teacher), count(0), sumGrade(0) {}
void AddStudent(int id, string name, double grade) {
Student student(id, name, grade);
students[count++] = student;
sumGrade += grade;
}
double GetPassRate() {
if (count == 0) return 0;
int passCount = 0;
for (int i = 0; i < count; i++) {
if (students[i].getGrade() >= 60) {
passCount++;
}
}
return (double)passCount / count;
}
void ShowInfo() {
cout << "课程编号:" << id << endl;
cout << "课程名称:" << name << endl;
cout << "任课教师:" << teacher << endl;
cout << "选课学生信息:" << endl;
for (int i = 0; i < count; i++) {
cout << "学号:" << students[i].getId() << " 姓名:" << students[i].getName() << " 成绩:" << students[i].getGrade() << endl;
}
cout << "及格率:" << GetPassRate() << endl;
}
private:
int id;
string name;
string teacher;
Student students[100];
int count;
double sumGrade;
};
int main() {
Lesson lesson(1, "计算机程序设计", "张三");
lesson.AddStudent(101, "小明", 80);
lesson.AddStudent(102, "小红", 70);
lesson.AddStudent(103, "小刚", 90);
lesson.ShowInfo();
return 0;
}
```
这个程序中,我们定义了两个类:Student和Lesson,其中Student类表示一个学生,包括学号、姓名及该门课的成绩;Lesson类表示一门课程,包括课号id、课程名name、任课老师teacher、选课学生s,其中学生信息定义称学生类的数组。成员函数有:AddStudent增加选课学生信息,GetPassRate计算课程的及格率,ShowInfo显示课程信息。
阅读全文