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