友元課後題

來源:互聯網
上載者:User

 

 

 
#include<iostream.h>
#include"string.h"
class score
{
private:
 char name[8],xh[7];
 double sx,yw,yy;
  
public:
 score(char n[8],char z[7],double a,double b,double c)
 {
  strcpy(name,n);
  strcpy(xh,z);
  sx=a;
  yw=b;
  yy=c;
 
 }
  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+yw+yy<<endl;
  cout<<"輸出平均分:";
 }
   
     friend double average(score);
};
double average(score x)
{
 double pj;
 pj=(x.sx+x.yw+x.yy)/3;
 return pj;
}

void main()
{
 score s;
    s.display();
 cout<<average(s)<<endl;
 
}

 

題目要求:設計學產生績類SCORE包括學號,姓名,數學,語文,英語和平均成績私人資料成員,再定義一個計算學生平均成績的普通函數AVERAGE(),並將該函數定義為SCORE類的友元函數,在主函中定義學產生績對象,通過建構函式給初平均成績之外的成員賦值,然後通過調用AVERAGE(),計算平均成績並賦值輸出學產生績的所有資訊!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.