Thread: homework problem

  1. #1
    Unregistered
    Guest

    homework problem

    This assignment is from deitel and deitel text
    it states modify the payroll sysem of fig 10.1 to add private data members birthdate ( a Date object and departmentCode ( an int ) to class Employee. Assume this payroll is processed once per month. THen as your program calculates the payroll for each Employee ( polymorphically add a 100,000 bonus to the person's payroll amount if this is the month in which the Employees birthday occurs

    #include<iostream>
    using std::cout;
    using std::endl;


    #include<cstring>
    #include<cassert>
    #include<iomanip>

    using std::ios;
    using std::setiosflags;
    using std::setprecision;

    class Employee {
    public:
    Employee( const char *, const char * );
    ~Employee(); // destructor reclaims memory
    const char *getFirstName() const;
    const char *getLastName() const;

    // Pure virtual function makes Employee abstract base class
    virtual double earnings() const = 0; // pure virtual
    virtual void print() const; // virtual
    private:
    char *firstName;
    char *lastName;
    };


    Employee::Employee( const char *first, const char *last )
    {
    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:rint() const
    { cout << firstName << ' ' << lastName; }


    //************************************************** ******

    class Boss : public Employee {
    public:
    Boss( const char *, const char *, 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, double s )
    : Employee( first, last ) // 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:rint() const
    {
    cout << "\n Boss: ";
    Employee:rint();
    }


    //************************************************** *****
    class CommissionWorker : public Employee {
    public:
    CommissionWorker( const char *, const char *,
    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, double s, double c, int q )
    : Employee( first, last ) // 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:rint() const
    {
    cout << "\nCommission worker: ";
    Employee:rint();
    }


    //***********************************************
    class PieceWorker : public Employee {
    public:
    PieceWorker( const char *, const char *,
    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,
    double w, int q )
    : Employee( first, last ) // 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:rint() const
    {
    cout << "\n Piece worker: ";
    Employee:rint();
    }



    class HourlyWorker : public Employee {
    public:
    HourlyWorker( const char *, const char *,
    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,
    double w, double h )
    : Employee( first, last ) // 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:rint() const
    {
    cout << "\n Hourly worker: ";
    Employee:rint();
    }



    //************************************************** ****************


    //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", 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", 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", 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", 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();
    }

  2. #2
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Do you actually have a question or did you just post 80 or so lines from the book for someone here to do your homework for you?

    Read the FAQ, your answers in there.

  3. #3
    Unregistered
    Guest

    homework

    how do i get started i am dumb

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    362

    Someone needs to do it...

    Unregistered,

    1. Learn to use code tags. That is, precede the code you wish to show us with "code" enclosed in square brackets and end it with "/code" enclosed in square brackets. This will set your code off with the proper indentation, etc.

    2. Only under extreme circumstances should you ever include your entire code. We're interested in the problem area, not the whole program.

    3. You haven't asked a question. What seems to be the problem?

    4. We don't do homework. We help, but the homework is yours to complete. (Be specific about what the problem is.)

    Getting real? The code you've submitted is "patented" textbook stuff. There are no mod's, which means that you haven't tried anything with it. You looked it over, gave up and sent it here. Doesn't fly well in this forum.

    (Would that I could flatter myself that I so eloquently reached the heart of the matter in my post that jdinger did in just two lines!)

    -Skipper

    P.S. You're not dumb. You're new. And, you've got a lot to learn. Follow jdinger's advice and read the FAQ.
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    1. Learn to use code tags. That is, precede the code you wish to show us with "code" enclosed in square brackets and end it with "/code" enclosed in square brackets. This will set your code off with the proper indentation, etc.
    To show you an example, this:

    [code]
    /* your code here */
    [/code]

    ... will become this
    Code:
    /* your code here */
    Damn, I must be bored if I'm posting this
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Agreed!

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. One More Homework Problem
    By joxerjen in forum C++ Programming
    Replies: 5
    Last Post: 10-12-2005, 04:39 PM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. homework assignment problem
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 10-29-2001, 10:24 AM