-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path04_person_emp_manager.cpp
348 lines (284 loc) · 9.38 KB
/
04_person_emp_manager.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// // Write a C++ program to design a base class Person (name, address, phoneNumber). Derive a class Employee (empId) from Person. Derive a class Manager (designation, departmentName, basicSalary) from Employee. Now, Accept all details of 'n' managers and display manager having highest salary.
// // Header files
#include <iostream>
#include <string.h>
#include <stdlib.h>
// // use namespace
using namespace std;
// // define Macro
#define MAX_MANAGERS 20
// // define class Person
class Person
{
public:
// // static member variable
static const int MAX_CHARS_IN_NAME = 31;
static const int MAX_CHARS_IN_ADDRESS = 21;
static const int MAX_DIGITS_IN_NUMBER = 11;
private:
// // instance member variables
char *name, *address, *phoneNumber;
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 address
void setAddress(const char *address)
{
int length = strlen(address);
this->address = new char[length > MAX_CHARS_IN_ADDRESS - 1 ? MAX_CHARS_IN_ADDRESS : length + 1];
if (length > MAX_CHARS_IN_ADDRESS - 1)
{
strncpy(this->address, address, MAX_CHARS_IN_ADDRESS - 1);
this->address[MAX_CHARS_IN_ADDRESS - 1] = 0; // // terminate with null character
}
else
{
strcpy(this->address, address);
}
}
// // instance member function to get address
const char *getAddress() const
{
return address;
}
// // instance member function to set phoneNumber
void setPhoneNumber(const char *phoneNumber)
{
this->phoneNumber = new char[MAX_DIGITS_IN_NUMBER];
if (strlen(phoneNumber) > MAX_DIGITS_IN_NUMBER - 1)
{
strncpy(this->phoneNumber, phoneNumber, MAX_DIGITS_IN_NUMBER - 1);
this->phoneNumber[MAX_DIGITS_IN_NUMBER - 1] = 0; // // terminate with null character
}
else
{
strcpy(this->phoneNumber, phoneNumber);
}
}
// // instance member function to get phoneNumber
const char *getPhoneNumber() const
{
return phoneNumber;
}
// // instance member function to input and set details
void setDetails()
{
char name[MAX_CHARS_IN_NAME], address[MAX_CHARS_IN_ADDRESS], phoneNumber[MAX_DIGITS_IN_NUMBER];
cout << "\nEnter Name (MAX_CHARS " << MAX_CHARS_IN_NAME - 1 << ") => ";
cin.ignore();
cin.getline(name, MAX_CHARS_IN_NAME);
setName(name);
cout << "\nEnter Address (MAX_CHARS " << MAX_CHARS_IN_ADDRESS - 1 << ") => ";
cin.getline(address, MAX_CHARS_IN_ADDRESS);
setAddress(address);
cout << "\nEnter Phone Number (MAX_CHARS " << MAX_DIGITS_IN_NUMBER - 1 << ") => ";
cin.getline(phoneNumber, MAX_DIGITS_IN_NUMBER);
setPhoneNumber(phoneNumber);
}
// // instance member function to show details
void showDetails() const
{
cout << "\nName => " << name;
cout << "\nPhone Number => " << phoneNumber;
cout << "\nAddress => " << address;
}
// // destructor
~Person()
{
delete[] name;
delete[] address;
delete[] phoneNumber;
}
};
// // define class Employee by inheriting class Person
class Employee : public Person
{
private:
// // instance member variables
int empId;
public:
// // 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
void setDetails()
{
Person::setDetails();
cout << "\nEnter Employee Id => ";
cin >> empId;
}
// // instance member function to show details
void showDetails()
{
Person::showDetails();
cout << "\nEmployee Id => " << empId;
}
};
// // define class Manager by inheriting class Employee
class Manager : public Employee
{
public:
// // static member variable
static const int MAX_CHARS_IN_DESIGNATION = 51;
static const int MAX_CHARS_IN_DEPARTMENT_NAME = 51;
private:
// // instance member variables
char *designation, *departmentName;
double basicSalary;
public:
// // instance member function to set designation
void setDesignation(const char *designation)
{
int length = strlen(designation);
this->designation = new char[length > MAX_CHARS_IN_DESIGNATION - 1 ? MAX_CHARS_IN_DESIGNATION : length + 1];
if (length > MAX_CHARS_IN_DESIGNATION - 1)
{
strncpy(this->designation, designation, MAX_CHARS_IN_DESIGNATION - 1);
this->designation[MAX_CHARS_IN_DESIGNATION - 1] = 0; // // terminate with null character
}
else
{
strcpy(this->designation, designation);
}
}
// // instance member function to get designation
const char *getDesignation() const
{
return designation;
}
// // instance member function to set departmentName
void setDepartmentName(const char *departmentName)
{
int length = strlen(departmentName);
this->departmentName = new char[length > MAX_CHARS_IN_DEPARTMENT_NAME - 1 ? MAX_CHARS_IN_DEPARTMENT_NAME : length + 1];
if (length > MAX_CHARS_IN_DEPARTMENT_NAME - 1)
{
strncpy(this->departmentName, departmentName, MAX_CHARS_IN_DEPARTMENT_NAME - 1);
this->departmentName[MAX_CHARS_IN_DEPARTMENT_NAME - 1] = 0; // // terminate with null character
}
else
{
strcpy(this->departmentName, departmentName);
}
}
// // instance member function to get departmentName
const char *getDepartmentName() const
{
return departmentName;
}
// // instance member function to set basicSalary
void setBasicSalary(double basicSalary)
{
this->basicSalary = basicSalary;
}
// // instance member function to get basicSalary
double getBasicSalary() const
{
return basicSalary;
}
// // instance member function to input and set details
void setDetails()
{
char designation[MAX_CHARS_IN_DESIGNATION], departmentName[MAX_CHARS_IN_DEPARTMENT_NAME];
Employee::setDetails();
cout << "\nEnter Designation (MAX_CHARS " << MAX_CHARS_IN_DESIGNATION - 1 << ") => ";
cin.ignore();
cin.getline(designation, MAX_CHARS_IN_DESIGNATION);
setDesignation(designation);
cout << "\nEnter Department Name (MAX_CHARS " << MAX_CHARS_IN_DEPARTMENT_NAME - 1 << ") => ";
cin.getline(departmentName, MAX_CHARS_IN_DEPARTMENT_NAME);
setDepartmentName(departmentName);
cout << "\nEnter Basic Salary => ";
cin >> basicSalary;
}
// // instance member function to show details
void showDetails()
{
Person::showDetails();
cout << "\nDesignation => " << designation;
cout << "\nDepartment Name => " << departmentName;
cout << "\nBasic Salary => " << basicSalary;
}
// // destructor
~Manager()
{
delete[] designation;
delete[] departmentName;
}
// // friend function to find manager with the highest salary
friend int highestSalary(Manager[], int);
};
// // friend function to find manager with the highest salary
int highestSalary(Manager managers[], int size)
{
int highestIndex = 0;
for (int i = 0; i < size; i++)
{
if (managers[i].basicSalary > managers[highestIndex].basicSalary)
highestIndex = i;
}
return highestIndex;
}
// // Main Function Start
int main()
{
int n;
cout << "\nHow Many Managers Details You Want to Enter (MAX " << MAX_MANAGERS << " ) => ";
cin >> n;
// // invalid input
if (n < 1 || n > MAX_MANAGERS)
{
cout << "\n!!! Invalid Input...";
exit(0);
}
// // create an array of instances of Manager
Manager managers[n];
// // get details
cout << "\n>>>>>>>>> Enter Details of " << n << " Managers <<<<<<<<<<<\n";
for (int i = 0; i < n; i++)
{
cout << "\n>>>>>>>> Enter Details of Manager-" << i + 1 << " <<<<<<<<<<\n";
managers[i].setDetails();
}
// // show details
cout << "\n>>>>>>>>> Details of " << n << " Managers <<<<<<<<<<<\n";
for (int i = 0; i < n; i++)
{
cout << "\n\n>>>>>>>> Details of Manager-" << i + 1 << " <<<<<<<<<<";
managers[i].showDetails();
}
// // find employee with highest salary
int highestIndex = highestSalary(managers, n);
// // show employee with highest salary
cout << "\n\nManager with the Highest Salary => " << managers[highestIndex].getBasicSalary();
cout << "\nManager Name => " << managers[highestIndex].getName();
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End