//******************************************************************** // // hourly.cpp // CSCI 241 Inheritance and Polymorphism Example // //******************************************************************** #include #include #include "hourly.h" using std::cout; using std::endl; using std::string; /** * Constructor for hourly employee. * * @param first_name The employee's first name. * @param last_name The employee's last name. * @param ssn The employee's Social Security Number. * @param wage The employee's hourly wage. * @param hours The employee's number of hours worked. * * @note The first name, last name, and ssn are passed to the base * class constructor. ********************************************************************/ hourly::hourly(const string& first_name, const string& last_name, const string& ssn, double wage, double hours) : employee(first_name, last_name, ssn) { set_wage(wage); set_hours(hours); } /** * Accessor function for hourly wage data member. * * @return The employee's hourly wage. ********************************************************************/ double hourly::get_wage() const { return wage; } /** * Mutator function for hourly wage data member. * * @param wage The new hourly wage. * * @note If an attempt is made to set the wage to a negative value, * the wage will instead be set to 0. ********************************************************************/ void hourly::set_wage(double wage) { this->wage = (wage < 0.0) ? 0.0 : wage; } /** * Accessor function for hours worked data member. * * @return The employee's hours worked. ********************************************************************/ double hourly::get_hours() const { return hours; } /** * Mutator function for hours worked data member. * * @param wage The new hours worked. * * @note If an attempt is made to set the hours worked to an * impossible value, the hours worked will instead be set to 0. ********************************************************************/ void hourly::set_hours(double hours) { this->hours = (hours >= 0.0 && hours <= 168.0) ? hours : 0.0; } /** * Computes the employee's earnings for the week. * * @return The computed earnings. * * @note Hours worked over 40 are paid at time and a half. ********************************************************************/ double hourly::earnings() const { if (hours <= 40) // No overtime. return wage * hours; else // Overtime is paid at time and a half. return 40 * wage + (hours - 40) * wage * 1.5; } /** * Prints information for hourly employee. ********************************************************************/ void hourly::print() const { cout << "Hourly employee:\n"; // Call employee version of print() to print name and ssn. employee::print(); // Print remaining information. cout << "Hours worked: " << hours << endl; cout << "Hourly wage: $" << wage << endl; }