C ++编程项目 - Employee Class不输出正确的信息

时间:2017-03-26 15:28:14

标签: c++

对于上课我应该设计一个员工班,而且说实话,我不知道我在做什么。但是我确实把它打印出去的地方就像它应该的那样,唯一的问题是它打印错误的信息。我不知道我做错了什么。如果有人能够至少指出我正确的方向,那将非常有帮助。我试图去我的学校CS实验室,他们并没有真正帮助我,我是一个初学者,而且我正在努力奋斗。

这是我的Employee.h

#pragma once
#include <string>

using namespace std;
class Employee {
private:
int employeeNumber; // Employee's employee number
string employeeName; //Employee's name
string streetAddress; //Employee's street address
string phoneNumber; //Employee's phone number
double hourlyWage; //Employee's hourly wage
double hoursWorked; //Employee's hours worked
double netPay; //Net pay
double grossPay; //Gross pay
public:

Employee();
Employee(int, string, string, string, double, double);



int getEmployeeNumber();
void setEmployeeNumber(int);

string getEmployeeName();
void setEmployeeName(string);

string getStreetAddress();
void setStreetAddress(string);

string getPhoneNumber();
void setPhoneNumber(string);

double getHourlyWage();
void setHourlyWage(double);

double getHoursWorked();
void setHoursWorked(double);



double calcPay()
{
    const int OVER = 40;
    double federal = 0.2;
    double state = 0.075;
    double timeHalf = 1.5;
    double grossPay;
    double netPay;
    if (getHoursWorked() < OVER)
    {
        grossPay = getHoursWorked() * getHourlyWage();
        netPay = grossPay - (grossPay * federal) - (grossPay - state);
    }

    if (getHoursWorked() >= OVER)
    {
        grossPay = getHoursWorked() * (getHourlyWage() * timeHalf);
        netPay = grossPay - (grossPay * federal) - (grossPay - state);

    }
    return netPay;
}

};

这是我的Employee.cpp

#include <iostream>
#include <string>
#include <iomanip>
#include "Employee.h"
#include <conio.h>

using namespace std;

Employee::Employee()
{
int employeeNumber = 0; // Employee's employee number
string employeeName = ""; //Employee's name
string streetAddress = ""; //Employee's street address
string phoneNumber = ""; //Employee's phone number
double hourlyWage = 0; //Employee's hourly wage
double hoursWorked = 0;

}


Employee::Employee(int empNum, string empName, string streetAddress, string phoneNumber, double hourlyWage, double hoursWorked)
{

}

Employee emp1(111, "Steve", "77 N 3484 E", "1234556785", 10.00, 45.00);
Employee emp2(222, "Suresh", "239 N 5103 E", "8835556788", 12.50, 30.00);

int Employee::getEmployeeNumber()
{
    return employeeNumber;
}

void Employee::setEmployeeNumber(int empNum)
{
    employeeNumber = empNum;
}

string Employee::getEmployeeName()
{
    return employeeName;
}

void Employee::setEmployeeName(string empName)
{
    employeeName = empName;
}


string Employee::getStreetAddress()
{
    return streetAddress;
}

void Employee::setStreetAddress(string strtAddrs)
{
    streetAddress = strtAddrs;
}

string Employee::getPhoneNumber()
{
    return phoneNumber;
}

void Employee::setPhoneNumber(string phnNum)
{
    phoneNumber = phnNum;
}

double Employee::getHourlyWage()
{
    return hourlyWage;
}

void Employee::setHourlyWage(double hrWage)
{
    hourlyWage = hrWage;
}

double Employee::getHoursWorked()
{
    return hoursWorked;
}

void Employee::setHoursWorked(double hrWorked)
{
    hoursWorked = hrWorked;
}



void printCheck(Employee ee)
{
    cout << "\n\n--------------------- Fluff Shuffle Electronics -------------------------------- \n";
    cout << " Pay to the order of " << ee.getEmployeeName() << "..........................." << ee.calcPay();
    cout << "\n\n United Bank of Eastern Orem \n";
    cout << "------------------------------------------------------------------------------- \n";
    cout << " Hours Worked: " << ee.getHoursWorked();
    cout << "\n Hourly Wage: " << ee.getHourlyWage();
}//End of function

 //Main function
int main(Employee ee)
{

    //Calls function to displays information
    printCheck(emp1);
    cout << "\n Press any key to continue..........";
    //getch();
    printCheck(emp2);
    cin.get();
}//End of main

预期产量: This is what it should be outputting

当前输出: And this is what it is ouputting

1 个答案:

答案 0 :(得分:1)

您没有设置Employee构造函数的值。

现在它是空的,因此当您实例化一个Employee时,它将没有分配属性成员值。 你应该这样做:

Employee::Employee(int empNum, string empName, string streetAddress, string phoneNumber, double hourlyWage, double hoursWorked)
{

    employeeNumber = employeeNumber;
    employeeName = empName;
    this->streetAddress = streetAddress;
    this->phoneNumber = phoneNumber;
    this->hourlyWage = hourlyWage;
    this->hoursWorked = hoursWorked;

}

您的main()功能也不是标准功能。

main()是您的计划的起点,您不应该在那里传递Employee参数。

尝试这样的事情:

int main()  // Remove Employee argument
{
    // Instantiate your employees here first, then call their functions.
    Employee emp1(1, "John Doe", "10 Downing Street", "555-555-555", 20, 40);
    Employee emp2(2, "Jane Williams", "10 Clark Street", "432-543-222", 20, 40);  
    //Calls function to displays information
    printCheck(emp1);
    cout << "\n Press any key to continue..........";
    //getch();
    printCheck(emp2);
    cin.get();
}