#includeusing namespace std; class Student { private: //static int count; //在类的声明中,仅对静态数据进行引用性声明,必须在文件作用域的某个地方用类名限定进行定义 int number; char name[20]; int age; public: static int count; Student(const char* n, int a) { count = count + 1; number = 202000 + count; strcpy_s(name, strlen(n) + 1, n); age = a; } void show(void) { cout << "学号:" << number << endl; cout << "姓名:" << name << endl; cout << "年龄:" << age << endl; cout << "学生总数:" << count << endl; cout << "................." << endl; } }; int Student::count = 0; //静态数据成员不依托具体对象而存在,有自己单独的内存空间 //当静态数据成员是被声明为public时,可以通过类名直接对他进行访问 int main() { Student s1("张三",21); Student s2("李四",22); Student s3("王五",23); s1.show(); s2.show(); s3.show(); cout << Student::count; return 0; }



