Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Quick C++ Question....Quick Answer please

Options
  • 22-11-2005 10:34pm
    #1
    Closed Accounts Posts: 1,061 ✭✭✭


    Dunno how to phrase this but...
    I have a class with(for basic example)this:

    class employee
    {
    private:
        int idno;
    float sales;
    public:
        calcComission();           <---- part of my problem too..
    };
    

    and now I need to store a value, as entered by the user.

    This value will remain constant for eact of the objects created for employee, so putting it in the class as say:
    float commission;
    

    won't really work, cos then I'd have to set it every time i create a new object, which i don't want to do.
    I can't really put it in main, cos i need to calculate a figure from the commission fig and from the float sales above.
    Also can't make it a global var, cos it's apparently bad practice, and I don't want to do it anyway..
    Can't just put it in main() either...
    Put it in another class?
    Would it work if i made the float commission; a base class and derived the employee class from it?

    Put it in a struct? I tried that already and came into some error.. cant remember at the moment but I'd like to hear your suggestions first..


Comments

  • Closed Accounts Posts: 1,061 ✭✭✭dawballz


    Jaysus... must have came to the wrong place looking for a quick reply :D


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    Make commission a static variable


  • Closed Accounts Posts: 1,061 ✭✭✭dawballz


    MrPinK wrote:
    Make commission a static variable

    In the employee class yeah?

    edit; won't work.. get a linker error.
    if the var is set as static, surely it won't be allowed to be changed after it has been initialised?


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    Yeah.
    class employee
    {
    private:
        static float commission;
        int idno;
        float sales;
    public:
        static void setCommission(float c);
    };
    
    if the var is set as static, surely it won't be allowed to be changed after it has been initialised?
    As long as the variable isn't a const, it can still be changed


  • Closed Accounts Posts: 1,061 ✭✭✭dawballz


    Ok it's not working for me..
    could i send you my code?
    it's a lot more complicated than that above.. maybe you could point me in the right direction...?


  • Advertisement
  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    Stick it in a PM and I'll take a quick look


  • Closed Accounts Posts: 1,061 ✭✭✭dawballz


    sent.. had to cut some off cos it was too long..


  • Registered Users Posts: 1,481 ✭✭✭satchmo


    Including static member variables inside a class definition is only a declaration of the variable and not a definition. Static variables have external linkage, so have to be defined outside the class definition in order to be usable. Add this at the end of your code:
    float employee::commission;
    
    and it should work. If you want to initialise it to a value you should do it there too.


  • Closed Accounts Posts: 1,061 ✭✭✭dawballz


    Thanks, got it sorted..
    thanks to MrPink..

    ah.. that sense of accomplishment... :D:D:D


Advertisement