home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / EMPLIST.CP$ / EMPLIST
Encoding:
Text File  |  1991-12-11  |  3.4 KB  |  150 lines

  1. // EMPLIST.CPP
  2.  
  3. // This is an example program that uses the Employee class from
  4. //     Chapter 7 of the C++ Tutorial. This program defines the
  5. //     EmployeeList class, which stores an array of Employee pointers,
  6. //     and the EmpIter class, which defines an iterator for the
  7. //     EmployeeList class. (See the PhoneList example class from Chapter
  8. //     6 for more information about friend classes and iterators.)
  9. //     The main function in this program allocates three different
  10. //     types of Employee and adds them to an EmployeeList. The
  11. //     computePayroll function adds up the salaries of all the Employees
  12. //     in the list by calling the virtual computePay member function.
  13.  
  14. #include "employee.h"
  15. #include <string.h>
  16. #include <iostream.h>
  17.  
  18. const int MAXLENGTH = 100;
  19.  
  20. class EmployeeList
  21. {
  22. friend class EmpIter;
  23. public:
  24.     EmployeeList();
  25.     int add( Employee *newEmp );
  26.     Employee *search( const char *searchKey );
  27. private:
  28.     Employee *aray[MAXLENGTH];
  29.     int firstEmpty;          // First unused element
  30. };
  31.  
  32. class EmpIter
  33. {
  34. public:
  35.     EmpIter( EmployeeList &m );
  36.     Employee *getFirst();
  37.     Employee *getLast();
  38.     Employee *getNext();
  39.     Employee *getPrev();
  40. private:
  41.     EmployeeList *const mine;     // Constant pointer to EmployeeList object
  42.     int currIndex;
  43. };
  44.  
  45.  
  46. EmployeeList::EmployeeList()
  47. {
  48.     firstEmpty = 0;
  49. }
  50.  
  51. int EmployeeList::add( Employee *newEmp )
  52. {
  53.     if( firstEmpty < MAXLENGTH - 1 )
  54.     {
  55.         aray[firstEmpty++] = newEmp;
  56.         return 1;   // Indicate success
  57.     }
  58.     else return 0;
  59. }
  60.  
  61. Employee *EmployeeList::search( const char *searchKey )
  62. {
  63.     for( int i = 0; i < firstEmpty; i++ )
  64.         if( !strcmp( aray[i]->getName(), searchKey ) )
  65.             return aray[i];
  66.  
  67.     return 0;
  68. }
  69.  
  70. EmpIter::EmpIter( EmployeeList &m )
  71.     : mine( &m )          // Initialize the constant member
  72. {
  73.     currIndex = 0;
  74. }
  75.  
  76. Employee *EmpIter::getFirst()
  77. {
  78.     currIndex = 0;
  79.     return mine->aray[currIndex];
  80. }
  81.  
  82. Employee *EmpIter::getLast()
  83. {
  84.     currIndex = mine->firstEmpty - 1;
  85.     return mine->aray[currIndex];
  86. }
  87.  
  88. Employee *EmpIter::getNext()
  89. {
  90.     if( currIndex < mine->firstEmpty - 1 )
  91.     {
  92.         currIndex++;
  93.         return mine->aray[currIndex];
  94.     } 
  95.     else return 0;
  96. }
  97.  
  98. Employee *EmpIter::getPrev()
  99. {
  100.     if( currIndex > 0 )
  101.     {
  102.         currIndex--;
  103.         return mine->aray[currIndex];
  104.     }
  105.     else return 0;
  106. }
  107.  
  108. float computePayroll( EmployeeList &dept )
  109. {
  110.     float payroll = 0;
  111.     Employee *person;
  112.     EmpIter anIter( dept );
  113.  
  114.     person = anIter.getFirst();
  115.     payroll += person->computePay();
  116.     while( person = anIter.getNext() )
  117.     {
  118.         // Call appropriate function
  119.         //     for each type of employee
  120.         payroll += person->computePay();
  121.     }
  122.  
  123.     return payroll;
  124. }
  125.  
  126. void main()
  127. {
  128.     EmployeeList myDept;
  129.     WageEmployee *wagePtr;
  130.     SalesPerson *salePtr;
  131.     Manager *mgrPtr;
  132.  
  133.     wagePtr = new WageEmployee( "Bill Shapiro" );
  134.     wagePtr->setWage( 8.00 );
  135.     wagePtr->setHours( 40.0 );
  136.     salePtr = new SalesPerson( "John Smith" );
  137.     salePtr->setWage( 8.00 );
  138.     salePtr->setHours( 40.0 );
  139.     salePtr->setSales( 2000.00 );
  140.     salePtr->setCommission( 0.05 );
  141.     mgrPtr = new Manager( "Mary Brown" );
  142.     mgrPtr->setSalary( 1000.00 );
  143.  
  144.     myDept.add( wagePtr );
  145.     myDept.add( salePtr );
  146.     myDept.add( mgrPtr );
  147.  
  148.     cout << "payroll: " << computePayroll( myDept ) << '\n';
  149. }
  150.