정적 구성원---학생 개수 계산

1151 단어
</pre><pre name="code" class="cpp">#include <iostream>
#include <string>
using namespace std;
class Student
{
private:
string name;  // 
int age;      // 
int score;    // 
static int count; // 
static int sum;  // 
public:
Student(string n,int a,int s);  // 
static int get_count();  // , count 
static int get_sum();   // , sum 
};

// , begin end 
//******************** begin ********************
int Student::count=0;
int Student::sum=0;
Student::Student(string n,int a,int s)
{
     name=n;
age=a;
score=s;
count++;
sum+=s;
}
int Student::get_count()
{
    return count;
}
int Student::get_sum()
{
     return sum;
}
//********************* end ********************
int  main( )
{
  string name;
  int age;
  int score;
  int n;
  cin>>n;  // 
  while(n--)
  {
         cin>>name>>age>>score;
new Student(name,age,score);
  }
  cout<<"the count of student objects=";
  cout<<Student::get_count()<<endl;
  cout<<"the sum of all students score=";
  cout<<Student::get_sum()<<endl;
  return 0;
}

좋은 웹페이지 즐겨찾기