-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassesExample.cpp
More file actions
63 lines (57 loc) · 1.45 KB
/
classesExample.cpp
File metadata and controls
63 lines (57 loc) · 1.45 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
#include <iostream>
#include <vector>
using namespace std;
// Abstract base class
class Employee {
protected:
string name;
double salary;
public:
Employee(string n, double s) : name(n), salary(s) {}
virtual void work()=0; // pure virtual
virtual void showInfo() {
cout << name << " earns " << salary << endl;
}
virtual ~Employee() {
cout<<"Employee Destructor\n";
}
};
// Derived class
class Developer : public Employee {
private:
string language;
public:
Developer(string n, double s, string lang)
: Employee(n, s), language(lang) {}
void work() override {
cout << name << " is coding in " << language << endl;
}
void showInfo() override { // overriding the base version
cout << name << " earns " << salary
<< " and codes in " << language << endl;
}
~Developer() {
cout<<"Developer Destructor"<<endl;
}
};
// Derived class
class Manager : public Employee {
public:
Manager(string n, double s) : Employee(n, s) {}
void work() override {
cout << name << " is managing projects" << endl;
}
~Manager() {
cout<<"Manager Destructor"<<endl;
}
};
int main() {
vector<Employee*> team;
team.push_back(new Developer("Alice", 70000, "C++"));
team.push_back(new Manager("Bob", 90000));
for (auto emp : team) {
emp->showInfo();
emp->work();
}
for (auto emp : team) delete emp; // Cleanup
}