指向学生类的指针

来源:互联网 发布:数据透视表显示合计 编辑:程序博客网 时间:2024/06/06 01:46
  1. #include <iostream>  
  2. using namespace std;  
  3.   
  4. class Student  
  5. {  
  6. public:  
  7.     Student(int x = 0,double y = 0.0):num(x),score(y) {}  
  8.     void input();  
  9.     void output();  
  10.     int get_num() {return num;}  
  11.     double get_score() {return score;}  
  12. private:  
  13.     int num;  
  14.     double score;  
  15. };  
  16.   
  17. void Student::input()  
  18. {  
  19.     cin >> num >> score;  
  20. }  
  21.   
  22. void Student::output()  
  23. {  
  24.     cout << num << "--" << score << endl;  
  25. }  
  26.   
  27. int max(Student *arr)  
  28. {  
  29.     int temp;  
  30.     double tscore = arr[0].get_score();  
  31.     for(int i = 0; i < 5; ++i)  
  32.         if(tscore < arr[i].get_score())  
  33.         {  
  34.             tscore = arr[i].get_score();  
  35.             temp = arr[i].get_num();  
  36.         }  
  37.     return temp;  
  38. }  
  39.   
  40. int main()  
  41. {  
  42.     Student stud[5] =  
  43.     {  
  44.         Student(101,78.5),Student(102,85.5),Student(103,100),  
  45.         Student(104,98.5),Student(105,95.5)  
  46.     };  
  47.   
  48.     for(int i = 0; i < 5; i+=2)//输出1、3、5的成绩  
  49.         stud[i].output();  
  50.   
  51.     cout << "5个学生中成绩最高者的学号为: " << max(stud);  
  52.   
  53.     return 0;  
  54. }  
0 0