Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Polymorphism I\'m having trouble geting everything to work properly, the cod

ID: 3694771 • Letter: C

Question

C++ Polymorphism

I'm having trouble geting everything to work properly, the code I have so far is posted below the instructions.

The program must calculate payroll using polymorphism!

Instructions:

In this project, you’ll create a more general Employee base class that factors outthe attributes and
behaviors in class CommissionEmployee that are common to all Employees. The common attributes and
behaviors for all Employees are firstName, middleName, lastName, birthDate, socialSecurityNumber,
getFirstName, getMiddleName, getLastName, getBirthday, getSocialSecurityNumber, earnings, and a
portion of function print. The new base class Employee should contain these instance variables and
functions, and a constructor. (Feel free to use class Date of Fig. 9.17 – Fig. 9.18, or any other Date class, to
represent an employee’s birthday).
Next, rewrite class CommissionEmployee as a derived class of Employee. ClassCommissionEmployee should
contain only the instance variables and functions that are not declared in base class Employee. It should
contain a concrete earnings method. Class CommissionEmployee’s constructor should invoke class
Employee’s constructor, and CommissionEmployee’s print function should invokeEmployee’s print function.
Similarly, modify class BasePlusCommissionEmployee such that it extends classCommissionEmployee.
Other types of Employees are SalariedEmployees who get paid a fixed weekly salary; HourlyEmployees who
get paid an hourly wage with time-and-a-half (1.5 times the hourly wage) for hours over 40 hours; and
PieceWorker that represents an employee whose pay is based on the number of pieces of merchandise
produced;
Create class HourlyEmployee such that it extends class Employee and it has instance variable hours (a
double) that represents the hours worked, instance variable wage (a double) that represents the wages
per hour, a constructor that takes as arguments a first name, a middle name, a last name, a birth date, a
social security number, an hourly wage and the number of hours worked, set andget functions for
manipulating the hours and wage, an earnings method to calculate anHourlyEmployee’s earnings based
on the hours worked, and a Print function that displays the HourlyEmployee’s information. Function
setWage should ensure that wage is not below $15.000 an hour, and setHoursshould ensure that the
value of hours is between 0 and 60.
Create Class PieceWorker such that it extends class Employee, and it should contain private instance variables
wage (to store the employee’s wage per piece) and pieces (to store the number of pieces produced). Provide a
concrete implementation of method earnings in class PieceWorker that calculates the employee’s earnings by
multiplying the number of pieces produced by the wage per piece.
All set functions must accurately validate all your input.
Create a single test/driver file that instantiates an object of each type of employee, and interactively prompts for
each employee’s data at runtime. All the data fields must be validated. Bad data must be repeatedly rejected until
Page 2 of 3
good data is entered. Create an array of Employee variables to store references to objects of each concrete class
in the new Employee hierarchy.
The application should process one object of each of the classes derived fromEmployee class. In a loop, calculate
the payroll for each Employee (polymorphically). For each Employee, display itsString representation and
earnings.
If the object currently being processed is a PieceWorker, the application should double the wage per piece. The
application should output the gross pay amount for each employee. Also, add a $200.00 bonus to the person’s
payroll amount if the current month is the one in which the Employee’s birthday occurs.

Header.h:

#define EMPLOYEE_H
#define SALARIED_H
#define BASEPLUS_H

#include <string>

class Employee
{
public:
   Employee(const std::string &, const std::string &,
       const std::string &);
   virtual ~Employee() { }
   void setFirstName(const std::string &);
   std::string getFirstName() const;

   void setLastName(const std::string &);
   std::string getLastName() const;

   void setSocialSecurityNumber(const std::string &);
   std::string getSocialSecurityNumber() const;

   virtual double earnings() const = 0;
   virtual void print() const;
private:
   std::string firstName;
   std::string lastName;
   std::string socialSecurityNumber;
};

class CommissionEmployee : public Employee
{
public:
   CommissionEmployee(const std::string &, const std::string &,
       const std::string &, double = 0.0, double = 0.0);
   virtual ~CommissionEmployee() { }
   void setCommissionRate(double);
   double getCommissionRate() const;

   void setGrossSales(double);
   double getGrossSales() const;

   virtual double earnings() const override;
   virtual void print() const override;
private:
   double grossSales;
   double commissionRate;
};

class SalariedEmployee : public Employee

{
public:
   SalariedEmployee(const std::string &, const std::string &,
       const std::string &, double = 0.0);
   virtual ~SalariedEmployee() { }

   void setWeeklySalary(double);
   double getWeeklySalary() const;

                                  
   virtual double earnings() const override;
   virtual void print() const override;
private:
   double weeklySalary;
};

class BasePlusCommissionEmployee : public CommissionEmployee
{
public:
   BasePlusCommissionEmployee(const std::string &, const std::string &,
       const std::string &, double = 0.0, double = 0.0, double = 0.0);
   virtual ~BasePlusCommissionEmployee() { }

   void setBaseSalary(double);
   double getBaseSalary() const;

                              
   virtual double earnings() const override;
   virtual void print() const override;
private:
   double baseSalary;
};

class PieceWorker : public PieceWorker
{
public:
   PieceWorker(const std::string &, const std::string &,
       const std::string &, double = 0.0);
   virtual ~PieceWorker() { }

   void setPiecesMade(double);
   double getPiecesMade() const;


   virtual double earnings() const override;
   virtual void print() const override;
private:
   double weeklySalary;
};

Project11.cpp

#include <iostream>
#include <string>
#include <stdexcept>
#include "Header.h"
using namespace std;


Employee::Employee(const string &first, const string &last,
   const string &ssn)
   : firstName(first), lastName(last), socialSecurityNumber(ssn)
{
  
}

  
void Employee::setFirstName(const string &first)
{
   firstName = first;
}

  
string Employee::getFirstName() const
{
   return firstName;
}
void Employee::setLastName(const string &last)
{
   lastName = last;
}
string Employee::getLastName() const
{
   return lastName;
}
void Employee::setSocialSecurityNumber(const string &ssn)
{
   socialSecurityNumber = ssn;
}
string Employee::getSocialSecurityNumber() const
{
   return socialSecurityNumber;
}
void Employee::print() const
{
   cout << getFirstName() << ' ' << getLastName()
       << " social security number: " << getSocialSecurityNumber();
}

SalariedEmployee::SalariedEmployee(const string &first,
   const string &last, const string &ssn, double salary)
   : Employee(first, last, ssn)
{
   setWeeklySalary(salary);
}
void SalariedEmployee::setWeeklySalary(double salary)
{
   if (salary >= 0.0)
       weeklySalary = salary;
   else
       throw invalid_argument("Weekly salary must be >= 0.0");
}
double SalariedEmployee::getWeeklySalary() const
{
   return weeklySalary;
}
double SalariedEmployee::earnings() const
{
   return getWeeklySalary();
}
void SalariedEmployee::print() const
{
   cout << "salaried employee: ";
   Employee::print();
   cout << " weekly salary: " << getWeeklySalary();
}


CommissionEmployee::CommissionEmployee(const string &first,
   const string &last, const string &ssn, double sales, double rate)
   : Employee(first, last, ssn)
{
   setGrossSales(sales);
   setCommissionRate(rate);
}
void CommissionEmployee::setGrossSales(double sales)
{
   if (sales >= 0.0)
       grossSales = sales;
   else
       throw invalid_argument("Gross sales must be >= 0.0");
}
double CommissionEmployee::getGrossSales() const
{
   return grossSales;
}
void CommissionEmployee::setCommissionRate(double rate)
{
   if (rate > 0.0 && rate < 1.0)
       commissionRate = rate;
   else
       throw invalid_argument("Commission rate must be > 0.0 and < 1.0");
}
double CommissionEmployee::getCommissionRate() const
{
   return commissionRate;
}

double CommissionEmployee::earnings() const
{
   return getCommissionRate() * getGrossSales();
}
void CommissionEmployee::print() const
{
   cout << "commission employee: ";
   Employee::print();
   cout << " gross sales: " << getGrossSales()
       << "; commission rate: " << getCommissionRate();
}


BasePlusCommissionEmployee::BasePlusCommissionEmployee(
   const string &first, const string &last, const string &ssn,
   double sales, double rate, double salary)
   : CommissionEmployee(first, last, ssn, sales, rate)
{
   setBaseSalary(salary);
}


void BasePlusCommissionEmployee::setBaseSalary(double salary)
{
   if (salary >= 0.0)
       baseSalary = salary;
   else
       throw invalid_argument("Salary must be >= 0.0");
}

  
double BasePlusCommissionEmployee::getBaseSalary() const
{
   return baseSalary;
}

  
double BasePlusCommissionEmployee::earnings() const
{
   return getBaseSalary() + CommissionEmployee::earnings();
}
void BasePlusCommissionEmployee::print() const
{
   cout << "base-salaried ";
   CommissionEmployee::print();
   cout << "; base salary: " << getBaseSalary();
}

Explanation / Answer

This below code is for Header file which include the basic of Employee and inheritance concept and polymorphism. Named Employee.h header file

#define EMPLOYEE_H
#define SALARIED_H
#define BASEPLUS_H

#include <string>

class Employee
{
public:
   Employee(const std::string &, const std::string &,
       const std::string &);
       void setFirstName(const std::string &);
   std::string getFirstName() const;

   void setLastName(const std::string &);
   std::string getLastName() const;

   void setSocialSecurityNumber(const std::string &);
   std::string getSocialSecurityNumber() const;

   virtual double earnings() const = 0;
   virtual void print() const;

~Employee() { }

private:
   std::string firstName;
   std::string lastName;
   std::string socialSecurityNumber;
};

class CommissionEmployee : public Employee
{
public:
   CommissionEmployee(const std::string &, const std::string &,
       const std::string &, double = 0.0, double = 0.0);
   virtual ~CommissionEmployee() { }
       double getCommissionRate() const;

   void setGrossSales(double);
   double getGrossSales() const;

   virtual double earnings() const override;
   virtual void print() const override;

void setCommissionRate(double);

private:
   double grossSales;
   double commissionRate;
};

class SalariedEmployee : public Employee

{
public:
   SalariedEmployee(const std::string &, const std::string &,
       const std::string &, double = 0.0);
   virtual ~SalariedEmployee() { }

   void setWeeklySalary(double);
   double getWeeklySalary() const;

                                  
   virtual double earnings() const override;
   virtual void print() const override;
private:
   double weeklySalary;
};

class BasePlusCommissionEmployee : public CommissionEmployee
{
public:
   BasePlusCommissionEmployee(const std::string &, const std::string &,
       const std::string &, double = 0.0, double = 0.0, double = 0.0);
   virtual ~BasePlusCommissionEmployee() { }

   void setBaseSalary(double);
   double getBaseSalary() const;

                              
   virtual double earnings() const override;
   virtual void print() const override;
private:
   double baseSalary;
};

class PieceWorker : public PieceWorker
{
public:
   PieceWorker(const std::string &, const std::string &,
       const std::string &, double = 0.0);
   virtual ~PieceWorker() { }

   void setPiecesMade(double);
   double getPiecesMade() const;


   virtual double earnings() const override;
   virtual void print() const override;
private:
   double weeklySalary;
};

#include <iostream>
#include <string>
#include <stdexcept>
#include "Header.h"
using namespace std;


Employee::Employee(const string &first, const string &last,
   const string &ssn)
   : firstName(first), lastName(last), socialSecurityNumber(ssn)
{
  
}

  
void Employee::setFirstName(const string &first)
{
   firstName = first;
}

  
string Employee::getFirstName() const
{
   return firstName;
}
void Employee::setLastName(const string &last)
{
   lastName = last;
}
string Employee::getLastName() const
{
   return lastName;
}
void Employee::setSocialSecurityNumber(const string &ssn)
{
   socialSecurityNumber = ssn;
}
string Employee::getSocialSecurityNumber() const
{
   return socialSecurityNumber;
}
void Employee::print() const
{
   cout << getFirstName() << ' ' << getLastName()
       << " social security number: " << getSocialSecurityNumber();
}

SalariedEmployee::SalariedEmployee(const string &first,
   const string &last, const string &ssn, double salary)
   : Employee(first, last, ssn)
{
   setWeeklySalary(salary);
}
void SalariedEmployee::setWeeklySalary(double salary)
{
   if (salary >= 0.0)
       weeklySalary = salary;
   else
       throw invalid_argument("Weekly salary must be >= 0.0");
}
double SalariedEmployee::getWeeklySalary() const
{
   return weeklySalary;
}
double SalariedEmployee::earnings() const
{
   return getWeeklySalary();
}
void SalariedEmployee::print() const
{
   cout << "salaried employee: ";
   Employee::print();
   cout << " weekly salary: " << getWeeklySalary();
}


CommissionEmployee::CommissionEmployee(const string &first,
   const string &last, const string &ssn, double sales, double rate)
   : Employee(first, last, ssn)
{
   setGrossSales(sales);
   setCommissionRate(rate);
}
void CommissionEmployee::setGrossSales(double sales)
{
   if (sales >= 0.0)
       grossSales = sales;
   else
       throw invalid_argument("Gross sales must be >= 0.0");
}
double CommissionEmployee::getGrossSales() const
{
   return grossSales;
}
void CommissionEmployee::setCommissionRate(double rate)
{
   if (rate > 0.0 && rate < 1.0)
       commissionRate = rate;
   else
       throw invalid_argument("Commission rate must be > 0.0 and < 1.0");
}
double CommissionEmployee::getCommissionRate() const
{
   return commissionRate;
}

double CommissionEmployee::earnings() const
{
   return getCommissionRate() * getGrossSales();
}
void CommissionEmployee::print() const
{
   cout << "commission employee: ";
   Employee::print();
   cout << " gross sales: " << getGrossSales()
       << "; commission rate: " << getCommissionRate();
}


BasePlusCommissionEmployee::BasePlusCommissionEmployee(
   const string &first, const string &last, const string &ssn,
   double sales, double rate, double salary)
   : CommissionEmployee(first, last, ssn, sales, rate)
{
   setBaseSalary(salary);
}


void BasePlusCommissionEmployee::setBaseSalary(double salary)
{
   if (salary >= 0.0)
       baseSalary = salary;
   else
       throw invalid_argument("Salary must be >= 0.0");
}

  
double BasePlusCommissionEmployee::getBaseSalary() const
{
   return baseSalary;
}

  
double BasePlusCommissionEmployee::earnings() const
{
   return getBaseSalary() + CommissionEmployee::earnings();
}
void BasePlusCommissionEmployee::print() const
{
   cout << "base-salaried ";
   CommissionEmployee::print();
   cout << "; base salary: " << getBaseSalary();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote