当前位置:编程学习 > C/C++ >>

用友元函数做C++

定义学生类Student,包括学号、姓名、三平均、门课的成绩成绩。设计一个友元函数grade(),输出按平均成绩对应的等级:
大于等于90为优
80~89为良
70~79为中
60~69为及格
小于60为不及格*/

以下程序是我写的,但不知道怎样用友元把这个程序弄好,麻烦帮我修改下,谢谢

#include "iostream.h"
#include "string.h"
class  student
{
public:
 student(char name[20],char num[20],int yu,int shu,int ying);
 void changename(char name[20],char num[20],int yu,int shu,int ying);
 void ave(int a,int b,int c);
    friend void grade(int pp);
   
private:
 char qname[20];
 char qnum[20];
 int qyu;
 int qshu;
 int qying;
 int ping;

};

student::student(char name[20],char num[20],int yu,int shu,int ying)
{
 strcpy(qname,name);
    strcpy( qnum,num);
    qyu=yu;
 qshu=shu;
 qying=ying;
 ping=(yu+shu+ying)/3;

}
void student::changename(char name[20],char num[20],int yu,int shu,int ying)
{
 strcpy(qname,name);
    strcpy( qnum,num);
    qyu=yu;
 qshu=shu;
 qying=ying; 
 ping=(yu+shu+ying)/3;

}
void student::ave(int a,int b,int c)
{
 double w;
 w=(a+b+c)/3;
 cout<<"平均成绩为"<<w<<endl;

}

void grade(int pp)
{
 
  
 if (pp>=90)
 {
  cout<<"优"<<endl;
 }
 if (pp>=80&&pp<=89)
 {
  cout<<"良"<<endl;
 }
 if (pp>=70&&pp<=79)
 {
  cout<<"中"<<endl;
 }
 if (pp>=60&&pp<=69)
 {
  cout<<"及格"<<endl;
 }
 if (pp<60)
 {
  cout<<"不及格"<<endl;
 }


}
void main()
{

 student a("mm","0000",99,99,99);
 a.ave(99,99,99);

    cout<<grade(a.ave)<<endl;
 char name[20];
 char num[20];
 
 int yu,shu,ying;
 cout<<"请输入姓名"<<endl;
 cin>>name;
 cout<<"请输入学号"<<endl;
 cin>>num;
 cout<<"请输入语文成绩"<<endl;
 cin>>yu;
 cout<<"请输入数学成绩"<<endl;
 cin>>shu;
 cout<<"请输入英语成绩"<<endl;
 cin>>ying;
 a.changename(name,num,yu,shu,ying);
 a.ave(yu,shu,ying);
}

答案:#include "iostream"
using namespace std;
#include "string.h"
class  student
{
public:
 student(char name[20],char num[20],int yu,int shu,int ying);
 void changename(char name[20],char num[20],int yu,int shu,int ying);
  double ave(int a,int b,int c);
    friend void grade(int pp);
  
private:
 char qname[20];
 char qnum[20];
 int qyu;
 int qshu;
 int qying;
 int ping;

};

student::student(char name[20],char num[20],int yu,int shu,int ying)
{
 strcpy(qname,name);
    strcpy(qnum,num);
    qyu=yu;
 qshu=shu;
 qying=ying;
 ping=(yu+shu+ying)/3;

}
void student::changename(char name[20],char num[20],int yu,int shu,int ying)
{
 strcpy(qname,name);
    strcpy( qnum,num);
    qyu=yu;
 qshu=shu;
 qying=ying;
 ping=(yu+shu+ying)/3;

}

double student::ave(int a,int b,int c)
{
 double w;
 w=(a+b+c)/3;
 return w;

}

void grade(int pp)
{
 
 
 if (pp>=90)
 {
  cout<<"优"<<endl;
 }
 if (pp>=80&&pp<=89)
 {
  cout<<"良"<<endl;
 }
 if (pp>=70&&pp<=79)
 {
  cout<<"中"<<endl;
 }
 if (pp>=60&&pp<=69)
 {
  cout<<"及格"<<endl;
 }
 if (pp<60)
 {
  cout<<"不及格"<<endl;
 }


}
void main()
{

 student a("mm","0000",99,99,99);
 //a.ave(99,99,99);

grade(a.ave(99,99,99));
 char name[20];
 char num[20];
 
 int yu,shu,ying;
 cout<<"请输入姓名"<<endl;
 cin>>name;
 cout<<"请输入学号"<<endl;
 cin>>num;
 cout<<"请输入语文成绩"<<endl;
 cin>>yu;
 cout<<"请输入数学成绩"<<endl;
 cin>>shu;
 cout<<"请输入英语成绩"<<endl;
 cin>>ying;
 a.changename(name,num,yu,shu,ying);
 a.ave(yu,shu,ying);
}

上一个:求C++高手解决
下一个:c++问题的求解

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,