improvements

This is a discussion on improvements within the C++ Programming forums, part of the General Programming Boards category; Does anyone know how i can improve this code. specifically my constructor. Code: #include <iostream> using namespace std; class netpay ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    230

    improvements

    Does anyone know how i can improve this code. specifically my constructor.
    Code:
    #include <iostream>
    using namespace std;
    class netpay
    {
    private:
    const double PAYRATE;
    double ssTaxRate, fedTaxRate, stateTaxRate;
    double netPay, grossPay, ssTax, fedTax, stateTax, healthIns;
    int unionDue, hours, dependants;
    public:
    
    //my horrible constructor
    netpay() : PAYRATE(16.78), ssTaxRate(0.06), fedTaxRate(0.14),
               stateTaxRate(0.05), netPay(0.0), grossPay(0.0), ssTax(0.0), 
               fedTax(0.0), stateTax(0.0), healthIns(0.0), unionDue(10), 
               hours(0), dependants(0)
    {}
    void getinfo();
    void calnet();
    void showinfo();
    };
    void netpay:: getinfo()
    {
        do
        {
            cout << "Enter the number of hours worked: "<<endl;
            cin >> hours;
        }while (hours <= 0);
        
        do
        {
            cout << "Enter the number of dependants: "<<endl;
            cin >> dependants;
        }while (dependants < 0);
        if (dependants >= 3)
            healthIns = 35;
        else
            healthIns = 0;
    }
    void netpay:: calnet() // calculates the netpay
    {
        if (hours > 40)
            grossPay = PAYRATE * 40 + 1.5 * PAYRATE * (hours - 40);
        else
            grossPay = hours * PAYRATE;
        ssTax = grossPay * ssTaxRate;
        fedTax = grossPay * fedTaxRate;
        stateTax = grossPay * stateTaxRate;
        netPay = grossPay - (ssTax + fedTax + stateTax + healthIns + unionDue);
        
    }
    
    void netpay:: showinfo() // outputs all of the the users information
    {
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);
        cout << "Your grosspay was: "<< grossPay <<endl;
        cout << "Your social security tax was: " << ssTax <<endl;
        cout << "Your federal income tax was: " << fedTax <<endl;
        cout << "Your State income tax was: "  << stateTax <<endl;
        cout << "Your union dues are: " << unionDue <<endl;
        cout << "Your health insurance was: " << healthIns <<endl;
        cout << "Your netpay was: " << netPay << endl;
    }
    int main()
    {
        char ans('n');
        do
        {
            netpay n1; // create object
        
            cout << "This program will calculate your netpay. " <<endl;
            n1.getinfo(); //these are
            n1.calnet(); //  the function
            n1.showinfo(); // calls
            cout << "Would you like to input again(y/n)"<<endl;
            cin >> ans;
        }while (ans == 'y' || ans == 'Y');
        return 0;
    }


    NOTE: if you notice any other things that i can improve on plz tell me.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    is there any particular reason you want to initialize all of your variables like that? If not you can just do it within the function itself.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I'd allow the user to pass values for those variables as parameters to the constructor perhaps with defaults). I'd leave them in the initializer list too.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    well even so, the initialization can be done in the function itself. The look is just nicer. I don't like more than one or two up there. But that's just an opinion thing I guess.

    It's just that it doesn't run any faster that way in case that's what you were trying to gain.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Nah... I just like to keep as much out of the function body as possible.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    so i guess there is no better way just a matter of opinion and look. ok then thx
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    well "better" usually suggests opinion anyway. Are you looking for faster?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  8. #8
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Indeed... Anyways, here's a bit more opinion for ya.

    Indent more in the class declaration (the variables and functions - distinguish them from the "public:" and "private:")... It just makes it a bit easier to read.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    i konw about the indenting that happened when i copied and pasted
    im looking for standard style
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  10. #10
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Here are some links to coding standards.

    I am a bit confused about exactly what you are looking for though.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  11. #11
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    for the most part i was jsut looking for what most people would probably do. but now i know its an opinion thing.
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by FillYourBrain
    well even so, the initialization can be done in the function itself. The look is just nicer. I don't like more than one or two up there. But that's just an opinion thing I guess.

    It's just that it doesn't run any faster that way in case that's what you were trying to gain.
    It runs much faster for objects, but not typically for primitives, although there can be other benefits (e.g. inlining) that can come from using the initialization list.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. See what you think and suggested improvements
    By Game in forum C++ Programming
    Replies: 6
    Last Post: 11-28-2008, 01:31 PM
  2. Improvements on Recursive Directory Listing
    By laney69er in forum C Programming
    Replies: 1
    Last Post: 03-15-2006, 01:39 AM
  3. Kitchen Timer sugestions for improvements
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 12-08-2005, 11:53 AM
  4. Newbie program - improvements?
    By -JM in forum C++ Programming
    Replies: 9
    Last Post: 06-26-2005, 07:53 PM
  5. Game Programming Board improvements
    By JoshG in forum Game Programming
    Replies: 8
    Last Post: 10-17-2002, 04:32 PM

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