/**************************************************************************** ** COPYRIGHT (C): 1997 Cay S. Horstmann. All Rights Reserved. ** PROJECT: Practical OO Development with C++ and Java ** FILE: employee.cpp ** PURPOSE: employee class for chapter 3 ** VERSION 1.0 ** PROGRAMMERS: Cay Horstmann (CSH) ** RELEASE DATE: 3-15-97 (CSH) ** UPDATE HISTORY: ****************************************************************************/ #include "setup.h" EXPORT #include "date.h" EXPORT class Employee { public: Employee(); Employee(string); Employee(string, int id); Employee(string n, int hd, int hm, int hy); void set_id(int id); void set_salary(double s); void raise_salary(double p); virtual void print() const; virtual void print(ostream& os) const; int id() const; double hourly_wage() const; virtual double weekly_pay(double hours) const; virtual ~Employee() {} private: string _name; double _wage; int _id; Date _hiredate; }; /*-------------------------------------------------------------*/ EXPORT class Manager : public Employee /* PURPOSE: A manager supervising employees */ { public: Manager(); Manager(string s); void add(Employee* e); virtual void print(ostream&) const; virtual double weekly_pay(double hours) const; private: #ifndef PRACTICALOO_MUST_USE_ALLOCATOR vector _superv; #else vector > _superv; #endif // ID numbers of supervised employees }; /*-------------------------------------------------------------*/ Employee::Employee() : _wage(0), _id(0), _hiredate(Date::today().day(), Date::today().month(), Date::today().year()) {} /*.............................................................*/ Employee::Employee(string n) /* RECEIVES: n - name */ : _name(n), _wage(0), _id(0), _hiredate(Date::today().day(), Date::today().month(), Date::today().year()) {} /*.............................................................*/ Employee::Employee(string n, int hd, int hm, int hy) /* RECEIVES: n - name hd, hm, hy - hire day, month, year */ : _name(n), _wage(0), _id(0), _hiredate(hd, hm, hy) {} /*.............................................................*/ Employee::Employee(string n, int id) /* RECEIVES: n - name id - ID number */ : _name(n), _wage(0), _id(id), _hiredate(Date::today().day(), Date::today().month(), Date::today().year()) {} /*.............................................................*/ void Employee::set_salary(double s) { _wage = s; } /*.............................................................*/ void Employee::raise_salary(double p) /* PURPOSE: raise employee salary RECEIVES: p - the percentage to raise by */ { _wage *= 1 + p; } /*.............................................................*/ double Employee::hourly_wage() const { const int WORKING_HRS_PER_YEAR = 2000; /* this is a commonly used figure -- 50 weeks at 40 hours/week */ return _wage / WORKING_HRS_PER_YEAR; } /*.............................................................*/ double Employee::weekly_pay(double h) const /* PURPOSE: computes pay for a week RECEIVES: h - number of hours worked in that week RETURNS: employee pay REMARKS: overtime is paid at 1.5 * the normal rate */ { if (h > 40) h += 0.5 * (h - 40); return h * hourly_wage (); } /*.............................................................*/ void Employee::set_id(int i) { _id = i; } /*.............................................................*/ int Employee::id() const { return _id; } /*.............................................................*/ void Employee::print() const /* REMARKS: the call print(cout) is a virtual call */ { print(cout); } /*.............................................................*/ void Employee::print(ostream& os) const { os << _name << " " << _id << " " << _wage << " " << _hiredate << endl; } /*-------------------------------------------------------------*/ Manager::Manager(string name) /* RECEIVES: name - manager name */ : Employee(name) {} /*.............................................................*/ void Manager::add(Employee* e) /* PURPOSE: add an employee to be supervised by this manager */ { _superv.push_back(e); } /*.............................................................*/ void Manager::print(ostream& os) const { Employee::print(os); for (int i = 0; i < _superv.size(); i++) { os << " "; _superv[i]->print(os); } } /*.............................................................*/ double Manager::weekly_pay(double h) const /* PURPOSE: computes pay for a week RECEIVES: h - number of hours worked in that week RETURNS: employee pay REMARKS: no overtime for managers */ { if (h <= 40) return hourly_wage() * h; else return 40 * h; }