-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion4.cpp
More file actions
52 lines (51 loc) · 2 KB
/
question4.cpp
File metadata and controls
52 lines (51 loc) · 2 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
/*Develop a class Employee with private data for employee name, a shift array for
a month, and total hours worked. Provide constructors for initialization and
methods for updating shifts. Include a static method to compute the total working
hours for all employees.*/
#include <iostream>
#include <string>
using namespace std;
class Employee {
string name;
int month[30];
int total_hours;
static int total_company_hours;
public:
Employee(string m = ""):name(m),total_hours(0){
for (int i=0;i<30;i++){
month[i] =0;}}
void update_shift(int day, int hours){
if (day <1 || day >30) {
cout <<"Invalid day! Please enter a day between 1 and 30." <<endl;}
else{
month[day -1] =hours;
total_hours =total_hours+hours;
total_company_hours =total_company_hours+hours;}}
void display()const{
cout <<"Employee Name: "<<name<<endl;
cout <<"Total Hours Worked: "<<total_hours <<endl;}
static int get_total_company_hours(){
return total_company_hours;}};
int Employee::total_company_hours=0;
int main() {
cout << "Enter the number of employees (max 100): ";
int n;
cin >> n;
if (n < 1 || n > 100) {
cout << "Invalid number of employees! Please enter a number between 1 and 100." << endl;}
Employee employees[100];
for (int i = 0; i < n; i++) {
cout << "Enter name for employee "<< i + 1 << ": ";
string name;
cin >> name;
employees[i] = Employee(name);
for (int j = 0; j < 30; j++) {
cout << "Enter working hours for day " << j + 1 << ": ";
int hours;
cin >> hours;
employees[i].update_shift(j + 1, hours);} }
for (int i = 0; i < n; i++) {
cout << "Details for Employee " << i + 1 << ":" << endl;
employees[i].display();}
cout << "Total working hours for all employees: " << Employee::get_total_company_hours() << endl;
return 0;}