原创 成员函数调用友员函数

2009-1-5 21:28 1837 7 7 分类: 工程师职场

#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);
}

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
7
关闭 站长推荐上一条 /3 下一条