-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path07_emp_fulltime_parttime.cpp
318 lines (258 loc) · 7.69 KB
/
07_emp_fulltime_parttime.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
// // Write class declarations and member function definitions for a C++ base class to represent an Employee (empId, name). Derive two classes as Fulltime (dailyRate, numberOfDays, salary) and Parttime (numberOfHours, hourlyRate, salary).
// // Header files
#include <iostream>
#include <string.h>
#include <stdlib.h>
// // use namespace
using namespace std;
// // define Macro
#define MAX_EMPS 20
// // define class Employee
class Employee
{
public:
// // static member variable
static const int MAX_CHARS_IN_NAME = 31;
private:
// // instance member variables
char *name;
int empId;
public:
// // instance member function to set name
void setName(const char *name)
{
int length = strlen(name);
this->name = new char[length > MAX_CHARS_IN_NAME - 1 ? MAX_CHARS_IN_NAME : length + 1];
if (length > MAX_CHARS_IN_NAME - 1)
{
strncpy(this->name, name, MAX_CHARS_IN_NAME - 1);
this->name[MAX_CHARS_IN_NAME - 1] = 0; // // terminate with null character
}
else
{
strcpy(this->name, name);
}
}
// // instance member function to get name
const char *getName() const
{
return name;
}
// // instance member function to set empId
void setEmpId(int empId)
{
this->empId = empId;
}
// // instance member function to get empId
int getEmpId() const
{
return empId;
}
// // instance member function to input and set details
virtual void setDetails()
{
char name[MAX_CHARS_IN_NAME];
cout << "\nEnter Name (MAX_CHARS " << MAX_CHARS_IN_NAME - 1 << ") => ";
cin.ignore();
cin.getline(name, MAX_CHARS_IN_NAME);
setName(name);
cout << "\nEnter Employee Id => ";
cin >> empId;
}
// // instance member function to show details
virtual void showDetails() const
{
cout << "\nName => " << name;
cout << "\nEmployee Id => " << empId;
}
// // friend function to search employee by id
friend int searchByEmpid(Employee **, int, int);
// // destructor
~Employee()
{
delete[] name;
}
};
// // define class FullTime by inheriting class Employee
class FullTime : public Employee
{
private:
// // instance member variables
int numberOfDays, dailyRate, salary;
public:
// // static member variable
static const char status[];
// // instance member function to set numberOfDays
void setNumberOfDays(int numberOfDays)
{
this->numberOfDays = numberOfDays;
}
// // instance member function to get numberOfDays
int getNumberOfDays() const
{
return numberOfDays;
}
// // instance member function to set dailyRate
void setDailyRate(int dailyRate)
{
this->dailyRate = dailyRate;
}
// // instance member function to get dailyRate
int getDailyRate() const
{
return dailyRate;
}
// // instance member function to calculate salary
int calculateSalary()
{
return this->salary = dailyRate * numberOfDays;
}
// // instance member function to input and set details
void setDetails()
{
Employee::setDetails();
cout << "\nEnter Daily Rate => ";
cin >> dailyRate;
cout << "\nEnter Number of Days => ";
cin >> numberOfDays;
}
// // instance member function to show details
void showDetails()
{
Employee::showDetails();
cout << "\nDaily Rate => " << dailyRate;
cout << "\nNumber of Days => " << numberOfDays;
cout << "\nSalary => " << calculateSalary();
cout << "\nStatus => " << status;
}
};
// // define static member variables of class FullTime
const char FullTime::status[] = "Full Time";
// // define class PartTime by inheriting class Employee
class PartTime : public Employee
{
private:
// // instance member variables
int numberOfHours, hourlyRate, salary;
public:
// // static member variable
static const char status[];
// // instance member function to set numberOfHours
void setNumberOfHours(int numberOfHours)
{
this->numberOfHours = numberOfHours;
}
// // instance member function to get numberOfHours
int getNumberOfHours() const
{
return numberOfHours;
}
// // instance member function to set hourlyRate
void setHourlyRate(int hourlyRate)
{
this->hourlyRate = hourlyRate;
}
// // instance member function to get hourlyRate
int getHourlyRate() const
{
return hourlyRate;
}
// // instance member function to calculate salary
int calculateSalary()
{
return this->salary = hourlyRate * numberOfHours;
}
// // instance member function to input and set details
void setDetails()
{
Employee::setDetails();
cout << "\nEnter Hourly Rate => ";
cin >> hourlyRate;
cout << "\nEnter Number of Hours => ";
cin >> numberOfHours;
}
// // instance member function to show details
void showDetails()
{
Employee::showDetails();
cout << "\nHourly Rate => " << hourlyRate;
cout << "\nNumber of Hours => " << numberOfHours;
cout << "\nSalary => " << calculateSalary();
cout << "\nStatus => " << status;
}
};
// // define static member variables of class PartTime
const char PartTime::status[] = "Part Time";
// // friend function to search employee by id
int searchByEmpid(Employee **ptr, int size, int empId)
{
for (int i = 0; i < size; i++)
{
if (ptr[i]->empId == empId)
return i;
}
return -1;
}
// // Main Function Start
int main()
{
int n, empId, indexOfFoundEmp;
cout << "\nHow Many Employees Details You Want to Enter (MAX " << MAX_EMPS << ") => ";
cin >> n;
// // invalid input
if (n < 1 || n > MAX_EMPS)
{
cout << "\n!!! Invalid Input...";
exit(0);
}
// // create an array of pointers to instances of Employees
Employee *emps[n] = {nullptr};
// // get details
cout << "\n>>>>>>>>> Enter Details of " << n << " Employees <<<<<<<<<<<\n";
for (int i = 0; i < n; i++)
{
cout << "\n>>>>>>>> Enter Details of Employee-" << i + 1 << " <<<<<<<<<<\n";
char empType;
while (true)
{
cout << "\nEnter Employee Type Full Time or Part Time (Press F for Full Time or P for Part Time) => ";
cin.ignore();
cin >> empType;
if (empType == 'F' || empType == 'f' || empType == 'P' || empType == 'p')
break;
else
{
cout << "\n!!!Invalid Employee Type, Please Enter Valid Type. Try Again...\n";
}
}
if (empType == 'F' || empType == 'f')
emps[i] = new FullTime;
else
emps[i] = new PartTime;
emps[i]->setDetails();
}
// // show details
cout << "\n>>>>>>>>> Details of " << n << " Full Time Employees <<<<<<<<<<<\n";
for (int i = 0; i < n; i++)
{
cout << "\n\n>>>>>>>> Details of Employee-" << i + 1 << " <<<<<<<<<<";
emps[i]->showDetails();
}
// // get employee id to search an employee
cout << "\n\nEnter Employee Id to Search An Employee => ";
cin >> empId;
// // search employee by its id
indexOfFoundEmp = searchByEmpid(emps, n, empId);
// // show if employee found
if (indexOfFoundEmp == -1)
cout << "\n\nThere is No Employee Having Id " << empId;
else
{
cout << "\n\n>>>>>>>>>> Employee Found <<<<<<<<<<<\n";
emps[indexOfFoundEmp]->showDetails();
}
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End