Needing help with a c++ program I'm writing

This is a discussion on Needing help with a c++ program I'm writing within the C++ Programming forums, part of the General Programming Boards category; Here is the question.... Write a base class Worker and derived classes HourlyWorker and SalariedWorker. Every worker has a name ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    1

    Needing help with a c++ program I'm writing

    Here is the question....
    Write a base class Worker and derived classes HourlyWorker and SalariedWorker. Every worker has a name and a salary rate. Write a virtual function ComputePay (in hour) that computes weekly pay for every worker. An hourly worker gets paid the hourly wages for the actual number of hours worked. The hours are at most 40 per week. If it is greater than 40, the worker gets 1.5 times of the hourly rate for excess hour. Te salaried worker gets paid the hourly wage for 40 hours, no matter what the actual number of hours is .

    And here is the little code I have so far. The int main() was given to me, and I need to write the base worker class and the two derived classes. But I am lost. Thanks for any help!

    Code:
     
    #include <iostream>
    using namespace std;
    
    class Worker
    {
         public:
    
        {
               virtual compute_Pay(float hours);} //this doesn't like to compile correctly
                                                                      //hourly and salaried functions
        }
    }
    class hourly: public Worker
    {
    
               compute_Pay; //need to add the computer_Pay in here, salaried, and worker.
                                     //and functions that calculate hourly guys with/without overtime
    }
    
    class Salaried: public Worker
    {
              compute_Pay=; //also need to add functions that calculate just salaried guys
    };
    
    
    
    
    
    int main()
    {
              HourlyWorker a("Sam", 20);
              HourlyWorker b("Mary", 15);
              SalariedWorker c("Tom", 30);
              SalariedWorker d("Pat", 40);
    
                         cout << "Hourly worker " << a.get_name() << " earns $" << a.get_salary();
                         cout << " and worked 20 hours for a pay of $" << a.compute_pay(20) << "\n";
                         cout << "Hourly worker " << b.get_name() << " earns $" << b.get_salary();
                         cout << " and worked 50 hours for a pay of $" << b.compute_pay(50) << "\n";
                         cout << "Salaried worker " << c.get_name() << " earns $" << c.get_salary();
                         cout << " and worked 20 hours for a pay of $" << c.compute_pay(20) << "\n";
                         cout << "Salaried worker " << d.get_name() << " earns $" << d.get_salary();
                         cout << " and worked 50 hours for a pay of $" << d.compute_pay(50) << "\n";
    
                         system("pause");
                        return 0;
    }
    Last edited by woodkm; 11-14-2008 at 05:51 AM.

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Code:
    virtual compute_Pay(float hours);}
    You have a closing brace with no opening once, hence the error in the syntax.
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by swgh View Post
    Code:
    virtual compute_Pay(float hours);}
    You have a closing brace with no opening once, hence the error in the syntax.
    And no return type, which is not valid in C++.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    Quote Originally Posted by woodkm View Post
    Code:
     
    #include <iostream>
    using namespace std;
    
    class Worker
    {
         public:
        {
               virtual compute_Pay(float hours);} //this doesn't like to compile correctly
                                                                      //hourly and salaried functions
        }
    [/COLOR]}
    class hourly: public Worker
    {
    
               compute_Pay; //need to add the computer_Pay in here, salaried, and worker.
                                     //and functions that calculate hourly guys with/without overtime
    }
    
    class Salaried: public Worker
    {
              compute_Pay=; //also need to add functions that calculate just salaried guys
    };
    
    
    
    
    
    int main()
    {
              HourlyWorker a("Sam", 20);
              HourlyWorker b("Mary", 15);
              SalariedWorker c("Tom", 30);
              SalariedWorker d("Pat", 40);
    
                         cout << "Hourly worker " << a.get_name() << " earns $" << a.get_salary();
                         cout << " and worked 20 hours for a pay of $" << a.compute_pay(20) << "\n";
                         cout << "Hourly worker " << b.get_name() << " earns $" << b.get_salary();
                         cout << " and worked 50 hours for a pay of $" << b.compute_pay(50) << "\n";
                         cout << "Salaried worker " << c.get_name() << " earns $" << c.get_salary();
                         cout << " and worked 20 hours for a pay of $" << c.compute_pay(20) << "\n";
                         cout << "Salaried worker " << d.get_name() << " earns $" << d.get_salary();
                         cout << " and worked 50 hours for a pay of $" << d.compute_pay(50) << "\n";
    
                         system("pause");
                        return 0;
    }
    You also seem to have a local scope inside a class... not sure if it is legal or not, but it still makes no sense.
    And then you have declared the function inside hourly... But it isn't a function, but rather just a name - an identifier, lacking any sort of type. What is it supposed to be? Think on that one, you.
    And in third class, you have done the gravest sin of all... You are trying to assign to what is supposed to be a function, and which yet isn't, because it lacks everything that marks it as one.

    Do you have your books handy? It looks like need to get back to one.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    137
    Yeah take off the braces around Public:

    Take off that brace at the end with the semicolon where you said it doesn't compile right.

    Add a constructor :
    public:
    Worker();
    virtual int ComputePay();

    in your other classes, override:
    Code:
    class name {
     public:
    int ComputePay();
    }
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    I find it good practice to add "virtual" to all functions that virtual, in the base class and the derived classes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing a program to make a calendar
    By Northstar in forum C Programming
    Replies: 17
    Last Post: 11-07-2007, 10:34 AM
  2. writing a calendar program help please!!!
    By Newbie2006 in forum C Programming
    Replies: 7
    Last Post: 11-20-2002, 06:36 PM
  3. Replies: 5
    Last Post: 11-19-2002, 08:36 PM
  4. Help with a file writing program
    By pritesh in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 12:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21