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

Class Student

week09/examples/constructor.cpp:6–54  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers 1

mainFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected