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

Class Student

week09/examples/access-attribute.cpp:4–39  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2#include <cstring>
3
4class Student
5{
6 private:
7 char name[4];
8 int born;
9 bool male;
10 public:
11 void setName(const char * s)
12 {
13 if (s == NULL)
14 {
15 std::cerr << "The input is NULL." << std::endl;
16 return;
17 }
18 size_t len = sizeof(name) - 1;
19 strncpy(name, s, len);
20 name[len] = '\0';
21 }
22 void setBorn(int b)
23 {
24 if (b >= 1990 && b <= 2020 )
25 born = b;
26 else
27 std::cerr << "The input b is " << b << ", and should be in [1990, 2020]." << std::endl;
28 }
29 void setGender(bool isMale)
30 {
31 male = isMale;
32 }
33 void printInfo()
34 {
35 std::cout << "Name: " << name << std::endl;
36 std::cout << "Born in " << born << std::endl;
37 std::cout << "Gender: " << (male ? "Male" : "Female") << std::endl;
38 }
39};
40
41int main()
42{

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected