Introduction
Payroll is the archetypal inheritance example because every employee is paid, but by different rules. An abstract Employee defines monthly_pay(); three subclasses implement it. Tax is computed once in the base class from whatever the subclass returns — the template-method pattern.
Problem statement
type,id,name,dept,...) for salaried, hourly (with 1.5× overtime beyond 160 hours) and commissioned employees. Reject unknown types, wrong field counts, out-of-range values and duplicate ids. Print a payslip table with pay basis, gross, progressive tax (0% to $2,000, 10% to $5,000, 20% above) and net pay, totals, a department summary, totals per pay model and the highest earner.Objectives
- Design an abstract base class and three concrete subclasses
- Store a heterogeneous collection as vector<unique_ptr<Base>>
- Use the template-method pattern for shared logic
- Parse and validate structured records
Features
- Three pay models with their own rules
- Progressive tax in the base class
- Clear rejection reasons for bad rows
- Department and pay-model breakdowns
Concepts demonstrated
- Inheritance
- Pure virtual functions
- Template method pattern
- std::unique_ptr
- Validation
System architecture
employee.h declares Employee with pure virtual monthly_pay, kind and pay_basis, plus non-virtual tax and net that call monthly_pay. Salaried, Hourly and Commissioned override them. parse_employee splits a line on commas and constructs the right subclass, throwing std::invalid_argument for problems. main.cpp reads the roster, checks ids with a std::set, then loops over vector<unique_ptr<Employee>> — every call is resolved at run time.
Folder structure
Complete source code
Makefile
What each file does
- src/employee.h
- The
Employeehierarchy,moneyand theparse_employeefactory. - src/employee.cpp
- Pay rules for each subclass, the progressive tax, currency formatting and the roster parser.
- src/main.cpp
- Reading the roster, duplicate checks, the payslip table and the summaries.
- Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
# type,id,name,dept,... salaried,E01,Asha,Engineering,96000 salaried,E02,Vikram,Engineering,72000 hourly,E03,Ravi,Support,22.50,152 hourly,E04,Lina,Support,24,180 commission,E05,Meera,Sales,1500,42000,0.08 commission,E06,Omar,Sales,1500,9000,0.08 salaried,E07,Priya,Finance,60000 hourly,E08,Sam,Support,20 salaried,E01,Duplicate,Engineering,50000 commission,E09,Kiran,Sales,1000,5000,1.5 contract,E10,Zoe,Design,4000
Testing
- Compare
./app < tests/sample-input.txtwithtests/expected-output.txt. - Lina works 180 hours at $24: 160 × 24 + 20 × 36 = $4,560.
- Asha's $8,000 gross: tax = 10% of $3,000 + 20% of $3,000 = $900.
- Four rows are rejected: a missing field, a duplicate id, a commission rate of 1.5 and an unknown type.
Common errors
Deleting through a base pointer leaks or crashes
Give the base class a virtual destructor (virtual ~Employee() = default;).
All employees are paid like the base class
You stored objects by value (slicing). Store pointers — unique_ptr<Employee>.
Possible improvements
- Add interns with a fixed stipend
- Read the tax brackets from a file
- Produce individual payslip files
Additional challenges
- 1Add a Manager subclass that earns a bonus per direct report.
- 2Sort payslips by net pay with a single std::sort call.
- 3Show year-to-date totals across several monthly runs.
Built and tested it yourself? Mark it to update your dashboard.