// Fig. 9.5: hourly.cpp // Member function definitions for class HourlyWorker #include #include #include "Hourly.h" using namespace std; // Constructor for class HourlyWorker: calls base-class constructor HourlyWorker::HourlyWorker(const string &first, const string &last, double initHours, double initWage): Employee( first, last ) { setHours(initHours); setWage(initWage); } void HourlyWorker::setHours(double initHours) { hours = initHours; // should validate} } void HourlyWorker::setWage(double initWage) { wage = initWage; // should validate} } // Get the HourlyWorker's pay double HourlyWorker::getPay() const { return(wage * hours); } // Print the HourlyWorker's name and pay ostream &operator<<(ostream& Dest,const HourlyWorker &HW) { Dest << "HourlyWorker::operator<< is about to be executed\n\n"; // Declare a pointer to the base class type and // set it to point at the derived class' object (perfectly legal) Employee const *ePtr=&HW; // Now invoke << on the object of that pointer, // which is an Employee, not an HourlyWorker Dest << HW.getFirstName() << " "; Dest << ePtr->getLastName(); // and the Employee version of << will be invoked Dest << " is an hourly worker with pay of $" << fixed << showpoint << setprecision( 2 ) << HW.getPay() << endl; return(Dest); }