4.21

来源:互联网 发布:数控加工中心编程软件 编辑:程序博客网 时间:2024/05/16 18:56
  1. #include<iostream>  
  2. #include<string>  
  3. using namespace std;  
  4. class student{  
  5. public:  
  6.     student(string number,string name,float score);  
  7.     void show();  
  8.     void show_count_sum_ave();  
  9. private:  
  10.     string number;  
  11.     string name;  
  12.     float score;  
  13.     static int count;    //学生人数  
  14.     static float sum;    //总成绩  
  15.     static float ave;    //平均成绩  
  16. };  
  17. student::student(string num,string nam,float sco)   //构造函数  
  18. {  
  19.     number=num;  
  20.     name=nam;  
  21.     score=sco;  
  22.     ++count;  
  23.     sum=sum+score;  
  24.     ave=sum/count;  
  25. }  
  26.   
  27. int student::count=0;  
  28. float student::sum=0.0;  
  29. float student::ave=0.0;  
  30.   
  31. void student::show()         //显示学号、姓名、成绩  
  32. {  
  33.     cout<<"该学生的学号:"<<number<<endl;  
  34.     cout<<"该学生的姓名:"<<name<<endl;  
  35.     cout<<"该学生的成绩:"<<score<<endl<<endl;  
  36. }  
  37.   
  38. void student::show_count_sum_ave()   //显示人数、总成绩、平均成绩  
  39. {  
  40.     cout<<"学生人数:"<<count<<endl;  
  41.     cout<<"总成绩:"<<sum<<endl;  
  42.     cout<<"平均成绩:"<<ave<<endl;  
  43. }  
  44.   
  45.   
  46. int main()  
  47. {  
  48.     student stu1("001","lily",90);  
  49.     stu1.show();  
  50.     student stu2("002","smile",90);  
  51.     stu2.show();  
  52.     student stu3("003","rose",90);  
  53.     stu3.show();  
  54.     stu3.show_count_sum_ave();  
  55.   
  56.     return 0;  
  57. }  
0 0
原创粉丝点击