home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / PROG_C / CL187A.ZIP / EXAMP205.CPP < prev    next >
C/C++ Source or Header  |  1994-03-15  |  2KB  |  103 lines

  1. // Container Lite (CL v 1.87a)
  2. // (C) Copyright John Webster Small 1994
  3. // All rights reserved
  4. // examp205.cpp -- link with cl.obj
  5.  
  6. // #define CL_NO_TEMPLATES
  7.  
  8. #include "cl.h"
  9.  
  10. class Employee  {
  11.     char *name;
  12.     unsigned salary;
  13.     int cmp(const Employee& e) const;
  14.     static    Employee * THIS;
  15.     static    ostream& SHOW(ostream& os)
  16.     {
  17.        return os << "Employee name: "
  18.           << setw(20)
  19.           << (THIS->name? THIS->name : "n/a")
  20.           << "\tsalary: " << THIS->salary;
  21.     }
  22. public:
  23.     Employee(const char * name = 0,
  24.         unsigned salary = 0)
  25.     {
  26.         this->name = (name? strdup(name) : 0);
  27.         this->salary = salary;
  28.     }
  29.     Employee(const Employee& e)
  30.     {
  31.         name = (e.name? strdup(e.name) : 0);
  32.         salary = e.salary;
  33.     }
  34.     Employee& operator=(const Employee& e)
  35.     {
  36.         delete name;
  37.         name = (e.name? strdup(e.name) : 0);
  38.         salary = e.salary;
  39.         return *this;
  40.     }
  41.     int operator==(const Employee& e) const
  42.         { return !cmp(e); }
  43.     int operator>(const Employee& e) const
  44.         { return (cmp(e) > 0); }
  45.     ~Employee() { delete name; }
  46.     ostream& (*show())(ostream&)
  47.         { THIS = this; return SHOW; }
  48.     friend ostream& operator<<
  49.         (ostream& os, Employee& e)
  50.     { return os << &e.name << endm << e.salary; }
  51.     friend istream& operator>>
  52.         (istream& is, Employee& e)
  53.     { return is >> &e.name >> nextm >> e.salary; }
  54. };
  55.  
  56. Employee * Employee::THIS;
  57.  
  58. int Employee::cmp(const Employee& e) const
  59. {
  60.     if (!name)
  61.         if (!e.name)
  62.             return 0;
  63.         else
  64.             return -1;
  65.     else
  66.         if (!e.name)
  67.             return 1;
  68.         else
  69.            return strcmp(name,e.name);
  70. }
  71.  
  72.  
  73. #include "cl.h"
  74. #if defined(CL_NO_TEMPLATES)
  75.     #define   ITEM      Employee
  76.     #define   CL_WELL_ENDOWED
  77.     #define   CL        CL_Employee
  78.     #include "cl.hf"
  79. #else
  80.     CL_WELL_ENDOWED(Employee)
  81.     #define   CL_Employee       CL<Employee>
  82. #endif
  83.  
  84. #define EmployeeFile "employs.tmp"
  85.  
  86. main()
  87. {
  88.     CL_Employee cE(CL_ANDS);
  89.     cE.ins(new Employee("Doe, John",1000));
  90.     Employee E("Small, John",100);
  91.     cE.insQNew(&E);
  92.     cE.push(new Employee("Morgan, Maria",10000));
  93.     cE.save(EmployeeFile);
  94.     cE.allClr();
  95.     cE.load(EmployeeFile);
  96.     cE.sort();
  97.     cout << "\nEmployees in alphabetical order: "
  98.         << endl;
  99.     while (cE.nextAsg(&E))
  100.         cout << E.show() << endl;
  101.     return 0;
  102. }
  103.