C++编程:派生类、对象数组与函数结合找出成绩最高学生信息
以下是使用 C++实现的代码: ```cpp #include <iostream> #include <string> class CPerson { public: std::string name; std::string gender; }; class CStudent : public CPerson { public: int studentId; int cLanguageScore; int cPlusPlusScore; int dataStructureScore; }; // 找到成绩最高的学生 void max(CStudent* students, int numStudents) { int maxScore = 0; int maxIndex = 0; for (int i = 0; i < numStudents; i++) { int totalScore = students[i].cLanguageScore + students[i].cPlusPlusScore + students[i].dataStructureScore; 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].cLanguageScore << std::endl; std::cout << "C++成绩: " << students[maxIndex].cPlusPlusScore << std::endl; std::cout << "数据结构成绩: " << students[maxIndex].dataStructureScore << std::endl; } int main() { CStudent students[5] = { {"张三", "男", 1, 80, 90, 70}, {"李四", "女", 2, 70, 80, 90}, {"王五", "男", 3, 90, 70, 80}, {"赵六", "女", 4, 80, 70, 90}, {"孙七", "男", 5, 70, 90, 80} }; max(students, 5); return 0; } ``` 额外需要注意的逻辑点: 1. 要确保输入的学生成绩是合理的数值范围。 2. 处理可能出现多个学生成绩相同且最高的情况。 3. 考虑添加错误处理,比如学生数组为空的情况。 ######[AI写代码神器 | 574点数解答 | 2024-11-23 17:35:32]
- C 语言:快速实现 5 个学生信息录入、按成绩升序排序并输出!(字节豆包 | 373点数解答 | 2024-10-30 00:37:20)410
- C语言实现:输入5名学生信息并按成绩升序排序输出(字节豆包 | 354点数解答 | 2024-10-30 18:45:58)354
- C语言:输入5个学生信息,按成绩升序排序并输出(字节豆包 | 373点数解答 | 2024-10-31 19:49:58)345
- C语言:输入5名学生信息并按成绩升序排序输出(字节豆包 | 326点数解答 | 2025-01-01 20:11:47)340
- C 语言实现链表操作:创建、输出、删除及插入功能详解(字节豆包 | 534点数解答 | 2024-12-09 21:33:48)338
- C语言实现链表创建、输出、删除与插入操作的完整代码及注意要点(字节豆包 | 609点数解答 | 2024-12-18 12:18:21)358
- 用 Pandas 深度剖析数据:从读取到统计分析全流程实战(GPT | 642点数解答 | 2024-06-30 15:39:49)355
- 用 Pandas 深入分析招聘数据:从数据读取到可视化全流程解析(GPT | 1539点数解答 | 2024-06-30 15:56:50)367
- 递归实现:将对象所有属性值转换为字符串!含代码示例(字节豆包 | 143点数解答 | 2024-11-08 10:25:12)376
- 递归实现:将对象所有属性值转换为字符串类型(字节豆包 | 142点数解答 | 2024-11-11 21:06:15)254
- 递归实现:将对象所有属性值转换为字符串,复杂嵌套也不怕!(阿里通义 | 467点数解答 | 2024-11-12 09:07:17)262
- 成绩等级统计:20个60 - 100分成绩的等级数量揭秘(字节豆包 | 219点数解答 | 2025-04-09 10:20:32)219