#include
#include
using namespace std;
class Student
{ public:
Student(int x,string y,int z):number(x),name(y),score(z){}
void display();
friend void contrast(Student *a,Student *b,Student *c);
private:
int number;
string name;
int score;
};
void Student::display()
{
cout<<"score="<
void Student::contrast(Student *a,Student *b,Student *c)
{
int max;
max=(a->score>b->score )? a->score : b->score;
max=(max > c->score )? max : c->score;
if (max==a->score )
(*a).display();
else if (max==b->score)
(*b).display();
else (*c).display();
}
int main()
{
Student student[3]={Student(1,"wangli",90),Student(2,"dengxin",88),Student(3,"chenchao",82)};
student[0].contrast(&student[0],&student[1],&student[2]);
return 0;
}
不好意思,上面的程序由于友元不支持vc6,我没有调试,但是下面的不包含的仿制品,估计差不多哈。。。
不包含友元函数的:
#include
#include
using namespace std;
class Student
{ public:
Student(int x,string y,int z):number(x),name(y),score(z){}
void display();
int getscore();
void contrast(Student *a,Student *b,Student *c);
private:
int number;
string name;
int score;
};
void Student::display()
{
cout<<"score="<
int Student::getscore()
{
return score;
}
void Student::contrast(Student *a,Student *b,Student *c)
{
int max;
max=(a->getscore()>b->getscore() )? a->getscore() : b->getscore();
max=(max > c->getscore() )? max : c->getscore();
if (max==a->getscore() )
(*a).display();
else if (max==b->getscore() )
(*b).display();
else (*c).display();
}
int main()
{
Student student[3]={Student(1,"wangli",90),Student(2,"dengxin",88),Student(3,"chenchao",82)};
student[0].contrast(&student[0],&student[1],&student[2]);
return 0;
}