c++友元函数学习_友元函数c++友元函数学习_友元函数_02View Code
#include<iostream>
#include<string.h>
using namespace std;

class student
{
private:
char name[10];
int score;

public:
char* gan(student &student1,student &student2);
friend void disp(student &student1);//友元函数

student(char tname[10]="",int tscore=0)//构造函数
{
strcmp(name,tname);
score=tscore;
}
};

void disp(student &student1)//友元函数定义是不需引用对象
{
if(student1.score>=80)
cout<<student1.score<<endl;
}

char* student::gan(student &student1,student &student2)
{
if(student1.score>student2.score)
return student1.name;
return student2.name;
}

void main()
{
student st1("hc",100);
student st2("s2",99);
}