酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

实验题目:创建一个cperson类并派生一个学生类cstudent,并建立一个学生对象数组,内放5个学生数据(姓名,性别,学号,c语言成绩,c++成绩,数据结构成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找到5个学生中成绩最高者,并输出该学生的全部信息。 具体要求如下: 1. 绘制类图、类描述图、程序流程图。

以下是为您编写的 C++代码示例: ```cpp #include <iostream> #include <string> class CPerson { public: std::string name; std::string gender; }; class CStudent : public CPerson { public: int studentId; double cScore; double cppScore; double dataStructScore; }; // 用于比较成绩找出最高者的函数 void max(CStudent* students, int numStudents) { double maxScore = students[0].cScore + students[0].cppScore + students[0].dataStructScore; int maxIndex = 0; for (int i = 1; i < numStudents; i++) { double totalScore = students[i].cScore + students[i].cppScore + students[i].dataStructScore; if (totalScore > maxScore) { maxScore = totalScore; maxIndex = i; } } // 输出成绩最高者的全部信息 std::cout << "姓名: " << students[maxIndex].name << std::endl; std::cout << "性别: " << students[maxIndex].gender << std::endl; std::cout << "学号: " << students[maxIndex].studentId << std::endl; std::cout << "C 语言成绩: " << students[maxIndex].cScore << std::endl; std::cout << "C++成绩: " << students[maxIndex].cppScore << std::endl; std::cout << "数据结构成绩: " << students[maxIndex].dataStructScore << std::endl; } int main() { CStudent students[5] = { {"张三", "男", 1, 80, 90, 70}, {"李四", "女", 2, 75, 85, 80}, {"王五", "男", 3, 90, 70, 85}, {"赵六", "女", 4, 85, 75, 90}, {"孙七", "男", 5, 70, 80, 75} }; max(students, 5); return 0; } ``` 对于这段代码,需要注意以下几点逻辑: 1. 确保在计算总成绩时,正确相加各项成绩。 2. 注意在比较成绩时,准确判断最大值。 3. 处理好输入学生数据的合理性和有效性。 [2024-11-25 01:17:13 | AI写代码神器 | 588点数解答]

相关提问