-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP_Class.cpp
More file actions
73 lines (58 loc) · 1.91 KB
/
OOP_Class.cpp
File metadata and controls
73 lines (58 loc) · 1.91 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
#include <iostream>
#include <string>
using namespace std; // Using namespace directive
// Employee class definition
class Employee {
private:
// Private member variables
string name;
int age;
double salary;
static int employeeCount; // Static member variable for counting employees
public:
// Constructors
Employee() : name(""), age(0), salary(0.0) {
employeeCount++;
}
Employee(const string& empName, int empAge, double empSalary)
: name(empName), age(empAge), salary(empSalary) {
employeeCount++;
}
// Destructor
~Employee() {
employeeCount--;
}
// Public member functions for encapsulation
void displayDetails() const {
cout << "Employee Details:" << endl;
cout << "Name: " << name << endl;
cout << "Age: " << age << endl;
cout << "Salary: $" << salary << endl;
}
void updateSalary(double newSalary) {
salary = newSalary;
}
static int getEmployeeCount() {
return employeeCount;
}
};
// Initializing static member variable
int Employee::employeeCount = 0;
int main() {
// Creating employee objects
Employee emp1("John Doe", 25, 50000.0);
Employee emp2("Jane Smith", 30, 60000.0);
// Displaying initial employee details
emp1.displayDetails();
emp2.displayDetails();
// Updating employee salary
emp1.updateSalary(55000.0);
// Displaying updated employee details
emp1.displayDetails();
// Displaying the total number of employees
cout << "Total Employees: " << Employee::getEmployeeCount() << endl;
// Employee objects go out of scope and destructors are called
// Displaying the total number of employees after some employees are deleted
cout << "Total Employees after some are deleted: " << Employee::getEmployeeCount() << endl;
return 0;
}