fork download
  1. //********************************************************
  2. //
  3. // Assignment 11 - Object Oriented Design
  4. //
  5. // Name: Po Yuen
  6. //
  7. // Class: C Programming, Spring 2026
  8. //
  9. // Date: April 24, 2026
  10. //
  11. // Description: An object oriented program design using
  12. // C++ that will process our existing set of employees.
  13. // It utilizes a class called Employee and generates an
  14. // array of objects that are used to store, calculate,
  15. // and print out a simple report of inputted and calculated
  16. // values.
  17. //
  18. //
  19. // Object Oriented Design (using C++)
  20. //
  21. //********************************************************
  22.  
  23. #include <iomanip> // std::setprecision, std::setw
  24. #include <iostream> // std::cout, std::fixed
  25. #include <string> // string functions
  26.  
  27. using namespace std;
  28.  
  29. // define constants
  30. #define STD_HOURS 40.0
  31. #define OT_RATE 1.5
  32. #define MA_TAX_RATE 0.05
  33. #define NH_TAX_RATE 0.0
  34. #define VT_TAX_RATE 0.06
  35. #define CA_TAX_RATE 0.07
  36. #define DEFAULT_TAX_RATE 0.08
  37. #define NAME_SIZE 20
  38. #define TAX_STATE_SIZE 3
  39. #define FED_TAX_RATE 0.25
  40. #define FIRST_NAME_SIZE 10
  41. #define LAST_NAME_SIZE 10
  42.  
  43. #define EMP_SIZE 5
  44.  
  45. using std::string;
  46.  
  47. // class Employee
  48. class Employee
  49. {
  50. private:
  51.  
  52. // private data available only to member functions
  53. // all data is initialized to support creating instances that
  54. // are not done via a full constructor with data to be used
  55.  
  56. string firstName = ""; // Employee First Name
  57. string lastName = ""; // Employee Last Name
  58. string taxState = ""; // Employee Tax State
  59. int clockNumber = 0; // Employee Clock Number
  60. float wageRate = 0.0; // Hourly Wage Rate
  61. float hours = 0.0; // Hours worked in a week
  62. float overTimeHrs = 0.0; // Overtime Hours worked
  63. float grossPay = 0.0; // Weekly Gross Pay
  64. float stateTax = 0.0; // State Tax
  65. float fedTax = 0.0; // Fed Tax
  66. float netPay = 0.0; // Net Pay
  67.  
  68. // private functions for call only by an Employee object
  69. // ... these are gernally more complex computations
  70. float calcOverTimeHrs();
  71. float calcGrossPay();
  72. float calcStateTax();
  73. float calcFedTax();
  74. float calcNetPay();
  75.  
  76. // Delcare "getter" function prototypes to retrieve private data
  77. // Note: The inline keyword is not required for the prototypes
  78. string getFirstName();
  79. string getLastName();
  80. string getTaxState();
  81. int getClockNumber();
  82. float getWageRate();
  83. float getHours();
  84. float getOverTimeHrs();
  85. float getGrossPay();
  86.  
  87. // TODO - Declare a "getter" function declaration that will retrieve the employee stateTax
  88. float getStateTax(); // returns the calculated state tax amount
  89.  
  90. // TODO - Declare a "getter" function declaration that will retrieve the employee fedTax
  91. float getFedTax(); // returns the calculated federal tax amount
  92.  
  93. // TODO - Declare a "getter" function declaration that will retrieve the employee netPay
  94. float getNetPay(); // returns the calculated net pay amount
  95.  
  96. public:
  97.  
  98. // public member functions that can be called
  99. // to access private data member items
  100.  
  101. // public no argument constructor with defaults
  102. // All Employee class data will be initialized to defaults
  103. Employee() {}
  104.  
  105. // public constructor with arguments passed to it
  106. Employee (string myFirstName, string myLastName, string myTaxState,
  107. int myClockNumber, float myWageRate, float myHours);
  108.  
  109. ~Employee(); // destructor
  110.  
  111. // print out Employee data to the console
  112. void printEmployee();
  113.  
  114.  
  115. }; // End class declarations.
  116.  
  117. // The inlined Employee members follow...
  118.  
  119. // A "getter" function that will retrieve the First Name
  120. inline string Employee::getFirstName() {
  121. return firstName;
  122. }
  123.  
  124. // A "getter" function that will retrieve the employee Last Name
  125. inline string Employee::getLastName() {
  126. return lastName;
  127. }
  128.  
  129. // A "getter" function that will retrieve the employee Tax State
  130. inline string Employee::getTaxState() {
  131. return taxState;
  132. }
  133.  
  134. // A "getter" function that will retrieve the employee Clock Number
  135. inline int Employee::getClockNumber() {
  136. return clockNumber;
  137. }
  138.  
  139. // A "getter" function that will retrieve the employee Wage Rate
  140. inline float Employee::getWageRate() {
  141. return wageRate;
  142. }
  143.  
  144. // A "getter" function that will retrieve the employee Hours Worked
  145. inline float Employee::getHours() {
  146. return hours;
  147. }
  148.  
  149. // A "getter" function that will retrieve the employee Overtime Hours
  150. inline float Employee::getOverTimeHrs() {
  151. return overTimeHrs;
  152. }
  153.  
  154. // A "getter" function that will retrieve the employee Gross Pay
  155. inline float Employee::getGrossPay() {
  156. return grossPay;
  157. }
  158.  
  159. // TODO - Add an inline "getter" function that will retrieve the employee stateTax
  160. inline float Employee::getStateTax() {
  161. return stateTax; // return the stored state tax amount to the caller
  162. }
  163.  
  164. // TODO - Add an inline "getter" function that will retrieve the employee fedTax
  165. inline float Employee::getFedTax() {
  166. return fedTax; // return the stored federal tax amount to the caller
  167. }
  168.  
  169. // TODO - Add an inline "getter" function that will retrieve the employee netPay
  170. inline float Employee::getNetPay() {
  171. return netPay; // return the stored net pay amount to the caller
  172. }
  173.  
  174. // private member function to calculate Overtime Hours
  175. float Employee::calcOverTimeHrs ( )
  176. {
  177. // Calculate the overtime hours for the week
  178. if (hours > STD_HOURS)
  179. overTimeHrs = hours - STD_HOURS; // ot hours
  180. else
  181. overTimeHrs = 0; // no ot hours
  182.  
  183. // the calculated overtime hours
  184. return (overTimeHrs);
  185.  
  186. } // calcOverTimeHrs
  187.  
  188. // private member function to calculate Gross Pay
  189. float Employee::calcGrossPay ( )
  190. {
  191. // Process gross pay based on if there is overtime
  192. if (overTimeHrs > 0)
  193. {
  194. // overtime
  195. grossPay = (STD_HOURS * wageRate) // normal pay
  196. + (overTimeHrs * (wageRate * OT_RATE)); // ot pay
  197. }
  198. else // no overtime pay
  199. {
  200. grossPay = wageRate * hours; // normal pay
  201. }
  202.  
  203. // the calculated gross pay
  204. return (grossPay);
  205.  
  206. } // calcGrossPay
  207.  
  208. // private member function to calculate State Tax
  209. float Employee::calcStateTax ()
  210. {
  211.  
  212. float theStateTax; // calculated state tax
  213.  
  214. theStateTax = grossPay; // initialize to gross pay
  215.  
  216. // calculate state tax based on where employee resides
  217.  
  218. if (taxState.compare("MA") == 0)
  219. theStateTax *= MA_TAX_RATE;
  220. else if (taxState.compare("VT") == 0)
  221. theStateTax *= VT_TAX_RATE;
  222. else if (taxState.compare("NH") == 0)
  223. theStateTax *= NH_TAX_RATE;
  224. else if (taxState.compare("CA") == 0)
  225. theStateTax *= CA_TAX_RATE;
  226. else
  227. theStateTax *= DEFAULT_TAX_RATE; // any other state
  228.  
  229. // return the calculated state tax
  230. return (theStateTax);
  231.  
  232. } // calcStateTax
  233.  
  234. // private member function to calculate Federal Tax
  235. float Employee::calcFedTax ()
  236. {
  237.  
  238. float theFedTax; // The calculated Federal Tax
  239.  
  240. // Federal Tax is the same for all regardless of state
  241. theFedTax = grossPay * FED_TAX_RATE;
  242.  
  243. // return the calculated federal tax
  244. return (theFedTax);
  245.  
  246. } // calcFedTax
  247.  
  248. // private member function to calculate Net Pay
  249. float Employee::calcNetPay ()
  250. {
  251.  
  252. float theNetPay; // total take home pay (minus taxes)
  253. float theTotalTaxes; // total taxes owed
  254.  
  255. // calculate the total taxes owed
  256. theTotalTaxes = stateTax + fedTax;
  257.  
  258. // calculate the net pay
  259. theNetPay = grossPay - theTotalTaxes;
  260.  
  261. // return the calculated net pay
  262. return (theNetPay);
  263.  
  264. } // calcNetPay
  265.  
  266.  
  267. // constructor with arguments
  268. Employee::Employee (string myFirstName, string myLastName, string myTaxState,
  269. int myClockNumber, float myWageRate, float myHours)
  270. {
  271. // initialize all member data items
  272. firstName = myFirstName; // input
  273. lastName = myLastName; // input
  274.  
  275. // Convert myTaxState to uppercase if entered in lowercase
  276. if (std::islower(myTaxState[0]))
  277. myTaxState[0] = std::toupper(myTaxState[0]);
  278. if (std::islower(myTaxState[1]))
  279. myTaxState[1] = std::toupper(myTaxState[1]);
  280.  
  281. taxState = myTaxState; // input
  282. clockNumber = myClockNumber; // input
  283. wageRate = myWageRate; // input
  284. hours = myHours; // input
  285. overTimeHrs = calcOverTimeHrs(); // calculated overtime hours
  286. grossPay = calcGrossPay(); // calculated gross pay
  287.  
  288. // TODO - set stateTax as the return from calcStateTax member function
  289. stateTax = calcStateTax(); // calculate and store state tax
  290.  
  291. // TODO - set fedTax as the return from calcFedTax member function
  292. fedTax = calcFedTax(); // calculate and store federal tax
  293.  
  294. // TODO - set netPay as the return from calcNetPay member function
  295. netPay = calcNetPay(); // calculate and store net pay
  296.  
  297. } // Employee constructor
  298.  
  299. // a member function to print out the info in a given Employee object
  300. void Employee::printEmployee() {
  301.  
  302. // Display the entered input on the Employee
  303. cout << endl << endl <<"*** Entered Details are *** " << endl;
  304. cout << endl <<" First Name: "<< getFirstName();
  305. cout << endl <<" Last Name: "<< getLastName();
  306. cout << endl <<" Tax State: "<< getTaxState();
  307. cout << endl <<" Clock Number: "<< getClockNumber();
  308. cout << endl <<" Wage Rate: "<< getWageRate ();
  309. cout << endl <<" Hours: "<< getHours ();
  310.  
  311. // Display the calculated values of the Employee
  312. cout<< endl << endl <<" *** Calculated Values are *** " << endl;
  313.  
  314. // print out overtime hours based on the employee information entered
  315. cout << endl << " Overtime Hours : " << getOverTimeHrs();
  316.  
  317. // print out gross pay based on the employee information entered
  318. cout << endl << " Gross Pay : $" << getGrossPay();
  319.  
  320. // TODO - Add cout statement with call to stateTax getter function
  321. // print the calculated state tax retrieved through its private getter
  322. cout << endl << " State Tax : $" << getStateTax();
  323.  
  324. // TODO - Add cout statement with call to fedTax getter function
  325. // print the calculated federal tax retrieved through its private getter
  326. cout << endl << " Federal Tax : $" << getFedTax();
  327.  
  328. // TODO - Add cout statement with call to netPay getter function
  329. // print the calculated net pay retrieved through its private getter
  330. cout << endl << " Net Pay : $" << getNetPay();
  331.  
  332. // add a new line to separate the next employee
  333. cout <<"\n";
  334.  
  335. } // printEmployee
  336.  
  337. // Employee's destructor
  338. Employee::~Employee()
  339. {
  340. // nothing to print here, but could ...
  341. }
  342.  
  343.  
  344. // main function to start the processing
  345. int main ()
  346. {
  347. // local variables to collect user input
  348.  
  349. string myFirstName; // First Name to input
  350. string myLastName; // Last Name to input
  351. string myTaxState; // Tax State to input
  352. int myClockNumber; // Clock Number to input
  353. float myWageRate; // Wage Rate to input
  354. float myHours; // Hours to input
  355.  
  356. cout << fixed // fix the number of decimal digits
  357. << setprecision(2); // to 2
  358.  
  359. // Array of Objects to store each of our employees
  360. // This calls the default constructor for each array element
  361. Employee e[EMP_SIZE];
  362.  
  363. // prompt for information to read in employee information
  364. for (int i = 0; i < EMP_SIZE; ++i)
  365. {
  366. // Enter Employee Information
  367. cout <<"\n\n Enter Employee First Name: ";
  368. cin>>myFirstName ;
  369. cout <<"\n Enter Employee Last Name: ";
  370. cin>>myLastName ;
  371. cout <<"\n Enter Employee Tax State: ";
  372. cin>>myTaxState ;
  373. cout<<"\n Enter Employee Clock Number: ";
  374. cin>>myClockNumber;
  375. cout <<"\n Enter Employee Hourly Wage Rate: ";
  376. cin>>myWageRate;
  377. cout <<"\n Enter Employee Hours Worked for the Week: ";
  378. cin>>myHours;
  379.  
  380. // Call our constructor to create an employee object
  381. // using the input entered above. The constructor
  382. // will also put into motion the execution of the
  383. // various private member functions which will
  384. // calculate the overtime, gross pay, state tax, federal
  385. // tax, and net pay for the current employee.
  386.  
  387. // The updated object will be returned and placed in the
  388. // the element of our array of objects named "e", using the index
  389. // of the current value of our loop index "i" ... thus: e[i]
  390. e[i] = {myFirstName, myLastName, myTaxState,
  391. myClockNumber, myWageRate, myHours
  392. };
  393.  
  394. // Call the printEmployee public member function to display all
  395. // the inputted and calculated values for the current employee
  396. e[i].printEmployee();
  397.  
  398. } // for
  399.  
  400. return 0;
  401.  
  402. } // main
Success #stdin #stdout 0.01s 5320KB
stdin
Connie
Cobol
MA
98401
10.60
51.0
Mary
Apl
NH
526488
9.75
42.5
Frank
Fortran
VT
765349
10.50
37.0
Jeff
Ada
NY
34645
12.25
45
Anton
Pascal
CA
127615
8.35
40.0
stdout

 Enter Employee First Name: 
 Enter Employee Last Name: 
 Enter Employee Tax State: 
 Enter Employee Clock Number: 
 Enter Employee Hourly Wage Rate: 
 Enter Employee Hours Worked for the Week: 

*** Entered Details are *** 

 First Name: Connie
 Last Name: Cobol
 Tax State: MA
 Clock Number: 98401
 Wage Rate: 10.60
 Hours: 51.00

 *** Calculated Values are *** 

 Overtime Hours : 11.00
 Gross Pay : $598.90
 State Tax : $29.95
 Federal Tax : $149.73
 Net Pay : $419.23


 Enter Employee First Name: 
 Enter Employee Last Name: 
 Enter Employee Tax State: 
 Enter Employee Clock Number: 
 Enter Employee Hourly Wage Rate: 
 Enter Employee Hours Worked for the Week: 

*** Entered Details are *** 

 First Name: Mary
 Last Name: Apl
 Tax State: NH
 Clock Number: 526488
 Wage Rate: 9.75
 Hours: 42.50

 *** Calculated Values are *** 

 Overtime Hours : 2.50
 Gross Pay : $426.56
 State Tax : $0.00
 Federal Tax : $106.64
 Net Pay : $319.92


 Enter Employee First Name: 
 Enter Employee Last Name: 
 Enter Employee Tax State: 
 Enter Employee Clock Number: 
 Enter Employee Hourly Wage Rate: 
 Enter Employee Hours Worked for the Week: 

*** Entered Details are *** 

 First Name: Frank
 Last Name: Fortran
 Tax State: VT
 Clock Number: 765349
 Wage Rate: 10.50
 Hours: 37.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross Pay : $388.50
 State Tax : $23.31
 Federal Tax : $97.12
 Net Pay : $268.07


 Enter Employee First Name: 
 Enter Employee Last Name: 
 Enter Employee Tax State: 
 Enter Employee Clock Number: 
 Enter Employee Hourly Wage Rate: 
 Enter Employee Hours Worked for the Week: 

*** Entered Details are *** 

 First Name: Jeff
 Last Name: Ada
 Tax State: NY
 Clock Number: 34645
 Wage Rate: 12.25
 Hours: 45.00

 *** Calculated Values are *** 

 Overtime Hours : 5.00
 Gross Pay : $581.88
 State Tax : $46.55
 Federal Tax : $145.47
 Net Pay : $389.86


 Enter Employee First Name: 
 Enter Employee Last Name: 
 Enter Employee Tax State: 
 Enter Employee Clock Number: 
 Enter Employee Hourly Wage Rate: 
 Enter Employee Hours Worked for the Week: 

*** Entered Details are *** 

 First Name: Anton
 Last Name: Pascal
 Tax State: CA
 Clock Number: 127615
 Wage Rate: 8.35
 Hours: 40.00

 *** Calculated Values are *** 

 Overtime Hours : 0.00
 Gross Pay : $334.00
 State Tax : $23.38
 Federal Tax : $83.50
 Net Pay : $227.12