天天看點

用類求總分和平均分

#include<iostream>
using namespace std;
class Student
{
  public:
    void account(); //定義一個輸入成績和計算總分的成員函數; 
    static  double sum();//傳回總分; 
    static double average();//傳回平均值; 
  private:
    double score;
    static double total_score;
    static int count;
};
void Student::account()
{
  cin>>score;//輸入成績; 
  total_score+=score;//計算總分; 
  count++;//統計人數; 
}
double Student::sum()
{
  return total_score;
}
double Student::average()
{
  return total_score/count;
}
int Student::count=0;//靜态變量賦初值; 
double Student::total_score=0;
int main()
{
  Student a1;
  for(int i=0;i<3;i++)
    a1.account();
  cout<<"總分是:"<<a1.sum()<<endl;
  cout<<"平均分是:"<<a1.average()<<endl;
  return 0;
}