友元函数(体检表)

体育已经成为一门重要的中考科目,但身体存在某些疾病的同学可以免考。

为此,凤湖中学每年需要整理一下毕业班同学(学生数不超过300人)的体检表中的体检结论,筛选出需要免考的同学,建议其申请免考。

要求建立一个类Exam,每个Exam类对象存储一位同学的体检信息,包括私有数据成员:

string num;//学号

int disease[10];//疾病信息

int count;//疾病数

该类还包括一些必要的公有成员函数。

此外,该类还有一个友元函数void print(Exam*e, int count, int *d)
用于输出需要申请免考的学生信息,这里e是一个指向Exam类对象数组元素的指针,count是学生数量,d是一个指向值得关注病病数组元素的指针。

#include<iostream>
#include<algorithm>
#include <iostream>
using namespace std;
class student
{ 	private:
		int chin,math,eng,all;
    public:
		friend student top(const student *p, int count);
		int sum;
		void set(int c,int m,int e,int a)
		{
			chin=c;math=m;eng=e;all=a;sum=chin+math+eng+all;
		}
};
student top(const student *p, int count)
{
	int i;
	int max=0;
	for(i=0;i<count;i++)
	{
		if(p[i].sum>max)
		{
			max=p[i].sum;
		}
	}
	cout<<max;
}

int main()
{
    int i;
    string name;
    int c,m,e,a;

    student stu[100];
    for(i=0;;i++)
    {
    	cin>>name;
    	if(name!="0")
    	{
    		cin>>c>>m>>e>>a;
    		stu[i].set(c,m,e,a);
		}
		else
		{
			break;
		}
	}
	top(stu,i);
    return 0;
}

 

posted @ 2022-05-18 21:35  墨燃云  阅读(226)  评论(0)    收藏  举报