home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch12 / employee.cpp < prev    next >
C/C++ Source or Header  |  2005-06-16  |  2KB  |  107 lines

  1. //                   employee.cpp
  2. //
  3. // Contains the implementation of functions in
  4. // the base class employee and the derived class manager.
  5. //
  6.  
  7. // Include Files
  8. #include <iostream.h>
  9. #include <string.h>
  10. #include "employee.h"
  11.  
  12. // The employee class functions
  13. employee::employee()
  14. {
  15.     name = new char[1];
  16.     name[0] = '0';
  17.     salary = 0;
  18.     groupno = 0;
  19. }
  20.  
  21. employee::employee( const employee& e )
  22. {
  23.     name = NULL;
  24.     *this = e;                                           // Note 1
  25. }
  26.  
  27. employee::employee( char * n, int s, int g )
  28. {
  29.     name = new char[ strlen( n ) + 1 ];
  30.     strcpy( name, n );
  31.     if ( s >= 0 )
  32.         salary = s;
  33.     else salary = 0;
  34.  
  35.     if ( g > 0 )
  36.         groupno = g;
  37.     else groupno = 0;
  38. }
  39.  
  40. employee::~employee()
  41. {
  42.     delete [] name;
  43. }
  44.  
  45. bool employee::salary_set( int s )
  46. {
  47.     if ( s >= 0 ) {
  48.         salary = s;
  49.         return true;
  50.     }
  51.     return false;
  52. }
  53.  
  54. void employee::print() const
  55. {
  56.     cout << "Name: " << name
  57.          << "\tSalary: " << salary
  58.          << "\tDepartment: " << groupno << endl;
  59. }
  60.  
  61. void employee::change_group( int g ) 
  62. {
  63.     if ( g >= 0 )
  64.         groupno = g;
  65. }
  66.  
  67. employee& employee::operator= ( const employee& emp )
  68. {
  69.     char * tmpname = new char[ strlen( emp.name ) + 1 ];
  70.     strcpy( tmpname, emp.name );
  71.     delete[] name;
  72.     name = tmpname;
  73.     salary = emp.salary;
  74.     groupno = emp.groupno;
  75.     return *this;
  76. }
  77.  
  78. // The manager class functions
  79.  
  80. manager::manager( char * n, int s, int g, int o ): 
  81.     employee( n, s, g ), office( o ), num_in_group( 0 )  // Note 2
  82. {}                                                       // Note 3
  83.  
  84. bool manager::set_group( employee& e )
  85. {
  86.     if ( num_in_group >= 10 )
  87.         return false;
  88.     group[num_in_group] = &e;
  89.     e.change_group( this->get_group() );                 // Note 4
  90.     num_in_group++;
  91.     return true;
  92. }
  93.  
  94. void manager::print() const
  95. {
  96.     cout << "MANAGER: "; 
  97.  
  98.     employee::print();                                   // Note 5
  99.     cout << "Office: " << office
  100.          << "\tNumber in the group: " << num_in_group << endl;
  101.     if ( num_in_group > 0 ) {
  102.         cout << "Group Members:\n-----------------------------\n";
  103.         for ( int i = 0; i < num_in_group; i++ )
  104.             group[i]->print();
  105.     }
  106. }
  107.