Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2508559
  • 博文数量: 308
  • 博客积分: 5547
  • 博客等级: 大校
  • 技术积分: 3782
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 09:47
个人简介

hello world.

文章分类

全部博文(308)

分类: C/C++

2010-11-12 10:33:28

#include <iostream>
using namespace std;

class Student
{
      public :
      Student(int n,int a,float s):num(n),age(a),score(s){}
      void total();
      static float myaverage(Student stu);
      static float average();
      
      private :
      int num;
      int age;
      float score;
      static float sum;
      static int count;
};

void Student::total()
{
     sum +=score;
     count ++;
}

float Student::myaverage(Student stu)
{
      cout << stu.score << endl;
      return sum / count;
}

float Student::average()
{
    return sum / count;
}
float Student::sum = 0;
int Student::count = 0;
int main()
{
    Student stud[3] =
    {
            Student(1001,18,70),
            Student(1002,19,78),
            Student(1005,20,98)
    };
    int n;
    cout << "please input the number of students:";
    cin >> n;
    
    for (int i = 0; i < n; i++)
    {
        stud[i].total();
    }
    cout << "the average score of " << n << " student is " << Student::myaverage(stud[0]) <<endl;
    
    system("pause");
    return 0;
}


阅读(1874) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~