定义并实现一个学生类(Student).cpp 802 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include <iostream>
  2. #include <string>
  3. class Student {
  4. private:
  5. std::string name;
  6. int id;
  7. static int count;
  8. static const int classNumber = 1001;
  9. public:
  10. Student(std::string name) : name(name) {
  11. id = ++count - 45;
  12. }
  13. void PrintStuInfo() const {
  14. std::cout << "名字:" << name << std::endl;
  15. std::cout << "学号:" << id << std::endl;
  16. std::cout << "班级:" << classNumber << std::endl;
  17. std::cout << "班级人数:" << count << std::endl;
  18. }
  19. static int GetStudentCount() {
  20. return count;
  21. }
  22. };
  23. int Student::count = 45;
  24. int main() {
  25. std::string name1, name2;
  26. std::cin >> name1 >> name2;
  27. Student stu1(name1);
  28. Student stu2(name2);
  29. stu1.PrintStuInfo();
  30. stu2.PrintStuInfo();
  31. return 0;
  32. }