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

Class Student

week09/examples/this.cpp:6–56  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected