-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathPerson.cpp
More file actions
183 lines (152 loc) · 5.4 KB
/
Copy pathPerson.cpp
File metadata and controls
183 lines (152 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// Base Class for our inheritance examples.
// Will include several layers of derived classes by end
// Initial Derived: Student, Staff, Academic
//
#include <string>
#include <vector>
#include <array>
using std::string;
class Person {
public:
// our base demographics for all people
long long uid = 15131324134;
// return age by subtracting current year
int age(int year) { return year - dob.year; }
// get dob as a string (so dob.tostring())
string get_dob() {
return std::to_string(dob.month) + "/"
+ std::to_string(dob.day) + "/"
+ std::to_string(dob.year);
}
string get_full_name();
string get_firstname();
string get_lastname();
protected:
// struct for holding name
struct name {
string first;
char m_initial;
string last;
};
// struct for holding date
struct date {
int year = 1900;
int month = 1;
int day = 1;
};
date dob;
// split passed string into first middle and last and assign to struct
// return struct to be assigned in derived classes
name assign_name(string n) {
// to hold our return values and words
name to_return;
std::vector<string> words;
string word = "";
// loop through letters and add any with spaces delimiters (and last after)
for (const auto letter : n) {
if (letter != ' ') {
word += letter;
} else {
words.push_back(word);
word = "";
}
}
words.push_back(word);
// assign first then last and middle if availalbe - if not null middle
to_return.first = words[0];
if (words.size() > 2) {
to_return.m_initial = words[1][0];
to_return.last = words[2];
} else {
to_return.m_initial = ' ';
to_return.last = words[1];
}
return to_return;
}
};
class Student: public Person {
private:
name student_name;
public:
Student() {}
Student(string n, long long id) {
student_name = assign_name(n);
uid = id;
}
// setter for dob
void set_dob(int day, int month, int year) {
dob.day = day;
dob.month = month;
dob.year = year;
}
string get_full_name() { return student_name.first + " " + student_name.m_initial + " " + student_name.last; }
string get_firstname() { return student_name.first; }
string get_lastname() { return student_name.last; }
};
// Student's created this in class
class Staff: Person {
private:
const std::array<string,7> departments = {"admin", "admission", "ITS",
"maintenance", "security", "student services",
"library"};
name staff_name;
string department;
string verify_department(string chosen_d) {
// if department matches it returns the department else unknown
for (const auto d: departments) {
if (d == chosen_d) {
return d;
}
}
return "unknown";
}
public:
Staff() {}
Staff(string n, long long id) {
staff_name = assign_name(n);
uid = id;
}
// using a function as a full member (no variable assigned)
string employee_type() {
if (department == "ITS" || department == "security" || department == "maintenance") {
return "support";
} else if (department == "library" || department == "student services") {
return "student support";
} else if (department == "admin" || department == "admission"){
return "admin";
} else {
return "unknown";
}
}
// getter and setters for the private variables
// setter for dob using string
void set_dob(string date, string delimiter = "/") {
// Whereas this parses a string and splits it into its numbers
// "1/1/11" is smallest date so if smaller just exit
// note - using Europe style will mess this up - d/m/yy
if (date.length() > 6) {
int start = 0;
int end = date.find(delimiter);
int dates[] = {0,0,0};
// so again - one could just loop until demlimiter and use same algo as words
// but I wanted to show the find/offset version here:
for (int i = 0; i < 3; i++) {
// using stoi (from string library) to cast our string to int
dates[i] = stoi(date.substr(start, end - start));
start = end + delimiter.size();
end = date.find(delimiter, start);
}
dob.month = dates[0];
dob.day = dates[1];
dob.year = dates[2];
}
}
// getter and setter for department (unknown if not a matching department passed)
void set_department(string d) { department = verify_department(d); }
string get_department() { return department; }
// getters for name (setter in Person)
string get_full_name() { return staff_name.first + " " + staff_name.m_initial + " " + staff_name.last; }
string get_firstname() { return staff_name.first; }
string get_lastname() { return staff_name.last; }
};