Design a student class student, the data members include the number (num) and the score (score), the member function according to the needs of the self-design (recommended to be equipped with the required set, get function, as well as the necessary input or output, the given code can also find the need for member functions of the thread). In the main function, to do this:
Set up an array of objects that, through initialization, sets the data for 5 students, requiring:
The pointer points to the first element of the array, outputting the information of the 1th, 3, 5 students;
Design a function int max (Student *arr), use a pointer to the object as a function parameter, and in the Max function find the top 5 students, and return the value of their study number.
/** Copyright (c) 2015, Yantai University School of Computer * All right reserved.* Shao * file: demo.cpp* finish: April 02, 2015 * Version number: v1.0*/#include <iostream >using namespace Std;class student{public:student (int n,double s): num (n), score (s) {}; int Max (Student *arr); void Show (); int Get_num (); Double Get_score ();p rivate:int num; Study number double score; Score};void student::show () {cout<<num<< "" <<SCORE<<ENDL;} int Student::get_num () {return num;} Double Student::get_score () {return score;} The Max function returns the highest score in an array of objects that Arr points to (Max is not a member function, but a normal function) int max (Student *arr); int main () {Student stud[5]= {Student (101), 78.5), Student (102,85.5), Student (103,100), Student (104,98.5), Student (105,95.5)}; Output information for the 1th, 3, 5 students (with a looping statement) for (int i=0; i<5; i+=2) stud[i].show (); The number of students who output the highest score cout<< "the highest score among the 5 pupils is:" <<max (stud);//The Call function shows the highest score return 0;} Defines the function Max, returns the highest score in the array of objects that arr points to, returns the number of the highest achievers int max (Student *arr) {///asks for the highest score and the corresponding student's number double Maxscore=arr[0].get_score (); Int J; for (int i=0;i<5;i++) {if (Arr[i].get_score () >maxscore) Maxscore=arr[i].get_score (); J=i; } return Arr[j].get_num ();//Return to the highest score of the school number}
Operation Result:
@ Mayuko
Item 4-pointers to student classes