C++語言 對象數組 友元函數

#pragma once
#include<iostream>
#include<iomanip>
using namespace std;

class Student
{
	int id;
	float score;
public:
	Student();
	Student(int i,float s):id(i),score(s){}
	friend Student Max(Student* StuArr, int len);
	float GetScore();
	void Print();
};


inline Student::Student()
{
	id = 0;
	score = 0.0f;

}


inline float Student::GetScore()
{
	return score;
}

inline void Student::Print()
{
	cout << "學號:" << id << endl << "分數:" << score << endl << endl;
}

Student Max(Student* StuArr, int len)
{
	Student s;
	for (int i = 0; i < len; i++)
	{
		if (s.score < StuArr[i].score)
		{
			s = StuArr[i];
		}
	}
	return s;
}


/******************************************************
*Complier:VS2017
*Author:Rise
*Project:對象數組
*******************************************************/
#include<iostream>
#include"Student.h"
using namespace std;


int main()
{
	Student st[4] = {
		{1001,98.2},
		{1002,97.8},
		{1003,99.0},
		{1004,95.9}
	};

	Max(st, 4).Print();
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章