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

// This program demonstrates overloaded functions to calculate // the gross week

ID: 3722806 • Letter: #

Question

// This program demonstrates overloaded functions to calculate

// the gross weekly pay of hourly paid, salarized or contracted

// employees.

#include <iostream>

#include <iomanip>

using namespace std;

// Function prototypes

void getChoice(char &);

double calcWeeklyPay(int, double);

double calcWeeklyPay(double);

int main()

{

char selection; // Employee selection

int worked; // Hours worked

double rate; // Hourly pay rate

doyble yearly; // Yearly salary

// Set numeric output formatting.

cout << fixed << showpoint << setprecision(2);

// Display the selection.

cout << "Do you want to calculate the weekly pay of ";

cout << "(H) an hourly paid employee, or ";

cout << "(S) a salarized employee? ";

cout << "(C) an contracted employee? ";

getChoice(selection);

// Process the selection.

switch (selection)

{

// Hourly paid employee

case 'H' :

case 'h' : cout << "How many hours were worked? ";

cin >> worked;

cout << "What is the hourly pay rate? ";

cin >> rate;

cout << "The gross weekly pay is $";

cout << calcWeeklyPay(worked, rate) << endl;

break;

// Salarized employee

case 'S' :

case 's' :

cout << "What is the annual salary? ";

cin >> yearly;

cout << "The gross weekly pay is $";

cout << calcWeeklyPay(yearly) << endl;

break;

}

return 0;

}

//***************************************************************************

//Defination of function getChoice. *

// The arameter letter is a refernce to a char. *

// This function asks the user for an H, S or C and returns *

// the validated input. *

//***************************************************************************

void getChoice(char & letter)

{

// Get the user's selection.

cout << "Enter your choice (H, S or C): ";

cin >> letter;

// Validate the selection.

while (letter != 'H' && letter != 'h' &&

letter != 'S' && letter != 's' &&

letter != 'C' && letter != 'c')

{

cout << "Please enter H, S or C: ";

cin >> letter;

}

}

//***************************************************************************

//Defination of function getChoice. * *

// This function calculates the gross weekly pay of *

//an hourly paid employee.The parameter hours holds the *

// number of hours worked. The parameter payRate holds the *

// hourly pay rate. The function returns the weekly salary. * *

//***************************************************************************

double calcWeeklyPay(int hours,double payRate)

{

return hours * payRate;

}

//***************************************************************************

//Defination of function getChoice. * *

// This function calculates the gross weekly pay of *

//an hourly paid employee.The parameter hours holds the *

// number of hours worked. The parameter payRate holds the *

// hourly pay rate. The function returns the weekly salary. * *

//***************************************************************************

double calcWeeklyPay(double annSalary)

{

return annSalary / 52;

}

Explanation / Answer

/*

Since your program is correct logically, It has one compilation error that I fixed and the last function's difiniton is not explained correctly. So I modified it and Please find the working code below.

Let's understand Method Overloading.

Method Overloading: Method overloading is a technique in which we use the functioin with same name but different signatue.

You can overload a method by using following ways.

1.Changing number of parameter => Function name is same but number of parameter are different

2.Changing data types of parameters=> Function name is same and number of parameters are same but data type of parameters may differ.

example:In your given program you used function calcWeeklyPay() two time with same name. But in both the time the number of parameters are differing

let's see

1. double calcWeeklyPay(double annSalary)

2. double calcWeeklyPay(int hours,double payRate)

In above both the functions the function name is same but first function has only one parameter of double type and second funvction has two paremeters one int and another is double. This is called method overloading.

Important: you can not do method overloading by just changing return type.

like:

1. dobule sum(int x,int y)

2. int sum(int x,int y)

This is error in C++ method overloading.

Since I don't have difinition for contracted based employee so I can't add that method but I hope that you understood the method overloading ,now you can add one more definition of method calcWeeklyPay() for contracted employee.

Do below two thing.

Add one more case like.

case 'C' :

case 'c' :

cout << "What is your hourly rate? ";

cin >> hour;

//Like that you can add other parameters

cout << "The gross weekly pay is $";

//You can pass all that parameters to the below function.

cout << calcWeeklyPay() << endl;

break;

Define you function : like

calcWeeklyPay(paramter1,parameter 2,paremeter3)

{

//do calculatioin and return ;

}

*/

//*************Please find working code below***************************************

//Please create a .cpp file and copy answer from below line to the end of answer in that file.

// This program demonstrates overloaded functions to calculate

// the gross weekly pay of hourly paid, salarised or contracted

// employees.

#include <iostream>

#include <iomanip>

using namespace std;

// Function prototypes

void getChoice(char &);

double calcWeeklyPay(int, double);

double calcWeeklyPay(double);

int main()

{

char selection; // Employee selection

int worked; // Hours worked

double rate; // Hourly pay rate

double yearly; // Yearly salary

// Set numeric output formatting.

cout << fixed << showpoint << setprecision(2);

// Display the selection.

cout << "Do you want to calculate the weekly pay of ";

cout << "(H) an hourly paid employee, or ";

cout << "(S) a salarized employee? ";

cout << "(C) an contracted employee? ";

getChoice(selection);

// Process the selection.

switch (selection)

{

// If user selected hourly worked employee the asking user to enter hours worked and pay rate per hour

case 'H' :

case 'h' : cout << "How many hours were worked? ";

cin >> worked;

cout << "What is the hourly pay rate? ";

cin >> rate;

cout << "The gross weekly pay is $";

//Calling function calcWeeklyPay() to calculate weekly pay , displaying total pay for week
cout << calcWeeklyPay(worked, rate) << endl;

break;

// Salarized employee
//If selection is Salaried person then asking for annual salary and calling function to calculate weekly average.

case 'S' :

case 's' :

cout << "What is the annual salary? ";

cin >> yearly;

cout << "The gross weekly pay is $";

//Calling the function and displaying return value from function.

cout << calcWeeklyPay(yearly) << endl;

break;

}

return 0;

}

//***************************************************************************

//Definition of function getChoice. *

// The parameter letter is a reference to a char. *

// This function asks the user for an H, S or C and returns *

// the validated input. *

//***************************************************************************

void getChoice(char & letter)

{

// Get the user's selection.

cout << "Enter your choice (H, S or C): ";

cin >> letter;

// Validate the selection. , If entered character is not H, S or C then asking user to enter choice again

while (letter != 'H' && letter != 'h' &&

letter != 'S' && letter != 's' &&

letter != 'C' && letter != 'c')

{

cout << "Please enter H, S or C: ";

cin >> letter;

}

}

//***************************************************************************


// This function calculates the gross weekly pay of *

//an hourly paid employee.The parameter hours holds the *

// number of hours worked. The parameter payRate holds the *

// hourly pay rate. The function returns the weekly salary. * *

//***************************************************************************

double calcWeeklyPay(int hours,double payRate)

{

return hours * payRate;

}

//***************************************************************************


// This function calculates the gross weekly pay of *

//for salaried employee.

//Parameter annSalary holds annual salary

//Function returns the average weekly salary of salaried employee. * *

//***************************************************************************

double calcWeeklyPay(double annSalary)

{

return annSalary / 52;

}