Title: Assignment
1Assignment 8
2Problem 10.14
- Problem 10.14
- Payroll System
- Virtual functions and Polymorphism
3Flow Chart
Start Program
Call Headers
Find Employee
Calculate Pay
Birthday?
End Program
Print Totals
4PSUEDO CODE
- Start Program
- Call the Header Files
- Determine what kind of Employee
- Determine Pay
- Is it their birthday?
- Add Birthday Bonus
- Print Results
- End Program
5Source Code - boss.cpp and boss.h
// BOSS.CPP // Member function definitions for
class Boss include ltiostream.hgt include
"boss.h" // Constructor function for class
Boss BossBoss( const char const first, const
char const last, int mn, int dy,
int yr, double s, int dept ) Employee(
first, last, mn, dy, yr, dept ) weeklySalary
s gt 0 ? s 0 // Set the Boss's salary void
BosssetWeeklySalary( double s )
weeklySalary s gt 0 ? s 0 // Get the
Boss's pay double Bossearnings() const return
weeklySalary // Print the Boss's name void
Bossprint() const cout ltlt "\nBoss " ltlt
getFirstName() ltlt ' ' ltlt getLastName()
// BOSS.H // Boss class derived from
Employee ifndef BOSS_H define BOSS_H include
"employ.h" class Boss public Employee
public Boss( const char const, const char
const, int, int, int, double 0.0,
int 0 ) void setWeeklySalary( double )
virtual double earnings() const virtual void
print() const private double weeklySalary
endif
6Source Code - commis.cpp and commis.h
// COMMIS.CPP // Member function definitions for
class CommissionWorker include
ltiostream.hgt include "commis.h" // Constructor
for class CommissionWorker CommissionWorkerCommi
ssionWorker( const char const first, const
char const last, int mn, int dy, int yr,
double s, double c, unsigned q, int dept )
Employee( first, last, mn, dy, yr, dept )
salary s gt 0 ? s 0 commission c gt 0 ? c
0 quantity q gt 0 ? q 0 // Set
CommissionWorker's weekly base salary void
CommissionWorkersetSalary( double s )
salary s gt 0 ? s 0 // Set
CommissionWorker's commission void
CommissionWorkersetCommission( double c )
commission c gt 0 ? c 0 // Set
CommissionWorker's quantity sold void
CommissionWorkersetQuantity( unsigned q )
quantity q gt 0 ? q 0 // Determine
CommissionWorker's earnings double
CommissionWorkerearnings() const return
salary commission quantity // Print the
CommissionWorker's name void CommissionWorkerpr
int() const cout ltlt "\nCommission worker "
ltlt getFirstName() ltlt ' ' ltlt
getLastName()
// COMMIS.H // CommissionWorker class derived
from Employee ifndef COMMIS_H define
COMMIS_H include "employ.h" class
CommissionWorker public Employee public
CommissionWorker(const char const, const char
const, int, int, int, double
0.0, double 0.0, unsigned 0, int 0 )
void setSalary( double ) void setCommission(
double ) void setQuantity( unsigned )
virtual double earnings() const virtual void
print() const private double salary
// base salary per week double commission
// amount per item sold unsigned quantity //
total items sold for week endif
7Source Code - hourly.cpp and hourly.h
// HOURLY.CPP // Member function definitions for
class HourlyWorker include ltiostream.hgt include
"hourly.h" // Constructor for class
HourlyWorker HourlyWorkerHourlyWorker( const
char const first, const char const last,
int mn, int dy, int yr,
double w, double h, int dept ) Employee(
first, last, mn, dy, yr, dept ) wage w gt 0
? w 0 hours h gt 0 h lt 168 ? h
0 // Set the wage void HourlyWorkersetWage(
double w ) wage w gt 0 ? w 0 // Set the
hours worked void HourlyWorkersetHours( double
h ) hours h gt 0 h lt 168 ? h 0 //
Get the HourlyWorker's pay double
HourlyWorkerearnings() const return wage
hours // Print the HourlyWorker's name void
HourlyWorkerprint() const cout ltlt
"\nHourly worker " ltlt getFirstName() ltlt
' ' ltlt getLastName()
// HOURLY.H // Definition of class
HourlyWorker ifndef HOURLY_H define
HOURLY_H include "employ.h" class HourlyWorker
public Employee public HourlyWorker(
const char const, const char const, int, int,
int, double 0.0, double 0.0,
int 0 ) void setWage( double ) void
setHours( double ) virtual double earnings()
const virtual void print() const private
double wage // wage per hour double hours
// hours worked for week endif
8Source Code - piece.cpp and piece.h
// PIECE.CPP // Member function definitions for
class PieceWorker include ltiostream.hgt include
"piece.h" // Constructor for class
PieceWorker PieceWorkerPieceWorker( const char
const first, const char const last,
int mn, int dy, int yr, double w,
unsigned q, int dept ) Employee( first, last,
mn, dy, yr, dept ) wagePerPiece w gt 0 ? w
0 quantity q gt 0 ? q 0 // Set the
wage void PieceWorkersetWage( double w )
wagePerPiece w gt 0 ? w 0 // Set the
number of items output void PieceWorkersetQuanti
ty( unsigned q ) quantity q gt 0 ? q 0
// Determine the PieceWorker's earnings double
PieceWorkerearnings() const return
quantity wagePerPiece // Print the
PieceWorker's name void PieceWorkerprint()
const cout ltlt "\nPiece worker " ltlt
getFirstName() ltlt ' ' ltlt getLastName()
// PIECE.H // PieceWorker class derived from
Employee ifndef PIECE_H define PIECE_H include
"employ.h" class PieceWorker public Employee
public PieceWorker( const char const,
const char const, int, int, int,
double 0.0, unsigned 0, int 0 ) void
setWage( double ) void setQuantity( unsigned
) virtual double earnings() const virtual
void print() const private double
wagePerPiece // wage for each piece output
unsigned quantity // output for week endif
9Source Code - employ.cpp
// Destructor deallocates dynamically allocated
memory EmployeeEmployee() delete
firstName delete lastName // Return a
pointer to the first name const char
EmployeegetFirstName() const // Const
prevents caller from modifying private data.
// Caller should copy returned string before
destructor // deletes dynamic storage to
prevent undefined pointer. return firstName
// caller must delete memory // Return a
pointer to the last name const char
EmployeegetLastName() const // Const
prevents caller from modifying private data.
// Caller should copy returned string before
destructor // deletes dynamic storage to
prevent undefined pointer. return lastName
// caller must delete memory // Return the
employee's birth date Date EmployeegetBirthDate(
) const return birthDate // Return the
employee's department code int EmployeegetDepart
mentCode() const return departmentCode
// EMPLOY.CPP // Member function definitions
for // abstract base class Employee. // // Note
No definitions given for pure virtual
functions. include ltiostream.hgt include
ltstring.hgt include ltassert.hgt include
"employ.h" // Constructor dynamically allocates
space for the // first and last name and uses
strcpy to copy // the first and last names into
the object. EmployeeEmployee( const char
const first, const char const last,
int mn, int dy, int yr, int dept )
birthDate( mn, dy, yr ), departmentCode( dept
) firstName new char strlen( first ) 1
assert( firstName ! 0 ) // test that
new worked strcpy( firstName, first )
lastName new char strlen( last ) 1
assert( lastName ! 0 ) // test that new
worked strcpy( lastName, last )
10Source Code - employ.h
// EMPLOY.H // Abstract base class
Employee ifndef EMPLOY_H define
EMPLOY_H include "date.h" class Employee
public Employee( const char const, const
char const, int, int, int, int)
Employee() const char getFirstName()
const const char getLastName() const
Date getBirthDate() const int
getDepartmentCode() const // Pure virtual
functions make Employee abstract base class.
virtual double earnings() const 0 // pure
virtual virtual void print() const 0 //
pure virtual private char firstName char
lastName Date birthDate int
departmentCode endif
11Source Code - date.cpp
// DATE.CPP // Member function definitions for
Date class. include ltiostream.hgt include
"date.h" // Constructor Confirm proper value
for month // call utility function checkDay to
confirm proper // value for day. DateDate( int
mn, int dy, int yr ) if ( mn gt 0 mn lt 12
) // validate the month month
mn else month 1 cout ltlt
"Month " ltlt mn ltlt " invalid. Set to month 1.\n"
year yr gt 1900 yr lt 2100 ? yr
1990 day checkDay( dy ) //
validate the day // Utility function to
confirm proper day value // based on month and
year. int DatecheckDay( int testDay ) const
int daysPerMonth 13 0, 31, 28, 31, 30,
31, 30, 31, 31, 30,
31, 30, 31
if ( testDay gt 0 testDay lt daysPerMonth
month ) return testDay if ( month
2 // February Check for possible leap
year testDay 29 ( year 400
0 (year 4 0 year 100 ! 0 ) ) )
return testDay cout ltlt "Day " ltlt testDay
ltlt " invalid. Set to day 1.\n" return 1 //
leave object in consistent state if bad
value // Return the month int DategetMonth()
const return month // Return the day int
DategetDay() const return day // Return
the year int DategetYear() const return year
// Print Date object in form
month/day/year void Dateprint() const cout
ltlt month ltlt '/' ltlt day ltlt '/' ltlt year
12Source Code - employ.h
// DATE.H // Declaration of the Date class. //
Member functions defined in DATE1.CPP ifndef
DATE_H define DATE_H class Date public
Date( int 1, int 1, int 1900 ) // default
constructor int getMonth() const // return
the month int getDay() const // return the
day int getYear()const // return the year
void print() const // print date in
month/day/year format private int month //
1-12 int day // 1-31 based on month int
year // any year // utility function to
test proper day for month and year int
checkDay( int ) const
13Source Code - Main Program
Exercise 10.14 solution // Driver for Employee
hierarchy include ltiostream.hgt include
ltiomanip.hgt include lttime.hgt include
ltstdlib.hgt include "employ.h" include
"boss.h" include "commis.h" include
"piece.h" include "hourly.h" int
determineMonth() int main() // set output
formatting cout ltlt setiosflags( iosfixed
iosshowpoint ) ltlt setprecision( 2
)
Boss b( "John", "Smith", 6, 15, 1944, 800.00, 1
) CommissionWorker c( "Sue", "Jones", 9, 8,
1954, 200.0, 3.0, 150, 1 ) PieceWorker p(
"Bob", "Lewis", 3, 2, 1965, 2.5, 200, 1
) HourlyWorker h( "Karen", "Price", 12, 29,
1960, 13.75, 40, 1 ) Employee ptr 4
b, c, p, h int month
determineMonth() cout ltlt "The month is " ltlt
month ltlt "\nThe payroll is\n" for ( int x
0 x lt 4 x ) ptr x -gtprint() cout ltlt
" of department " ltlt ptr x -gtgetDepartmentCode()
ltlt "\n whose birthday is " ptr
x -gtgetBirthDate().print() cout ltlt " earned
" if ( ptr x -gtgetBirthDate().getMonth()
month ) cout ltlt ptr x -gtearnings() 100.0
ltlt " HAPPY
BIRTHDAY!\n" else cout ltlt
ptrx-gtearnings() ltlt endl return 0 //
Determine the current month using standard
library functions // of time.h. int
determineMonth() time_t currentTime char
monthString 3 time( currentTime
) strftime( monthString, 3, "m", localtime(
currentTime ) ) return atoi( monthString )
14Source Code - Main Program part 2
cout ltlt ptr x -gtearnings() 100.0
ltlt " HAPPY BIRTHDAY!\n" else
cout ltlt ptrx-gtearnings() ltlt endl return
0 // Determine the current month using
standard library functions // of time.h. int
determineMonth() time_t currentTime char
monthString 3 time( currentTime
) strftime( monthString, 3, "m", localtime(
currentTime ) ) return atoi( monthString )
15Output