-
Help Me Please....
//I need help this is payroll program //that adds 100 dollars to the
//Employees account on his or her birthday
//THe program has a problem please help...
//I have to use polymorphism
#include<iostream>
using std::cout;
using std::endl;
#include<cstring>
#include<cassert>
#include<iomanip>
using std::ios;
using std::setiosflags;
using std::setprecision;
class Date {
public:
Date( int = 1, int = 1, int = 1900 ); // default constructor
int getmonth()const { return month; };
void print() const; // print date in month/day/year format
~Date(); // provided to confirm destruction order
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 );
};
Date:: Date( int mn, int dy, int yr )
{
if ( mn > 0 && mn <= 12 ) // validate the month
month = mn;
else {
month = 1;
cout << "Month " << mn << " invalid. Set to month 1.\n";
}
year = yr; // should validate yr
day = checkDay( dy ); // validate the day
cout << "\nDate object constructor for date ";
print(); // interesting: a print with no arguments
cout << endl;
}
// Print Date object in form month/day/year
void Date:: print() const
{ cout << month << '/' << day << '/' << year; }
// Destructor: provided to confirm destruction order
Date::~Date()
{
cout << "Date object destructor for date ";
print();
cout << endl;
}
// Utility function to confirm proper day value
// based on month and year.
// Is the year 2000 a leap year?
int Date::checkDay( int testDay )
{
static const int daysPerMonth[ 13 ] =
{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
return testDay;
if ( month == 2 && // February: Check for leap year
testDay == 29 &&
( year % 400 == 0 ||
( year % 4 == 0 && year % 100 != 0 ) ) )
return testDay;
cout << "Day " << testDay << " invalid. Set to day 1.\n";
return 1; // leave object in consistent state if bad value
}
//************************************************** ************
class Employee {
public:
Employee( const char *, const char *,int, int, int, int);
~Employee(); // destructor reclaims memory
const char *getFirstName() const;
const char *getLastName() const;
const Date getbirthDate() const;
// Pure virtual function makes Employee abstract base class
virtual double earnings() const = 0; // pure virtual
virtual double bonus(int bonus) const;
virtual void print() const; // virtual
private:
char *firstName;
char *lastName;
const Date birthDate; //What do i do with this
};
Employee::Employee( const char *first, const char *last, int deptno,
int m, int d, int y)
:birthDate( m, d, y)//, getbirthDate()
{
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 );
}
// Destructor deallocates dynamically allocated memory
Employee::~Employee()
{
delete [] firstName;
delete [] lastName;
}
// Return a pointer to the first name
// Const return type prevents caller from modifying private
// data. Caller should copy returned string before destructor
// deletes dynamic storage to prevent undefined pointer.
const char *Employee::getFirstName() const
{
return firstName; // caller must delete memory
}
// Return a pointer to the last name
// Const return type prevents caller from modifying private
// data. Caller should copy returned string before destructor
// deletes dynamic storage to prevent undefined pointer.
const char *Employee::getLastName() const
{
return lastName; // caller must delete memory
}
// Print the name of the Employee
void Employee::print() const
{ cout << firstName << ' ' << lastName; }
double Employee::bonus(int) const
{
return bonus(100.00);
}
//const :getbirthDate() const
//{
// return;
//}
//************************************************** ******
class Boss : public Employee {
public:
Boss( const char *, const char *,int, int, int, int, double = 0.0 );
void setWeeklySalary( double );
virtual double earnings() const;
virtual void print() const;
private:
double weeklySalary;
};
Boss::Boss( const char *first, const char *last, int deptno, int m, int d, int y, double s)
:Employee( first, last, deptno, m, d, y) // call base-class constructor
{ setWeeklySalary( s ); }
// Set the Boss's salary
void Boss::setWeeklySalary( double s )
{ weeklySalary = s > 0 ? s : 0; }
// Get the Boss's pay
double Boss::earnings() const { return weeklySalary; }
// Print the Boss's name
void Boss::print() const
{
cout << "\n Boss: ";
Employee::print();
}
//************************************************** *****
class CommissionWorker : public Employee {
public:
CommissionWorker( const char *, const char *,int, int, int, int,
double = 0.0, double = 0.0,
int = 0 );
void setSalary( double );
void setCommission( double );
void setQuantity( int );
virtual double earnings() const;
virtual void print() const;
private:
double salary; // base salary per week
double commission; // amount per item sold
int quantity; // total items sold for week
};
// Constructor for class CommissionWorker
CommissionWorker::CommissionWorker( const char *first,
const char *last, int deptno, int m, int d, int y, double s, double c, int q )
:Employee( first, last, deptno, m, d, y ) // call base-class constructor
{
setSalary( s );
setCommission( c );
setQuantity( q );
}
// Set CommissionWorker's weekly base salary
void CommissionWorker::setSalary( double s )
{ salary = s > 0 ? s : 0; }
// Set CommissionWorker's commission
void CommissionWorker::setCommission( double c )
{ commission = c > 0 ? c : 0; }
// Set CommissionWorker's quantity sold
void CommissionWorker::setQuantity( int q )
{ quantity = q > 0 ? q : 0; }
// Determine CommissionWorker's earnings
double CommissionWorker::earnings() const
{ return salary + commission * quantity; }
// Print the CommissionWorker's name
void CommissionWorker::print() const
{
cout << "\nCommission worker: ";
Employee::print();
}
//***********************************************
class PieceWorker : public Employee {
public:
PieceWorker( const char *, const char *,int, int, int, int,
double = 0.0, int = 0);
void setWage( double );
void setQuantity( int );
virtual double earnings() const;
virtual void print() const;
private:
double wagePerPiece; // wage for each piece output
int quantity; // output for week
};
// Constructor for class PieceWorker
PieceWorker::PieceWorker( const char *first, const char *last,
int deptno, int m, int d, int y, double w, int q )
: Employee( first, last, deptno, m, d, y ) // call base-class constructor
{
setWage( w );
setQuantity( q );
}
// Set the wage
void PieceWorker::setWage( double w )
{ wagePerPiece = w > 0 ? w : 0; }
// Set the number of items output
void PieceWorker::setQuantity( int q )
{ quantity = q > 0 ? q : 0; }
// Determine the PieceWorker's earnings
double PieceWorker::earnings() const
{ return quantity * wagePerPiece; }
// Print the PieceWorker's name
void PieceWorker::print() const
{
cout << "\n Piece worker: ";
Employee::print();
}
//*********************************************
class HourlyWorker : public Employee {
public:
HourlyWorker( const char *, const char *,int, int, int, int,
double = 0.0, double = 0.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
};
HourlyWorker::HourlyWorker( const char *first,
const char *last,int deptno, int m, int d, int y,
double w, double h )
:Employee( first, last, deptno, m, d, y ) // call base-class constructor
{
setWage( w );
setHours( h );
}
// Set the wage
void HourlyWorker::setWage( double w )
{ wage = w > 0 ? w : 0; }
// Set the hours worked
void HourlyWorker::setHours( double h )
{ hours = h >= 0 && h < 168 ? h : 0; }
// Get the HourlyWorker's pay
double HourlyWorker::earnings() const
{
if ( hours <= 40 ) // no overtime
return wage * hours;
else // overtime is paid at wage * 1.5
return 40 * wage + ( hours - 40 ) * wage * 1.5;
}
// Print the HourlyWorker's name
void HourlyWorker::print() const
{
cout << "\n Hourly worker: ";
Employee::print();
}
//************************************************** ****************
//main program
void virtualViaPointer( const Employee * );
void virtualViaReference( const Employee & );
int main()
{
// set output formatting
cout << setiosflags( ios::fixed | ios::showpoint )
<< setprecision( 2 );
Boss b( "John", "Smith",1, 2, 14, 1968, 800.00 );
b.print(); // static binding
cout << " earned $" << b.earnings(); // static binding
virtualViaPointer( &b ); // uses dynamic binding
virtualViaReference( b ); // uses dynamic binding
CommissionWorker c( "Sue", "Jones",2, 3, 24, 1950, 200.0, 3.0, 150 );
c.print(); // static binding
cout << " earned $" << c.earnings(); // static binding
virtualViaPointer( &c ); // uses dynamic binding
virtualViaReference( c ); // uses dynamic binding
PieceWorker p( "Bob", "Lewis", 1, 5, 28, 1973, 2.5, 200 );
p.print(); // static binding
cout << " earned $" << p.earnings(); // static binding
virtualViaPointer( &p ); // uses dynamic binding
virtualViaReference( p ); // uses dynamic binding
HourlyWorker h( "Karen", "Price", 1, 6, 14, 1965, 13.75, 40 );
h.print(); // static binding
cout << " earned $" << h.earnings(); // static binding
virtualViaPointer( &h ); // uses dynamic binding
virtualViaReference( h ); // uses dynamic binding
cout << endl;
return 0;
}
// Make virtual function calls off a base-class pointer
// using dynamic binding.
void virtualViaPointer( const Employee *baseClassPtr )
{
baseClassPtr->print();
cout << " earned $" << baseClassPtr->earnings();
}
// Make virtual function calls off a base-class reference
// using dynamic binding.
void virtualViaReference( const Employee &baseClassRef )
{
baseClassRef.print();
cout << " earned $" << baseClassRef.earnings();
}
-
-
Please read the faq's and stuff before posting.... the following are not good:
- No code tags (as already stated).
- Excessive amount of code. 400 lines is way too much. Narrow down your problem, then post only the appropriate bits.
- The title of your post (Help me please) is pretty lame, try and put something that is relevant to what your asking..... which leads me nicely into...
- What exactly are you asking? You say your code "has a problem", well what is it then? If you don't tell us, we have to go second guessing what you want...if we can be bothered that is, and I certainly can't!