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.
Reference Answer:
/* All rights reserved. * File name: Test.cpp * Chen Dani * Completion Date: April 3, 2015 * version number: v1.0 */#include <iostream>using namespace Std;//max function returns an array of objects to which arr points The highest score (Max is not a member function, but a normal function) class student{public:student (int n,double s): num (n), score (s) {} void display (); int Getnum () {return num; } double Getscore () {return score; }private:int num; Study number double score; Score};void Student::d isplay () {cout<<num<< "" <<SCORE<<ENDL;} 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) {cout<< "student" <<i+1<< ":"; Stud[i].display (); }//Find out who has the highest student performance and output the highest academic number cout<< "The number of the 5 students with the highest score is:" <<max (stud);//Call function to display 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 Max_score=arR[0].getscore (); Int J; for (int i=1;i<5;i++) if (Arr[i].getscore () >max_score) {Max_score=arr[i].getscore (); J=i; } return Arr[j].getnum ();//Return to the highest score of the school number}
Learning experience: The first clear order, in a small fast a small piece of the completion of each step, it became a complete program.
or the array and the pointer in the review again!! Keep on refueling!!
Item four--pointers to student classes (1)