Help please! This is for C++; Here is the Payroll System Code: // PayrollSystem
ID: 655634 • Letter: H
Question
Help please! This is for C++;
Here is the Payroll System Code:
// PayrollSystem
// Processing Employee derived-class objects individually
// and polymorphically using dynamic binding.
#include <iostream>
#include <iomanip>
#include <vector>
#include "Employee.h"
#include "SalariedEmployee.h"
#include "CommissionEmployee.h"
#include "BasePlusCommissionEmployee.h"
using namespace std;
void virtualViaPointer( const Employee * const ); // prototype
void virtualViaReference( const Employee & ); // prototype
int main()
{
// set floating-point output formatting
cout << fixed << setprecision( 2 );
// create derived-class objects
SalariedEmployee salariedEmployee(
"John", "Smith", "111-11-1111", 800 );
CommissionEmployee commissionEmployee(
"Sue", "Jones", "333-33-3333", 10000, .06 );
BasePlusCommissionEmployee basePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
cout << "Employees processed individually using static binding: ";
// output each Employee?s information and earnings using static binding
salariedEmployee.print();
cout << " earned $" << salariedEmployee.earnings() << " ";
commissionEmployee.print();
cout << " earned $" << commissionEmployee.earnings() << " ";
basePlusCommissionEmployee.print();
cout << " earned $" << basePlusCommissionEmployee.earnings()
<< " ";
// create vector of three base-class pointers
vector < Employee * > employees( 3 );
// initialize vector with Employees
employees[ 0 ] = &salariedEmployee;
employees[ 1 ] = &commissionEmployee;
employees[ 2 ] = &basePlusCommissionEmployee;
cout << "Employees processed polymorphically via dynamic binding: ";
// call virtualViaPointer to print each Employee's information
// and earnings using dynamic binding
cout << "Virtual function calls made off base-class pointers: ";
for ( const Employee *employeePtr : employees )
virtualViaPointer( employeePtr );
// call virtualViaReference to print each Employee's information
// and earnings using dynamic binding
cout << "Virtual function calls made off base-class references: ";
for ( const Employee *employeePtr : employees )
virtualViaReference( *employeePtr ); // note dereferencing
} // end main
// call Employee virtual functions print and earnings off a
// base-class pointer using dynamic binding
void virtualViaPointer( const Employee * const baseClassPtr )
{
baseClassPtr->print();
cout << " earned $" << baseClassPtr->earnings() << " ";
} // end function virtualViaPointer
// call Employee virtual functions print and earnings off a
// base-class reference using dynamic binding
void virtualViaReference( const Employee &baseClassRef )
{
baseClassRef.print();
cout << " earned $" << baseClassRef.earnings() << " ";
} // end function virtualViaReference
Explanation / Answer
// PayrollSystem
// Processing Employee derived-class objects individually
// and polymorphically using dynamic binding.
#include <iostream.h>
#include <iomanip.h>
#include <vector.h>
#include "Employee.h"
#include "SalariedEmployee.h"
#include "CommissionEmployee.h"
#include "BasePlusCommissionEmployee.h"
using namespace std;
void virtualViaPointer( const Employee * const ); // prototype
void virtualViaReference( const Employee & ); // prototype
int main()
{
// set floating-point output formatting
cout << fixed << setprecision( 2 );
// create derived-class objects
SalariedEmployee salariedEmployee(
"John", "Smith", "111-11-1111", 800 );
CommissionEmployee commissionEmployee(
"Sue", "Jones", "333-33-3333", 10000, .06 );
BasePlusCommissionEmployee basePlusCommissionEmployee(
"Bob", "Lewis", "444-44-4444", 5000, .04, 300 );
cout << "Employees processed individually using static binding: ";
// output each Employee
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.