雕刻时光

just do it……nothing impossible
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

c++友元函数学习

Posted on 2011-04-04 15:18  huhuuu  阅读(234)  评论(0编辑  收藏  举报
View 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);
}