#include<iostream.h>
#include<string.h>
class student
{
public:
student(char *num,char *name,float x,float y,float z)
{
strcpy(nu,num);
strcpy(na,name);
A=x;B=y;C=z;
}
friend float average(student a);
void print(student a);
private:
char nu[15],char na[10];
float A,B,C;
};
float average(student a)
{
return (a.A+a.B+a.C)/3;
}
void student::print(student a)
{
float t="average"(a);
if(t>=90)
cout<<"the number is "<<nu<<endl<<"the name is "<<na<<endl<<"the graduate is 优"<<endl;
if(t<=89&&t>=80)
cout<<"the number is "<<nu<<endl<<"the name is "<<na<<endl<<"the graduate is 中"<<endl;
}
void main()
{
student a("2220041190405","xiongwei",76,92.5,94);
a.print(a);
}
以上代码中,说明,成员函数可以调用友员函数。还可以用如下方法实现。
#include<iostream.h>
#include<string.h>
class student
{
public:
student(char *num,char *name,float x,float y,float z)
{
strcpy(nu,num);
strcpy(na,name);
A=x;B=y;C=z;
}
friend float average(student a);
void print(float t);
private:
char nu[15],char na[10];
float A,B,C;
};
float average(student a)
{
return (a.A+a.B+a.C)/3;
}
void student::print(float t)
{
if(t>=90)
cout<<"the number is "<<nu<<endl<<"the name is "<<na<<endl<<"the graduate is 优"<<endl;
if(t<=89&&t>=80)
cout<<"the number is "<<nu<<endl<<"the name is "<<na<<endl<<"the graduate is 中"<<endl;
}
void main()
{
student a("2220041190405","xiongwei",76,92.5,94);
float t="average"(a);
a.print(t);
}
文章评论(0条评论)
登录后参与讨论