// union example 2
#include <iostream>
#include <iomanip>
using namespace std;
struct HourlyPay
{
unsigned short hoursWorked;
float hourlyWage;
};
union Pay
{
HourlyPay hourlyPay;
float annualSalary;
};
enum EmployeeType {Hourly, Salary};
struct Employee
{
string name;
EmployeeType type;
Pay pay;
};
float weeklyPay(const Employee&);
int main()
{
Employee John;
John.name = "John";
John.type = Hourly;
John.pay.hourlyPay.hoursWorked = 30;
John.pay.hourlyPay.hourlyWage = 15.75;
Employee Mary;
Mary.name = "Mary";
Mary.type = Salary;
Mary.pay.annualSalary = 75000;
cout << setprecision(2) << fixed;
cout << '$' << weeklyPay(John) << endl;
cout << '$' << weeklyPay(Mary) << endl;
}
float weeklyPay(const Employee& employee)
{
if (employee.type == Hourly)
return employee.pay.hourlyPay.hoursWorked * employee.pay.hourlyPay.hourlyWage;
else return employee.pay.annualSalary / 52;
}
****** OUTPUT ******
$472.50
$1442.31