home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / trialva / ibmcppw / samples / compiler / rtti / user.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-20  |  2.4 KB  |  110 lines

  1. #include <iostream.h>
  2. #include <new.h>
  3. #include <typeinfo.h>
  4. #include "impl.h"
  5. #include "user.h"
  6.  
  7. employeeList* employeeList::first = 0;
  8. employeeList* employeeList::last = 0;
  9.  
  10. employeeList::~employeeList() 
  11. {
  12.   delete Empl;  
  13. }
  14.  
  15. void employeeList::addEmployee(employee* e)
  16. {
  17.   if (first == 0) 
  18.   {
  19.     first = last = new employeeList;
  20.   }
  21.   else 
  22.   {
  23.     last->Next = new employeeList;
  24.     last = last->Next;
  25.   }
  26.   last->Empl = e;
  27. }
  28.  
  29. company::company() 
  30. {
  31.   EmployeeList = new employeeList;
  32. }
  33.  
  34. company::~company()
  35. {
  36.   employeeList* el = EmployeeList;
  37.   employeeList* next = (el != 0) ? el->next() : 0; 
  38.   while (el != 0) 
  39.   {
  40.     delete el->empl();
  41.     delete el;
  42.     el = next;
  43.     next = (el != 0) ? el->next() : 0;
  44.   }
  45. }
  46.  
  47. void company::addManager(char* name, int hourRate, int experience)
  48. {
  49.   manager* m = new manager(name, hourRate, experience);
  50.   EmployeeList->addEmployee(m); 
  51. }
  52.  
  53. //
  54. // Because the member functions are not virtual, the user is 
  55. // obliged to ensure that the added member functions 
  56. // (setOvertimeRate, addOvertime, resetOvertime and overtimePay)
  57. // are used only if the employee referred to is a programmer.
  58. // A dynamic_cast or a typeid can be used to ensure the employee
  59. // referred to is a programmer.
  60. //
  61.  
  62. void company::addProgrammer(char* name, int hourRate, int ovRate)
  63. {
  64.   programmer* p = new programmer(name, hourRate);
  65.   p->setOvertimeRate(ovRate);
  66.   EmployeeList->addEmployee(p);
  67. }
  68.  
  69. void company::addOvertime(char* name, int overtime)
  70. {
  71.   for (employeeList* el = EmployeeList->first;
  72.        el != 0;
  73.        el = el->next()) 
  74.   {
  75.     if (!strcmp(name, el->empl()->name())) 
  76.     {
  77.       programmer* pe = dynamic_cast<programmer*>(el->empl());
  78.       if (pe != 0) 
  79.         pe->addOvertime(overtime);
  80.  
  81.       return;
  82.     }
  83.   }  
  84. }
  85.  
  86. int company::payRoll()
  87. {
  88.   int expense = 0;
  89.   
  90.   for (employeeList* el = EmployeeList->first; 
  91.        el != 0;
  92.        el = el->next())
  93.   {
  94.     int pay = el->empl()->salary();
  95.     programmer* pe = dynamic_cast<programmer*>(el->empl());
  96.     if (pe != 0)
  97.     {
  98.       pay += pe->overtimePay();
  99.       pe->resetOvertime();
  100.     }
  101.     cout << el->empl()->name() << " is a " << typeid(*(el->empl())).name()
  102.          << " who was paid $" << pay << " this week." << endl;
  103.     expense += pay;
  104.   }
  105.   cout << "The total expense in salary this week is " << expense << "." << endl;
  106.  
  107.   return expense;
  108. }
  109.  
  110.