MCPcopy Create free account
hub / github.com/ShiqiYu/CPP / Student

Class Student

week09/examples/static.cpp:6–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

4using namespace std;
5
6class Student
7{
8 private:
9 static size_t student_total; // declaration only
10 //inline static size_t student_total = 0; //C++17, definition outside isn't needed
11 char * name;
12 int born;
13 bool male;
14 public:
15 Student()
16 {
17 student_total++;
18 name = new char[1024]{0};
19 born = 0;
20 male = false;
21 cout << "Constructor: Person(): student_total = " << student_total << endl;
22 }
23 Student(const char * initName, int initBorn, bool isMale)
24 {
25 student_total++;
26 name = new char[1024];
27 setName(initName);
28 born = initBorn;
29 male = isMale;
30 cout << "Constructor: Person(const char, int , bool): student_total = " << student_total << endl;
31 }
32 ~Student()
33 {
34 student_total--;
35 cout << "To destroy object: " << name ;
36 cout << ". Then " << student_total << " students are left" << endl;
37 delete [] name;
38 }
39
40 void setName(const char * s)
41 {
42 if (s == NULL)
43 {
44 std::cerr << "The input is NULL." << std::endl;
45 return;
46 }
47 size_t len = 1024 - 1;
48 strncpy(name, s, len);
49 name[len] = '\0';
50 }
51 void setBorn(int b)
52 {
53 if (b >= 1990 && b <= 2020 )
54 born = b;
55 else
56 std::cerr << "The input b is " << b << ", and should be in [1990, 2020]." << std::endl;
57 }
58 static size_t getTotal() {return student_total;}
59 // the declarations, the definitions are out of the class
60 void setGender(bool isMale);
61 void printInfo();
62};
63

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected