• 友元课后题


    #include"iostream.h"
    #include"string.h"
    class score
    {
     private:
     char name[8],xh[7];
     double sx,yw,yy;
     public:
     score(char *n,char *x,double sx,double yw,double yy)
     {   strcpy(name,n);
      strcpy(xh,x);
      this->sx=sx;
      this->yw=yw;
      this->yy=yy;
     }
     score()
     {
      cout<<"请输入学生名:";
      cin>>name;
      cout<<"请输入学号:";
      cin>>xh;
      cout<<"请输入数学成绩:";
      cin>>sx;
      cout<<"请输入语文成绩:";
      cin>>yw;
      cout<<"请输入英语成绩:";
      cin>>yy;
     }

    void display()

    {

    cout<<"输出姓名:";

    cout<<name<<endl;

    cout<<"输出学号:" ;

    cout<<xh<<endl;

    cout<<"输出数学成绩:";

    cout<<sx<<endl;

    cout<<"输出语文成绩";

    cout<<yw<<endl;

    cout<<"输出英语成绩";

    cout<<yy<<endl;
    cout<<"输出三科的平均成绩";
    }

     friend double average(score);
    };
    double average(score z)
    {
     double avg;
     avg=(z.sx+z.yw+z.yy)/3;
     return avg;
    }
    void main()
    {
     score z;
     z.display();
        cout<<average(z)<<endl;
    }

     

    题目:设计学生成绩类SCORE,包括学号,姓名,数学,语文,英语和平均成绩私有数据成员,再定义一个计算学生平均成绩的普通函数AVERAGE(),并将该函数定义为SCORE类的友元函数.在主函数中定义学生成绩对象.通过构造函数给除平均成绩之外的成员同值,然后通过调用AVERAGE()计算机平均成绩并同仁,输出学生成绩的所有信息.

    分析:先定义一个类SCORE,在类中定义它的参数,学号,姓名,数学,语文,英语,分别赋值,在利用构造函数从键盘上输入上学生的所有信息,把函数AVERAGE定义为友元函数,用它来返回平均值,在定义一个类Z,在输出平均函数AVERAGE!!

  • 相关阅读:
    2.替换空格
    1.二维数组的查找
    poj 2431 expedition
    python入门第三天
    python入门第二天__练习题
    [Python3.6] print vs sys.stdout.write
    python入门第二天
    使用Flask-mail发送邮件无法连接主机
    KMP
    逆序对 线段树&树状数组 (重制版)
  • 原文地址:https://www.cnblogs.com/qiqiBoKe/p/2791595.html
Copyright © 2020-2023  润新知