习题 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;
}

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

猜你喜欢

转载自blog.csdn.net/Captain_Aaron/article/details/105057124