-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet02.cpp
More file actions
108 lines (87 loc) · 2.75 KB
/
Copy pathSet02.cpp
File metadata and controls
108 lines (87 loc) · 2.75 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
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
/* [OOP Practical Eaxm 2025 - Set 02]
a) Develop a C++ program using Object-Oriented Programming concepts
to perform the following tasks:
Define a base class Employee with data members emplD, name and a static data member
count to keep track of the total number of employees created.
b) Create a parameterized constructor in Employee to initialize the data members and increment
count each time a new object is created. Also create a destructor for the class displaying
an appropriate message.
c) Derive a class Manager from Employee. Use constructor delegation to call the base class
constructor from the derived class constructor. Add a protected data member department in the
derived class. Also create a destructor for the derived class displaying an appropriate message.
d) In the main() function:
Create three Manager objects by taking input from the user for emplD, name and department.
Display each manager's details.
Finally, display the total number of employees created using the static member.
*/
#include <iostream>
using namespace std;
class Employee {
protected:
int empID;
string name;
static int count;
public:
Employee(int id, string n) {
empID = id;
name = n;
count++;
cout << "Employee Constructor Called\n";
}
void displayEmployee() {
cout << "Employee ID : " << empID << endl;
cout << "Employee Name : " << name << endl;
}
static int getCount() {
return count;
}
~Employee() {
cout << "Employee Destructor Called for ID: " << empID << endl;
}
};
int Employee::count = 0;
class Manager : public Employee {
protected:
string department;
public:
// Constructor delegation to base class
Manager(int id, string n, string dept)
: Employee(id, n) {
department = dept;
cout << "Manager Constructor Called\n";
}
void displayManager() {
displayEmployee();
cout << "Department : " << department << endl;
cout << "---------------------------\n";
}
~Manager() {
cout << "Manager Destructor Called\n";
}
};
int main() {
int id;
string name, dept;
Manager* m[3];
for (int i = 0; i < 3; i++) {
cout << "\nEnter details for Manager " << i + 1 << endl;
cout << "Employee ID: ";
cin >> id;
cin.ignore();
cout << "Name: ";
getline(cin, name);
cout << "Department: ";
getline(cin, dept);
m[i] = new Manager(id, name, dept);
}
cout << "\n--- Manager Details ---\n";
for (int i = 0; i < 3; i++) {
m[i]->displayManager();
}
cout << "Total Employees Created: "
<< Employee::getCount() << endl;
for (int i = 0; i < 3; i++) {
delete m[i];
}
return 0;
}