设计包含静态数据成员的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