"Работники"

Материал из SEWiki
Перейти к: навигация, поиск
#include <stdio.h>
#include <string.h>

class Worker {
private:
        char myName[256];
public:
        Worker(const char* name) {
                strcpy(myName, name);
        }                
        
        virtual int getSalary() = 0;
        
        virtual ~Worker() {}
};

class Developer: public Worker {
private:
        int myLevel;
        int mySalary;
public:
        Developer(const char* name, int salary) : Worker(name) {            
                mySalary = salary;
                myLevel = 1;
        }
        
        void setLevel(int level) {
                myLevel = level;
        }
        
        virtual int getSalary() {
                return mySalary * myLevel;
        }
};

class Salesman: public Worker {
private:
        double myPercent;
        int myProductPrice;
        int myProductQuantity;
public:
        Salesman(const char* name, int productPrice, double percent) : Worker(name) {            
                myProductPrice = productPrice;
                myPercent = percent;
                myProductQuantity = 0;
        }

        void incQuantity() {
               myProductQuantity++; 
        }
                
        virtual int getSalary() {
                return myProductPrice * myProductQuantity * myPercent;
        }
};


class WorkerBase {
private:     
        Worker** myWorkers;        
        int myCapacity;
        int mySize;
        
public:
        WorkerBase(int capacity) {
                myCapacity = capacity;
                mySize = 0;
                myWorkers = new Worker* [capacity];
        }        
 
        ~WorkerBase() {
                //?
        }
 
       void add(Worker* w) {
                if(mySize + 1 > myCapacity) {
                        // ?
                }         
                else {
                        myWorkers[mySize++] = w;        
                }
       }
       
       int getTotalSalary() {
                int salary = 0;
                for(int i = 0; i < mySize; i++) {
                        salary += myWorkers[i]->getSalary();
                }
                return salary;
       } 
};

int main() {

        Developer* dev = new Developer("vasya", 2000);
        dev->setLevel(1);
        
        Developer* senior = new Developer("nina", 2000);
        senior->setLevel(2);
        
        Salesman* sale = new Salesman("petya", 1000, 0.1);
        sale->incQuantity();
        sale->incQuantity();
        sale->incQuantity();                

        WorkerBase wb(100);
        wb.add(dev);
        wb.add(senior);
        wb.add(sale);
        
        printf("%d\n", wb.getTotalSalary());

        return 0;
}