Thread: Help me clean this up.

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    9

    Question Help me clean this up.

    I got the idea to write this out of a book as a little project for myself. It works, but I'm thinking their had be be an easier way. I'm just starting, and struggling with the usage (not concept) of classes. Any suggestions will be greatly appreciated.

    Thanks in advance.

    #include <iostream.h>

    class Bob
    {
    public:
    Bob();
    void input_bob();
    void output_bob();
    int getBobAge() {return itsAge;}
    int getBobYearOnJob() {return itsYears;}
    double getBobSalary() {return itsSalary;}
    void setBobAge(int Age) {itsAge = Age;}
    void setBobYearsOnJob(int YOJ) {itsYears = YOJ;}
    void setBobSalary(double Salary) {itsSalary = Salary;}
    private:
    int itsAge, itsYears;
    double itsSalary;
    };

    class Martha
    {
    public:
    Martha();
    void input_martha();
    void output_martha();
    int getMarthaAge() {return itsAge;}
    int getMarthYearOnJob() {return itsYears;}
    double getMarthaSalary() {return itsSalary;}
    void setMarthaAge(int Age) {itsAge = Age;}
    void setMarthaYearsOnJob(int YOJ) {itsYears = YOJ;}
    void setMarthaSalary(double Salary) {itsSalary = Salary;}
    private:
    int itsAge, itsYears;
    double itsSalary;
    };

    Bob bob;
    Martha martha;

    int main()
    {
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    cout << "This program will show the user two different employees for Widegets Inc.\n";
    cout << "These two employees are Bob and Martha,\n";
    cout << "and you will be asked to input some data for each one.\n";
    cout << "The this program will output that data you have given it. Ready?\n";

    bob.input_bob();
    martha.input_martha();
    bob.output_bob();
    martha.output_martha();

    return 0;
    }

    Bob::Bob()
    {
    itsAge = 0;
    itsYears = 0;
    itsSalary = 0;
    }

    Martha::Martha()
    {
    itsAge = 0;
    itsYears = 0;
    itsSalary = 0;
    }

    void Bob::input_bob()
    {
    cout << "Type in Bob's age: ";
    cin >> itsAge;
    cout << "Type in Bob's number of years on the job: ";
    cin >> itsYears;
    cout << "Type in Bob's hourly wage: $";
    cin >> itsSalary;
    }

    void Martha::input_martha()
    {
    cout << "Type in Martha's age: ";
    cin >> itsAge;
    cout << "Type in Martha's number of years on the job: ";
    cin >> itsYears;
    cout << "Type in Martha's hourly wage: $";
    cin >> itsSalary;
    }

    void Martha:utput_martha()
    {
    cout << "\nMartha is " << martha.getMarthaAge() << " years old.\n";
    cout << "Martha has been with Widgets Inc. for " << martha.getMarthYearOnJob() << " years.\n";
    cout << "Martha's current hourly wage is $" << martha.getMarthaSalary() << " dollars and hour.\n";
    }

    void Bob:utput_bob()
    {
    cout << "\nBob is " << bob.getBobAge() << " years old.\n";
    cout << "Bob has been with Widgets Inc. for " << bob.getBobYearOnJob() << " years.\n";
    cout << "Bob's current hourly wage is $" << bob.getBobSalary() << " dollars and hour.\n";
    }

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    9
    Thanks, but I was looking for something more along the lines of making this little project more streamlined. Given a day to think about it, I suppose I should have made just one Employee class and made Bob and Martha instances of the class, but I'm still unsure on how to make everything I did happen that way.
    Please help me out.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Edit your original post (using the "edit" button) and add the code tags, like Salem suggested. People are more likely to help you if your code is readable.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Burning in Hell! Luigi's Avatar
    Join Date
    Nov 2002
    Posts
    117
    Use code tags!
    I for example wont even bother looking at code unless code tags are used...
    So if u want help, use code tags..
    Luigi


    // I use Xcode 1.1 && CodeWarrior 8.3
    // When on Mac Os X 10.3.2

    // I use Microsoft Visual C++ 6.0
    // When on windows XP

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    I suppose I should have made just one Employee class
    Sounds sensible to me . As far as I could tell the classes had the same functions and variables, so you could just erase the second one entirely and rename to first to something like "employee" just to make more sense then
    in main:
    Code:
    employee Bob;
    employee Martha;
    seems like the rest would work pretty much the same, but without the extra code...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    If you need the input method for the employee class to say the employee's name instead of just replacing it with "employee," you could pass the employee's name to the method. You could also add a variable and methods for the employee's name in the class declaration. Your SetAge, SetYOJ, etc declarations should probably return the value to which the variable was set instead of void...to aid in error checking both now and in the future when it's become a habit.

    One other thing that needs to become a habit...using CODE TAGS.

    Code:
     //code tags are easy to use and fun for the whole family!
    Away.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Establishing 'make clean' with GNU make
    By Jesdisciple in forum C Programming
    Replies: 9
    Last Post: 04-11-2009, 09:10 AM
  2. how to know that the buffer is not clean??
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 01-21-2009, 09:56 PM
  3. prepro. clean tool / #id#def mess
    By tomsky in forum Linux Programming
    Replies: 2
    Last Post: 09-08-2005, 07:18 AM
  4. clean out a buffer
    By threahdead in forum C Programming
    Replies: 8
    Last Post: 02-23-2003, 01:04 PM
  5. make clean
    By Jaguar in forum Linux Programming
    Replies: 5
    Last Post: 12-27-2002, 06:44 PM