StefiIOE

Employee polimorfizam

May 17th, 2020
149
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<cstring>
  3.  
  4. using namespace std;
  5.  
  6. class Employee {
  7.  
  8. protected:
  9.   char name[50];
  10.   int age;
  11.   int experience;
  12.  
  13. public:
  14.   Employee( const char * name, int age, int experience) {
  15.     strcpy(this->name,name);
  16.     this->age = age;
  17.     this->experience = experience;
  18.   }
  19.  
  20.   int getAge()  {
  21.     return age;
  22.   }
  23.  
  24.   virtual double plata()  = 0;
  25.   virtual double bonus() =0;
  26.   virtual ~Employee() {}
  27. };
  28.  
  29. bool operator == ( Employee &levo, Employee &desno) {
  30.   return levo.getAge() == desno.getAge()&&levo.bonus() == desno.bonus();
  31. }
  32. class SalaryEmployee : public Employee {
  33. private:
  34.   double basicSalary;
  35.  
  36.  
  37. public:
  38.   SalaryEmployee(char * name, int age, int experience, int basicSalary)
  39.   : Employee(name,age, experience) {
  40.     this->basicSalary = basicSalary;
  41.    
  42.   }
  43.  
  44.  
  45.   double bonus() {
  46.     return (experience * basicSalary) / 100.0;
  47.   }
  48.  
  49.   double plata() {
  50.     return basicSalary + bonus();
  51.   }
  52. };
  53.  
  54.  
  55. class HourlyEmployee : public Employee {
  56. private:
  57.   int hoursWorked;
  58.   double hourlyPay;
  59.  
  60. public:
  61.     HourlyEmployee(const char * name, int age, int experience, int hoursWorked, double hourlyPay)
  62.   : Employee(name, age, experience) {
  63.     this->hoursWorked = hoursWorked;
  64.     this->hourlyPay = hourlyPay;
  65.    
  66.   }
  67.  
  68.  
  69.   double bonus(){
  70.     if (hoursWorked > 320) {
  71.       return (hoursWorked - 320) * 0.5 * hourlyPay;
  72.     }
  73.     else
  74.     return 0;
  75.   }
  76.   double plata() {
  77.     return hoursWorked * hourlyPay + bonus();
  78.   }
  79.  
  80. };
  81.  
  82. class Freelancer : public Employee {
  83. private:
  84.   int num;
  85.   double projects[10];
  86. public:
  87.   Freelancer(const char * name, int age, int experience, int num, double projects[])
  88.   : Employee(name,age,experience) {
  89.         this->num = num;
  90.     for (int i=0; i < num; ++i) {
  91.       this->projects[i] = projects[i];
  92.     }
  93.    
  94.   }
  95.   double bonus() {
  96.       if (num > 5) {
  97.         return (num - 5) * 1000.0;
  98.       }
  99.       else
  100.         return 0;
  101.   }
  102.  
  103.   double plata() {
  104.     double sum = 0;
  105.     for (int i=0; i < num; ++i) {
  106.       sum += projects[i];
  107.     }
  108.     return sum + bonus();
  109.   }
  110. };
  111.  
  112.  
  113.  
  114. class Company {
  115. private:
  116.   char name[50];
  117.   int number;
  118.   Employee ** employee;
  119. public:
  120.   Company( char* name) {
  121.     number = 0;
  122.     strcpy(this->name,name);
  123.     employee = new Employee*[0];
  124.   }
  125.  
  126.   Company & operator += (Employee *emp) {
  127.     Employee ** tmp = new Employee*[number + 1];
  128.  
  129.     for (int i=0; i < number; ++i) {
  130.       tmp[i] = employee[i];
  131.     }
  132.     tmp[number++] = emp;
  133.     delete [] employee;
  134.     employee = tmp;
  135.     return * this;
  136.   }
  137.  
  138.   double vkupnaPlata() {
  139.     double sum = 0;
  140.     for (int i=0; i < number; ++i) {
  141.       sum += employee[i] -> plata();
  142.     }
  143.     return sum;
  144.   }
  145.  
  146.   void pecatiRabotnici()
  147.     {
  148.     int count=0 , count1=0, count2=0;
  149.         for(int i = 0 ; i < number ; i++){
  150.     SalaryEmployee *s=dynamic_cast<SalaryEmployee*>(employee[i]);
  151.         if(s!=0)
  152.         {
  153.         count++;
  154.         }
  155.         HourlyEmployee *h=dynamic_cast<HourlyEmployee*>(employee[i]);
  156.         if(h!=0)
  157.         {
  158.         count1++;
  159.         }
  160.         Freelancer*f=dynamic_cast<Freelancer*>(employee[i]);
  161.         if(f!=0)
  162.         {
  163.         count2++;
  164.         }
  165.    
  166.    
  167.     }
  168.         cout << "Vo kompanijata " << name << " rabotat:" <<endl;
  169.         cout<<"Salary employees: "<<count<<endl;
  170.         cout<<"Hourly employees: "<<count1<<endl;
  171.         cout<<"Freelancers: "<<count2<<endl;
  172.     }  
  173.  
  174.   double filtriranaPlata(Employee  *emp) {
  175.     double plata = 0;
  176.     for (int i=0; i < number; ++i) {
  177.       if (*emp == *employee[i]) {
  178.         plata += employee[i] -> plata();
  179.       }
  180.     }
  181.     return plata;
  182.   }
  183.  
  184. /*  ~Company() {
  185.     for (int i=0; i < number; ++i) {
  186.       delete employee[i];
  187.     }
  188.     delete [] employee;
  189.   }*/
  190.  
  191. };
  192. int main() {
  193.  
  194. char name[50];
  195. cin >> name;
  196. Company c(name);
  197.  
  198. int n;
  199. cin >> n;
  200.  
  201. char employeeName[50];
  202. int age;
  203. int experience;
  204. int type;
  205.  
  206. for (int i=0; i <n; ++i) {
  207.   cin >> type;
  208.   cin >> employeeName >> age >> experience;
  209.  
  210.   if (type == 1) {
  211.     int basicSalary;
  212.     cin >> basicSalary;
  213.     c += new SalaryEmployee(employeeName, age, experience, basicSalary);
  214.   }
  215.  
  216.   else if (type == 2) {
  217.     int hoursWorked;
  218.     int hourlyPay;
  219.     cin >> hoursWorked >> hourlyPay;
  220.     c += new HourlyEmployee(employeeName, age, experience, hoursWorked, hourlyPay);
  221.   }
  222.  
  223.   else {
  224.     int numProjects;
  225.     cin >> numProjects;
  226.     double projects[10];
  227.     for (int i=0; i < numProjects; ++i) {
  228.       cin >> projects[i];
  229.     }
  230.     c += new Freelancer(employeeName, age, experience, numProjects, projects);
  231.   }
  232. }
  233.  
  234. c.pecatiRabotnici();
  235. cout << "Vkupnata plata e: " << c.vkupnaPlata() << endl;
  236. Employee * emp = new HourlyEmployee("Petre_Petrov",31,6,340,80);
  237. cout << "Filtriranata plata e: " << c.filtriranaPlata(emp);
  238.  
  239. delete emp;
  240. }
RAW Paste Data