-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10_worker_officer_manager.cpp
236 lines (194 loc) · 5.13 KB
/
10_worker_officer_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
// // Consider two base classes worker(int code, char name, float salary), officer(float DA, HRA) class manager(float TA(is 10% of salary), grossSalary) is derived from both base classes. Write necessary member functions.
// // Header files
#include <iostream>
#include <string.h>
#include <stdlib.h>
// // use namespace
using namespace std;
// // define Macro
#define MAX_MANAGERS 20
// // define class Worker
class Worker
{
public:
// // static member variable
static const int MAX_CHARS_IN_NAME = 31;
private:
// // instance member variables
char *name;
int code;
double salary;
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 code
void setCode(int code)
{
this->code = code;
}
// // instance member function to get code
int getCode() const
{
return code;
}
// // instance member function to set salary
void setSalary(double salary)
{
this->salary = salary;
}
// // instance member function to get salary
double getSalary() const
{
return salary;
}
// // instance member function to input and set details
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 Code => ";
cin >> code;
cout << "\nEnter Salary => ";
cin >> salary;
}
// // instance member function to show details
void showDetails() const
{
cout << "\nName => " << name;
cout << "\nCode => " << code;
cout << "\nSalary => " << salary;
}
// // destructor
~Worker()
{
delete[] name;
}
};
// // define class Officer
class Officer
{
private:
// // instance member variables
double DA, HRA;
public:
// // instance member function to set DA
void setDA(double DA)
{
this->DA = DA;
}
// // instance member function to get DA
double getDA()
{
return DA;
}
// // instance member function to set HRA
void setHRA(double HRA)
{
this->HRA = HRA;
}
// // instance member function to get HRA
double getHRA()
{
return HRA;
}
// // instance member function to input and set details
void setDetails()
{
cout << "\nEnter DA => ";
cin >> DA;
cout << "\nEnter HRA => ";
cin >> HRA;
}
// // instance member function to show details
void showDetails() const
{
cout << "\nDA => " << DA;
cout << "\nHRA => " << HRA;
}
};
// // define class FullTime by inheriting class Worker and class Officer
class Manager : public Worker, public Officer
{
private:
// // instance member variables
double TA, grossSalary;
public:
// // instance member function to input and set details
void setDetails()
{
Worker::setDetails();
Officer::setDetails();
}
// // instance member function to show details
void showDetails()
{
Worker::showDetails();
Officer::showDetails();
cout << "\nTA => " << calculateTA();
cout << "\nGross Salary => " << calculateGrossSalary();
}
// // instance member function to calculate grossSalary
double calculateGrossSalary()
{
return grossSalary = calculateTA() + getSalary() + getDA() + getHRA();
}
// // instance member function to calculate TA
double calculateTA()
{
return TA = 10.0 / 100 * getSalary();
}
};
// // 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 pointers to 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();
}
cout << endl; // Add new line
cin.ignore();
return 0;
}
// // Main Function End