#include<iostream.h>
#include<string.h>
class CProcess;
class CStudent
{
public:
CStudent();
friend class CProcess;
private:
char Name[10];
float Degree;
char*Level;
};
class CProcess
{
public:
void Transform(CStudent& s);
void Display(CStudent& s);
};
CStudent::CStudent()
{
char na[10];
float de;
cout<<"输入姓名:"<<endl;
cin>>na;
cout<<"输入成绩:"<<endl;
cin>>de;
strcpy(Name,na);
Degree=de;
}
void CProcess::Transform(CStudent& s)
{
if(s.Degree>=90)
s.Level="优秀";
else if(s.Degree>=80)
s.Level="良好";
else if(s.Degree>=70)
s.Level="中等";
else if(s.Degree>=60)
s.Level="及格";
else
s.Level="不及格";
}
void CProcess::Display(CStudent& s)
{
cout<<s.Name<<"
"<<s.Degree<<"
"<<s.Level<<endl;
}
int main()
{
CStudent stu1,stu2,stu3,stu4;
CProcess po;
po.Transform(stu1);
po.Transform(stu2);
po.Transform(stu3);po.Transform(stu4);
cout<<"姓名"<<"
"<<" 成绩 "<<"
"<<"等级"<<endl;
po.Display(stu1);
po.Display(stu2);
po.Display(stu3);po.Display(stu4);
return 0;
}
此程序是很简单的,核心是明白友元类特点,但是在平时要慎重的应用友元。