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

C++.You will assign dynamically allocated derived class objects to base class po

ID: 3807306 • Letter: C

Question

C++.You will assign dynamically allocated derived class objects to base class pointers and use polymorphism to display output based on the type of the object.

1. First, start by implementing the base class Employee.
     One private int attribute empID
   - One public constructor that will take an int as an argument and assign it to empID
   - A public function that will return the empID with the following prototype: int getEmpID() const;
   - A public pure virtual function with the following prototype: virtual void printPay() =0;

2. Next, implement the derived class HourlyEmployee: public Employee.
   This class will have:
- One private double attribute hours
- One private double attribute payRate
- One public constructor that will take in three arguments: The employee id, the number of hours worked, and the pay rate. The constructor will pass the employee id argument to the base class constructor and assign the value of the other two arguments to its attributes.
- A public function that will return the hours with the following prototype: double getHours() const;
- A public function that will return the payRate with the following prototype: double getPayRate() const;
- An overridden public printPay function that will print the hourly empolyee’s id number and the weekly pay (i.e. hours * payRate) with 2 digits after the decimal place. The prototype is : void printPay();
3. Next, implement the derived class SalariedEmployee: public Employee. (10 points) This class will have:
One private double attribute salary
- One public constructor that will take in two arguments: The employee id and the salary. The constructor will pass the employee id argument to the base class constructor and assign the value of the other arguments to its attribute. -
A public function that will return the salary with the following prototype: double getSalary() const;
- An overridden public printPay function that will print the salaried empolyee’s id number and the weekly pay (i.e. salary/52) with 2 digits after the decimal place. The prototype is : void printPay();

4. You will also need to implement two stand-alone functions.
The function : void getInput(vector & Ve); will contain a loop that will give the user an option to enter information about an hourly employee or a salaried employee. Depending on the user’s choice, collect the appropriate input information from the user and use this information to dynamically construct a derived class object. Assign the address of the dynamically created derived class object to an element of the Ve vector. (Note: In this step, you are assigning the addresses of derived class objects to base class pointers).
The other function: void printList(const vector & Ve); will simply loop through the elements of the Ve vector and call the printPay() function of each of the dynamically allocated objects. (Note: In this step, you see the polymorphism in action. That is, depending on the type of the object there will be dynamic binding and the appropriate version of the printPay() function will execute).

All numeric input must be validated.
Below is the code for the main function to be used in your program.
int main() {
vector VEmp;
getInput(VEmp);
printList(VEmp);
system("pause");
return 0; }

Any ideas where to start??

Explanation / Answer

Sample program based on above question:

// Assign dynamically allocated derived class objects to base class pointers
// and use polymorphism to display output based on the type of the object.

#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;

// base class Worker
class Worker
{
private:
   // Worker ID
   int empID;

public:
   // Constructor that will take an int as an argument and
   // assign it to empID
   Worker(int);

   // Destructor
   ~Worker();

   // Return the empID
   int getEmpID() const;

   // pure virtual function
   virtual void printPay() = 0;

};
// Constructor that will take an int as an argument and
// assign it to empID
Worker::Worker(int empID)
{
   this->empID = empID;
}

// Destructor
Worker::~Worker()
{
}

// Return the empID
int Worker::getEmpID() const
{
   return empID;
}

// derived class HourlyWorker
class HourlyWorker : public Worker
{
public:
   // Constructor that will take in three arguments: The Worker
   // id, the number of hours worked, and the pay rate.
   HourlyWorker(int, double, double);

   // Destructor
   ~HourlyWorker();

   // Return the hours
   double getHours() const;

   // Return the payRate
   double getPayRate() const;

   // Print the hourly Worker's id and the weekly pay with 2 digits
   // after the decimal place.
   void printPay();

private:
   // number of hours worked
   double hours;
   // pay rate
   double payRate;
};

// Constructor that will take in three arguments: The Worker
// id, the number of hours worked, and the pay rate.
HourlyWorker::HourlyWorker(int empID, double hours, double payRate)
   : Worker(empID)
{
   this->hours = hours;
   this->payRate = payRate;
}

// Destructor
HourlyWorker::~HourlyWorker()
{
}


// Return the hours
double HourlyWorker::getHours() const
{
   return hours;
}

// Return the payRate
double HourlyWorker::getPayRate() const
{
   return payRate;
}

// Print the hourly Worker's id and the weekly pay with 2 digits
// after the decimal place.
void HourlyWorker::printPay()
{
   // calculate weekly pay
   double weeklyPay = hours * payRate;

   // set floating point output to 2 digits after the decimal place
   cout << fixed << showpoint << setprecision(2);

   cout << "The pay for the hourly Worker with ID number ";
   cout << getEmpID() << " is $" << weeklyPay << endl;
}

// derived class SalariedWorker
class SalariedWorker : public Worker
{
public:
   // Constructor that will take in two arguments: The Worker id and
   // the salary.
   SalariedWorker(int, double);

   // Destructor
   ~SalariedWorker();

   // Return the salary
   double getSalary() const;

   // Print the salaried Worker's id number and the weekly pay with
   // 2 digits after the decimal place.
   void printPay();

private:
   // salary
   double salary;
};

// Constructor that will take in two arguments: The Worker id and
// the salary.
SalariedWorker::SalariedWorker(int empID, double salary) : Worker(empID)
{
   this->salary = salary;
}

// Destructor
SalariedWorker::~SalariedWorker()
{
}

// Return the salary
double SalariedWorker::getSalary() const
{
   return salary;
}

// Print the salaried Worker's id number and the weekly pay with
// 2 digits after the decimal place.
void SalariedWorker::printPay()
{
   // calculate the weekly pay
   double weeklyPay = salary / 52;

   // set floating point output to 2 digits after the decimal place
   cout << fixed << showpoint << setprecision(2);

   cout << "The pay for the salaried Worker with ID number ";
   cout << getEmpID() << " is $" << weeklyPay << endl;
}

// function prototypes
void getInput(vector <Worker *> & Ve);
void printList(const vector <Worker *> & Ve);

int main()
{
  
   vector <Worker *> VEmp;
  
   getInput(VEmp);
  
   printList(VEmp);
  
   system("pause");
   return 0;
}

// Contain a loop that will give the user an option to enter information
// about an hourly Worker or a salaried Worker. Depending on the user's
// choice, collect the appropriate input information from the user and use
// this information to dynamically construct a derived class object. Assign
// the address of the dynamically created derived class object to an element
// of the Ve vector.
void getInput(vector <Worker *> & Ve)
{
   // Worker id
   int empID;
   // number of hours worked
   double hours;
   // pay rate
   double payRate;
   // salary
   double salary;
   // user's choice
   int option;

   // do until option 3 is entered
   do
   {
       // display options
       cout << "Enter 1 for Hourly Worker" << endl;
       cout << "Enter 2 for Salaried Worker" << endl;
       cout << "Enter 3 to stop: ";

       // read option
       cin >> option;
       cout << endl;

       // process option
       switch (option)
       {
       case 1:
           // read Worker ID
           cout << "Enter the ID: ";
           cin >> empID;
           // read number of hours worked
           cout << "Enter the number of hours worked: ";
           cin >> hours;
           // read pay rate
           cout << "Enter the pay rate: ";
           cin >> payRate;
           // dynamically construct a HourlyWorker object and
           // push back into vector
           Ve.push_back(new HourlyWorker(empID, hours, payRate));
           break;
       case 2:
           // read Worker ID
           cout << "Enter the ID: ";
           cin >> empID;
           // read salary
           cout << "Enter the salary: ";
           cin >> salary;
           // dynamically construct a SalariedWorker object and
           // push back into vector
           Ve.push_back(new SalariedWorker(empID, salary));
           break;
       case 3:
           break;
       default:
           cout << "Error: invalid option" << endl;
       }

       cout << endl;
   } while (option != 3);
}

// Loop through the elements of the Ve vector and call the printPay() function
// of each of the dynamically allocated objects.
void printList(const vector <Worker *> & Ve)
{
   for (size_t i = 0; i < Ve.size(); i++)
   {
       Ve[i]->printPay();
   }
}

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