C++ Problem Specification: You are to write a program that will define a structu
ID: 3770507 • Letter: C
Question
C++
Problem Specification:
You are to write a program that will define a structure consisting of the following data members, “fields”:
name
hours worked
hourly rate
Use a structure to implement the following:
You will use a function to read the name, hours and rate, passing them by reference to the main function.
You will use a function to print. This function prints each data member on a separate line followed by the grosspay - a local variable that gets its value by calling another function that will return the product of hours and rate.
Use a class to implement the same problem solution:
You must include a null constructor, and destructor.
You must not use inline function, must declare functions in the class declaration section, and then implement them.
Input to the program consists of:
name
hours worked
hourly rate
Your output should be a report like follows:
Employee Name: Jim Donavan
Hours worked: 40.00
Hourly Rate: $ 9.75
Explanation / Answer
Your question is little confusing. So, I have implemented it in two ways. One using c type structure, and another using class.
First, using structure:
#include <iostream>
#include <string>
using namespace std;
struct Employee {
string name;
int hrsWorked;
float hrlyRate;
};
void readData(string&, int&, float&);
void printData(Employee e);
float calProduct(int, float);
int main() {
Employee emp;
readData(emp.name, emp.hrsWorked, emp.hrlyRate);
printData(emp);
return 0;
}
void readData(string& name, int& workHrs, float& rate) {
cout << "Enter the name of employee: ";
cin >> name;
cout << "Enter the number of hours worked: ";
cin >> workHrs;
cout << "Enter the hourly rate: ";
cin >> rate;
}
void printData(Employee e) {
cout << "Employee Name: " << e.name << endl;
cout << "Hours Worked: " << e.hrsWorked << endl;
cout << "Hourly Rate: $" << e.hrlyRate << endl;
float grosspay = calProduct(e.hrsWorked, e.hrlyRate);
cout << "Grosspay: " << grosspay << endl;
}
float calProduct(int hrs, float rate) {
return hrs * rate;
}
Following is using class:
#include <iostream>
#include <string>
using namespace std;
float calProduct(int hrs, float rate) {
return hrs * rate;
}
class Employee {
string name;
int hrsWorked;
float hrlyRate;
public:
Employee(): hrsWorked(0), hrlyRate(0.0)
{
}
Employee(string name, int hrs, float rate)
{
this->name = name;
hrsWorked = hrs;
hrlyRate = rate;
}
void readData(string name, int hrs, float rate);
void printData();
~Employee()
{
}
};
void Employee :: readData(string name, int hrs, float rate)
{
this->name = name;
hrsWorked = hrs;
hrlyRate = rate;
}
void Employee :: printData()
{
cout << "Employee Name: " << name << endl;
cout << "Hours Worked: " << hrsWorked << endl;
cout << "Hourly Rate: $" << hrlyRate << endl;
float grosspay = calProduct(hrsWorked, hrlyRate);
cout << "Grosspay: " << grosspay << endl;
}
int main()
{
Employee emp;
string name;
int workHrs;
float rate;
cout << "Enter the name of employee: ";
cin >> name;
cout << "Enter the number of hours worked: ";
cin >> workHrs;
cout << "Enter the hourly rate: ";
cin >> rate;
emp.readData(name, workHrs, rate);
emp.printData();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.