设计包含静态数据成员的Student类

设计包含静态数据成员的Student类,在该类定义中包括:

数据成员:学号,年龄,分数score,及静态数据成员学生人数count;

定义成员函数用于设置和读取及显示学号,年龄,分数,累计学生人数;静态成员函数getCount( ) 用于返回总人数;外部函数average()用于求平均值。

在main函数中,输入某班同学的成绩,并调用上述函数求全班学生的平均分。

 

#include <iostream>

using namespace std;

class Student

{ private:

    int num;

    int age;

    float score;

    static int count;

  public:

    Student(){count++; }

    Student(int n,int a,float s)

    { num=n;age=a;score=s; count++; }

    void setStud(int n,int a,float s)

    { num=n;age=a;score=s; }

    void dispStud()

    { cout<<num<<"  "<<age<<"  "<<score<<endl; }

    void setNum(int n){ num=n; }

    void setAge(int a){ age=a; }

    void setScore(float s){ score=s; }

    int getNum(){ return num; }

    int getAge(){ return age; }

    float getScore(){ return score; }

    static int getCount( )

    {return count;}

    ~Student(){count--; cout<<count<<endl;}

};

float average(Student stud[],int n){

    int i;

    float sum=0;

    for(int i=0;i<n;i++)

        sum+=stud[i].getScore();

    return (sum/n);

//return (sum/Student::getCount( ));

 

}

int Student::count=0;

int main( )

{  Student stud[5]={

                     Student(1001,18,80),

                     Student(1002,20,78),

                     Student(1003,18,85),

                     Student(1004,19,90),

                     Student(1005,20,98)};

   Student st1,st2;

for(int i=0;i<5;i++)

   {stud[i].dispStud();

    cout<<Student::getCount( )<<" "<<stud[i].getCount( )<<endl; }

   cout<<average(stud,5 ) << endl;

   return 0;

}

 

设计包含静态数据成员的Student类,在该类定义中包括:

数据成员:学号,年龄,分数score及两个静态数据成员(总分total和学生人数count);

定义成员函数用于设置和读取及显示学号,年龄,分数,求总分和累计学生人数;静态成员函数getCount( ) 用于返回总人数;静态成员函数getTotal()用于返回总分;外部函数average()用于求平均值。

在main函数中,输入某班同学的成绩,并调用上述函数求全班学生的平均分。

 

#include <iostream>

using namespace std;

class Student

{ private:

    int num;

    int age;

    float score;

    static int count;

    static float total;

  public:

    Student(){num=0;age=0;score=0;count++;}

    Student(int n,int a,float s)

    { num=n; age=a; score=s; count++; total+=score;}

    void setStud(int n,int a,float s)

    { num=n;age=a;total-=score;score=s; total+=score;}

    void dispStud()

    { cout<<num<<"  "<<age<<"  "<<score<<endl; }

    void setNum(int n){ num=n; }

    void setAge(int a){ age=a; }

    void setScore(float s)

    { total-=score;score=s; total+=score; }

    int getNum(){ return num; }

    int getAge(){ return age; }

    float getScore(){ return score; }

    static int getCount( )

    {return count;}

    static float getTotal( )

    {return total;}

    ~Student(){count--; total-=score ;cout<<count<<"  "<<total<<endl;}

};

float average(){

 //   int i;

  //  float sum=0;

 //   for(int i=0;i<n;i++)

 //       sum+=stud[i].getScore();

  //  sum=Student::getTotal( );

    return (Student::getTotal( )/Student::getCount( ));

}

int Student::count=0;

float Student::total=0.0;

int main( )

{  Student stud[5]={

                     Student(1001,18,80),

                     Student(1002,20,78),

                     Student(1003,18,85),

                     Student(1004,19,90),

                     Student(1005,20,98)};

   Student st1,st2;

   st1.setStud(1006,18,81);st1.dispStud();

   st2.setStud(1007,19,91);st2.dispStud();

   for(int i=0;i<5;i++)   stud[i].dispStud();

   cout<<Student::getCount( )<<" "<<Student::getTotal( )<<endl;

   cout<<average( ) << endl;

   stud[2].setScore(80);

   stud[2].dispStud();

   cout<<Student::getTotal( )<<"  "<<average( ) << endl;

   return 0;

}

运行结果:

1006  18  81

1007  19  91

1001  18  80

1002  20  78

1003  18  85

1004  19  90

1005  20  98

7 603

86.1429

1003  18  80

598  85.4286

6  507

5  426

4  328

3  238

2  158

1  80

0  0

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值