C++语言构建一个小型的班级学生健康管理系统,对于每个学生应该统计下述数据:学号、姓名、出生日期、性别、身高、体重、身体状况等信息,并根据班级平均体重来判断该同学体重水平。请抽象出该系统中的类及其之间的关联,分析归纳出类的相关属性和功能并进行代码设计。要求如下: (1)使用UML图表示类的属性方法及类之间的关系 (2)请使用静态成员对学生数量及体重总和进行定义 (3)定义函数显示该同学的体重水平,规则为体重在平均体重上下50%的为正常体重,体重低于平均体重的50%为较轻,体重高于平均体重的50%为较重。 (4)设计方法输出学生健康的统计信息
时间: 2024-04-13 16:30:04 浏览: 104
C++实现学生信息管理系统,卓越课程设计源代码,完美运行!
根据您的需求,我为您设计了一个小型班级学生健康管理系统的类和其之间的关联。以下是UML图和代码设计:
```cpp
#include <iostream>
#include <vector>
class Student {
private:
static int numStudents;
static double totalWeight;
int studentID;
std::string name;
std::string birthDate;
char gender;
double height;
double weight;
public:
Student(std::string name, std::string birthDate, char gender, double height, double weight) {
this->studentID = numStudents++;
this->name = name;
this->birthDate = birthDate;
this->gender = gender;
this->height = height;
this->weight = weight;
totalWeight += weight;
}
static int getNumStudents() {
return numStudents;
}
static double getAverageWeight() {
return totalWeight / numStudents;
}
void displayWeightStatus() {
double averageWeight = getAverageWeight();
double weightPercentDiff = ((weight - averageWeight) / averageWeight) * 100;
if (weightPercentDiff >= -50 && weightPercentDiff <= 50) {
std::cout << name << "'s weight is normal." << std::endl;
} else if (weightPercentDiff < -50) {
std::cout << name << "'s weight is lighter than average." << std::endl;
} else {
std::cout << name << "'s weight is heavier than average." << std::endl;
}
}
static void displayHealthStatistics() {
std::cout << "Total number of students: " << numStudents << std::endl;
std::cout << "Average weight: " << getAverageWeight() << std::endl;
}
};
int Student::numStudents = 0;
double Student::totalWeight = 0.0;
int main() {
std::vector<Student> students;
// 添加学生
students.push_back(Student("Tom", "1999-01-01", 'M', 170.0, 65.0));
students.push_back(Student("Alice", "2000-02-02", 'F', 160.0, 50.0));
students.push_back(Student("Bob", "1998-03-03", 'M', 180.0, 80.0));
// 显示学生体重水平
for (const auto& student : students) {
student.displayWeightStatus();
}
// 显示学生健康统计信息
Student::displayHealthStatistics();
return 0;
}
```
UML图如下:
```
+--------------------------------+
| Student |
+--------------------------------+
| - numStudents: int (static) |
| - totalWeight: double (static) |
| - studentID: int |
| - name: string |
| - birthDate: string |
| - gender: char |
| - height: double |
| - weight: double |
+--------------------------------+
| + Student(name: string, |
| birthDate: string, |
| gender: char, |
| height: double, |
| weight: double) |
| + getNumStudents(): int (static)|
| + getAverageWeight(): double |
| (static) |
| + displayWeightStatus(): void |
| + displayHealthStatistics(): void|
+--------------------------------+
| + main(): int |
+--------------------------------+
```
以上代码定义了一个名为`Student`的类,具有学生的相关属性和功能。其中,`numStudents`和`totalWeight`是静态成员变量,用于统计学生数量和体重总和。构造函数用于初始化学生对象的属性,并更新静态成员变量的值。`getNumStudents()`和`getAverageWeight()`是静态成员函数,用于获取学生数量和平均体重。`displayWeightStatus()`方法根据平均体重判断学生的体重水平,并进行相应输出。`displayHealthStatistics()`方法用于显示学生健康的统计信息。在`main()`函数中,我们创建了几个学生对象,并进行了相应的操作和输出。
希望这个设计满足了您的需求。如果有任何问题,请随时向我提问!
阅读全文