习题 3.5 建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。

谭浩强c++ 面向对象程序设计(第2版)
习题 3.5 建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。

#include<iostream>
using namespace std;

class Student
{
	private:
		int num;
		int score;
	public:
		void max(Student *a, int n);
		Student(int n,int s):num(n),score(s){}
};

void Student::max(Student *a, int n)
{
	int temp = a->score;
	int tempt = a->num;
	Student* b = a;
	for ( ; b<a+n;b++)
	{
		if (temp<b->score)
		{
			temp = b->score;
			tempt = b->num;
		}
	}
	cout << "最高分:" << temp << endl;
	cout << "学号:" << tempt << endl;
}

int main()
{
	Student stud[5] = {
	Student(1,91),
	Student(2,92),
	Student(3,93),
	Student(4,94),
	Student(5,95)
	};
	Student* temp = stud;
	temp->max(stud, 5);

	system("pause");
	return 0;
}

感谢访问 如果觉得有帮助请点个赞 谢谢
欢迎交流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值