Skip to content
C++ project #16Intermediate

Employee Payroll with Inheritance

Salaried, hourly and commissioned employees processed polymorphically: payslips with progressive tax, department and pay-model summaries.

Estimated time
~4 hours to build
Files
7 files · 418 lines
Technologies
C++17, <memory>, <map>, <set>, Header/source split

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

Read a roster in CSV-like form (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.

TextArchitecture diagram
Employee (abstract)
monthly_pay()=0 kind()=0 pay_basis()=0
tax() net() <- shared, call monthly_pay()
/ | \
Salaried Hourly Commissioned
annual / 12 160h + 1.5x OT base + rate x sales

Folder structure

TextProject tree
employee-payroll-hierarchy/
├── README.md
├── Makefile
├── src/employee.cpp
├── src/employee.h
├── src/main.cpp
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/employee.h
The Employee hierarchy, money and the parse_employee factory.
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: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
Line 9 rejected: unknown type or wrong number of fields for 'hourly'
Line 10 rejected: duplicate id E01
Line 11 rejected: rate is out of range
Line 12 rejected: unknown type or wrong number of fields for 'contract'
ID Name Dept Type Basis Gross Tax Net
-----------------------------------------------------------------------------------------------------------
E01 Asha Engineering salaried $96,000.00/year $8,000.00 $900.00 $7,100.00
E02 Vikram Engineering salaried $72,000.00/year $6,000.00 $500.00 $5,500.00
E03 Ravi Support hourly 152h x $22.50 $3,420.00 $142.00 $3,278.00
E04 Lina Support hourly 180h x $24.00 (incl. overtime) $4,560.00 $256.00 $4,304.00
E05 Meera Sales commission $1,500.00 + 8% of $42,000.00 $4,860.00 $286.00 $4,574.00
E06 Omar Sales commission $1,500.00 + 8% of $9,000.00 $2,220.00 $22.00 $2,198.00
E07 Priya Finance salaried $60,000.00/year $5,000.00 $300.00 $4,700.00
-----------------------------------------------------------------------------------------------------------
Totals $34,060.00 $2,406.00 $31,654.00
By department
Engineering 2 staff $14,000.00 avg $7,000.00
Finance 1 staff $5,000.00 avg $5,000.00
Sales 2 staff $7,080.00 avg $3,540.00
Support 2 staff $7,980.00 avg $3,990.00
By pay model
commission $7,080.00
hourly $7,980.00
salaried $19,000.00
Highest gross: Asha ($8,000.00)
Input
# 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.txt with tests/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

  1. 1Add a Manager subclass that earns a bonus per direct report.
  2. 2Sort payslips by net pay with a single std::sort call.
  3. 3Show year-to-date totals across several monthly runs.

Built and tested it yourself? Mark it to update your dashboard.