1 | #include <stdio.h> |
---|
2 | #include <string.h> |
---|
3 | |
---|
4 | class Worker { |
---|
5 | private: |
---|
6 | char myName[256]; |
---|
7 | public: |
---|
8 | Worker(const char* name) { |
---|
9 | strcpy(myName, name); |
---|
10 | } |
---|
11 | |
---|
12 | virtual int getSalary() = 0; |
---|
13 | |
---|
14 | virtual ~Worker() {} |
---|
15 | }; |
---|
16 | |
---|
17 | class Developer: public Worker { |
---|
18 | private: |
---|
19 | int myLevel; |
---|
20 | int mySalary; |
---|
21 | public: |
---|
22 | Developer(const char* name, int salary) : Worker(name) { |
---|
23 | mySalary = salary; |
---|
24 | myLevel = 1; |
---|
25 | } |
---|
26 | |
---|
27 | void setLevel(int level) { |
---|
28 | myLevel = level; |
---|
29 | } |
---|
30 | |
---|
31 | virtual int getSalary() { |
---|
32 | return mySalary * myLevel; |
---|
33 | } |
---|
34 | }; |
---|
35 | |
---|
36 | class Salesman: public Worker { |
---|
37 | private: |
---|
38 | double myPercent; |
---|
39 | int myProductPrice; |
---|
40 | int myProductQuantity; |
---|
41 | public: |
---|
42 | Salesman(const char* name, int productPrice, double percent) : Worker(name) { |
---|
43 | myProductPrice = productPrice; |
---|
44 | myPercent = percent; |
---|
45 | myProductQuantity = 0; |
---|
46 | } |
---|
47 | |
---|
48 | void incQuantity() { |
---|
49 | myProductQuantity++; |
---|
50 | } |
---|
51 | |
---|
52 | virtual int getSalary() { |
---|
53 | return myProductPrice * myProductQuantity * myPercent; |
---|
54 | } |
---|
55 | }; |
---|
56 | |
---|
57 | |
---|
58 | class WorkerBase { |
---|
59 | private: |
---|
60 | Worker** myWorkers; |
---|
61 | int myCapacity; |
---|
62 | int mySize; |
---|
63 | |
---|
64 | public: |
---|
65 | WorkerBase(int capacity) { |
---|
66 | myCapacity = capacity; |
---|
67 | mySize = 0; |
---|
68 | myWorkers = new Worker* [capacity]; |
---|
69 | } |
---|
70 | |
---|
71 | ~WorkerBase() { |
---|
72 | //? |
---|
73 | } |
---|
74 | |
---|
75 | void add(Worker* w) { |
---|
76 | if(mySize + 1 > myCapacity) { |
---|
77 | // ? |
---|
78 | } |
---|
79 | else { |
---|
80 | myWorkers[mySize++] = w; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | int getTotalSalary() { |
---|
85 | int salary = 0; |
---|
86 | for(int i = 0; i < mySize; i++) { |
---|
87 | salary += myWorkers[i]->getSalary(); |
---|
88 | } |
---|
89 | return salary; |
---|
90 | } |
---|
91 | }; |
---|
92 | |
---|
93 | int main() { |
---|
94 | |
---|
95 | Developer* dev = new Developer("vasya", 2000); |
---|
96 | dev->setLevel(1); |
---|
97 | |
---|
98 | Developer* senior = new Developer("nina", 2000); |
---|
99 | senior->setLevel(2); |
---|
100 | |
---|
101 | Salesman* sale = new Salesman("petya", 1000, 0.1); |
---|
102 | sale->incQuantity(); |
---|
103 | sale->incQuantity(); |
---|
104 | sale->incQuantity(); |
---|
105 | |
---|
106 | WorkerBase wb(100); |
---|
107 | wb.add(dev); |
---|
108 | wb.add(senior); |
---|
109 | wb.add(sale); |
---|
110 | |
---|
111 | printf("%d\n", wb.getTotalSalary()); |
---|
112 | |
---|
113 | return 0; |
---|
114 | } |
---|