上机实验预习
这题让我对友元函数有了清晰的概念
友元函数只需要在Public中声明即可随时调用Private中的属性
#include<iostream>
#include<string>
using namespace std;
class CStudent
{
public:
CStudent(int a=0,string b="NULL",double c=0):num(a),name(b),grade(c){}
CStudent(CStudent &other)
{
num=other.num;
name=other.name;
grade=other.grade;
}
~CStudent(){}
void set_num(int a)
{
num=a;
}
void set_name(string b)
{
name=b;
}
void set_grade(double c)
{
grade=c;
}
int get_num()
{
return num;
}
string get_name()
{
return name;
}
double get_grade()
{
return grade;
}
friend CStudent find_max(CStudent ss[],int n);
private:
int num;
string name;
double grade;
};
CStudent find_max(CStudent ss[],int n)
{
int i,t=1;
double max_g=ss[1].grade;
for(i=2;i<=n;i++)
if(max_g<ss[i].grade)
{
max_g=ss[i].grade;
t=i;
}
return ss[t];
}
int main()
{
int n,i,x;
string s;
double r;
CStudent ss[10];
cin >> n;
for(i=1;i<=n;i++)
{
cin>>x;
ss[i].set_num(x);
cin>>s;
ss[i].set_name(s);
cin>>r;
ss[i].set_grade(r);
}
cout << find_max(ss,n).get_grade()<<endl;
return 0;
}