1. 程式人生 > >物件陣列·簡單的學生管理

物件陣列·簡單的學生管理

/*簡單的學生類,輸入某班的成績計算總分和平均值*/
/*簡單的學生類,輸入某班的成績計算總分和平均值*/
#include<iostream>
using namespace std;
class Student
{
	double score;
	static double total;
	static double count;
public:
	void scoretotalcount();
	static double sum();
	static double average();
};
double Student::total=0;
double Student::count=0;
void Student::scoretotalcount()
{
	
    double s;
    cin>>s;
    score=s;
    total=total+score;
    count=count+1;
}
double Student::sum()
{return total;}
double Student::average()
{return total/count;}
int main()
{
	int n;
	cout<<"please input  the total number of student:  ";
	cin>>n;
	Student *s;//動態建立物件陣列注意的是,n的個數要提前輸入,利用指標建立
	static int i=0;//保留i的值,以便最後輸出在類中累計的總分
	s=new Student[n];
	for(i;i<n;i++)
	{   cout<<"please input the score of "<<i+1<<" the student:  ";
		s[i].scoretotalcount();
	}
	cout<<"total:  "<<s[i].sum()<<"\taverage:  "<<s[i].average()<<endl;
	return 0;