函数max用指向对象的指针作函数参数

#include <iostream>
using namespace std;
class Student
{
public:
	Student(int n,int s):num(n),score(s){};
	void display();
	int num;
	int score;
};

void Student::display()
{
	cout<<num<<":"<<score<<endl;
}

int main()
{
	void max(Student *);
	Student stud[5]={
		Student(1011,84),Student(1012,97),Student(1013,78),Student(1014,83),Student(1015,88)};
	Student *p=stud;//使指针指向数组的首元素
	for(int i=0;i<3;p+=2,i++)
	{
		cout<<(i+1)<<" ";
		p->display();//stud[i].display
	}
	Student *p1=stud;
    max(p1);
	return 0;
}

void max(Student *stu)
{
	int max_score=0;
	int k;//存储最大值的角标
	for(int j=0;j<5;j++)//求最大值
		if(max_score<stu[j].score)
		{
			max_score=stu[j].score;
			k=j;
		}
	cout<<"The max score is "<<stu[k].score<<",its number is "<<stu[k].num<<endl;
}

你可能感兴趣的:(函数max用指向对象的指针作函数参数)