Passing class objects into function parameters c++

15,219

Solution 1

Your Employee::Employee(int en, string n, string a, string p, double w, double h) is declaring new local variables inside it and therefore shadowing the member variables inside your class. So in reality, you never properly initialized any of its members during construction.

The following should correct that problem:

Employee::Employee(int en, string n, string a, string p, double w, double h)
  : employeeNumber ( en ),
    name ( n ),
    streetAddress ( a ),
    phoneNumber ( p ),
    hourlyWage ( w ),
    hoursWorkedperWeek ( h )
{
}

Solution 2

This is the error

Employee::Employee(int en, string n, string a, string p, double w, double h)
{
    int employeeNumber = en;
    string name = n;
    string streetAddress = a;
    string phoneNumber = p;
    double hourlyWage = w;
    double hoursWorkedperWeek = h;
}

it should be

Employee::Employee(int en, string n, string a, string p, double w, double h)
{
    employeeNumber = en;
    name = n;
    streetAddress = a;
    phoneNumber = p;
    hourlyWage = w;
    hoursWorkedperWeek = h;
}

or even better it should be as in greatwolf's answer.

The error in your version is that you've declared variables in your constructor with exactly the same names as the members of your class. These variables hide your class members, so your constructor does not initialise your class. So you get garbage.

Share:
15,219
xavi
Author by

xavi

New CS student.

Updated on June 04, 2022

Comments

  • xavi
    xavi almost 2 years

    I have read several of the previously asked questions on this topic, but can't seem to find the answer I'm looking for. When I run the program I receive no errors, but I get a lot of garbage data. I know it's because I'm not passing the parameters right, but I'm new to c++ and specifically how to use pointers correctly. To make it simple: I am passing an employee object through PrintCheck(), which calls the CalcSalary() function, which must use GetHours() and GetWage() to access member data to do calculations and return the correct salary. Would appreciate any explanations as to why I am generating garbage data!

    I have a class:

    class Employee
    {
    private:
        int employeeNumber;
        string name;
        string streetAddress;
        string phoneNumber;
        double hourlyWage;
        double hoursWorkedperWeek;
    public:
        Employee(void);
        Employee(int, string, string, string, double, double);
        int GetEmployeeNum() const;
        void SetEmployeeNum(int);
        string GetName() const;
        void SetName(string);
        string GetAddress() const;
        void SetAddress(string);
        string GetPhone() const;
        void SetPhone(string);
        double GetWage() const;
        void SetWage(double);
        double GetHours() const;
        void SetHours(double);
        double CalcPay(double, double);
    };
    

    I also have a function that needs to interact with the class:

    void PrintCheck(Employee&);
    

    My main function looks like:

    void main()
    {
        Employee joe(1, "Joe Blo", "125 Cool St.", "555 555 5555", 10.00, 45); //create employee 1
        Employee jane(2, "Jane Doe", "521 Dumb St.", "1 800 555 5555", 12.50, 30); //create employee 2
        PrintCheck(joe); //print check
    }
    

    The printcheck function looks like:

    void PrintCheck(Employee& a)
    {
    
        cout << "Pay to the order of " << a.GetName() << "...................................";
        cout << a.CalcPay(a.GetWage(), a.GetHours()) << endl;
        cout << "Hours worked: " << a.GetHours() << endl;
        cout << "Hourly wage: " << a.GetWage() << endl;
    }
    

    Calcpay function is:

    double Employee::CalcPay(double h, double w)
    {
        double salary = 0;
        int OT = 40;
        double timeandahalf = 1.5;
        double STATE = .075;
        double FED = .20;
        if (h > OT) // overtime
        {
            salary = h * (w * timeandahalf); // calc time and a half
            salary = salary * STATE; // calc state deductions
            salary = salary * FED; // calc federal deductions
        }
        else 
        {
            salary = h * w; // calc salary
            salary = salary * STATE; // calc state deductions
            salary = salary * FED; // calc federal deductions
        }
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(PRECISION);
        return salary;
    }
    

    My "get" functions all follow this pattern:

    int Employee::GetEmployeeNum() const
    {
        return employeeNumber;
    }
    

    My expected output would be:

    Pay to the order of:  Joe Blo............ $salary.
    Hours worked: $hours.
    Hourly wage: $wage.
    

    What I got:

    Pay to the order of: ........................ 128509280503000000000000000000000000000.00 (this number is long, but I didn't feel like typing them all)
    Hours worked: -9723636237 (same, just tons of bs numbers)
    Hourly wage: (the exact same number as above)
    

    My class constructor:

    Employee::Employee(int en, string n, string a, string p, double w, double h)
    {
        int employeeNumber = en;
        string name = n;
        string streetAddress = a;
        string phoneNumber = p;
        double hourlyWage = w;
        double hoursWorkedperWeek = h;
    }
    Employee::Employee()
    {
    }