Как ввести данные для конструктора, который передается в качестве указателя на класс сотрудника? - PullRequest
0 голосов
/ 29 марта 2019

Я пытаюсь передать строковый указатель, который указывает на имя моего конструктора вместе с двумя другими типами данных. Я не знаю, как написать свой код таким образом, чтобы мне не пришлось перегружать оператор потока, чтобы можно было прочитать данные в int main.

Я пытался разыменовать метод get, как вы видите, и я потерян.

#include<iostream>
#include<string>
#include<cmath>

using namespace std;

class Employee
{
private:
    string name;
    int emp_ID;
    int emp_GrossSal;
    double emp_Savings;

public:
    Employee()
    {
        string name = "";

    }

    Employee(string *n,int , int )
    {
        name = *n;
        emp_ID = NULL;
        emp_GrossSal = NULL;
        //emp_Savings = (.1*emp_GrossSal);
    }
    //get and set methods
    int getSalary();
    double getSavings();
    int getEmp_ID();
    string getName() const;

    void setEmp_ID(int);
    void setSalary(int);
    void setName(const string &);

    //functions
    void displayEmp();
    ~Employee();

    //overloaded operators
    void operator =(const Employee &right)
    {
            name = right.name;
            emp_GrossSal = right.emp_GrossSal;
            emp_ID = right.emp_ID;
            emp_Savings = right.emp_Savings;

    }

};
int Employee::getEmp_ID()
{
    return emp_ID;
}
int Employee::getSalary()
{
    return emp_GrossSal;
}
double Employee::getSavings()
{

    return (emp_GrossSal*(1/100));
}
string Employee::getName() const
{
    return name;
}
void Employee::setEmp_ID(int t)
{
    emp_ID = t;
}
void Employee::setSalary(int s)
{
    emp_GrossSal = s;
}
void Employee::setName( const string &n)
{
    name = n;
}

void Employee::displayEmp()
{

    cout << "Employee Name:" << name << endl;
    cout << "Employee ID  :" << emp_ID << endl;
    cout << "Employee Gross Salary: " << emp_GrossSal << endl;
    cout << "Employee Savings: " << emp_Savings << endl;
    cout << "Employee Monthy Salary: $ " << emp_GrossSal / 12 << endl;



}
Employee::~Employee()
{
}
int main()
{
    Employee One;
    string inputName;
    int ID, Gross_Sal, choice;

    cout << "Enter the number of Employees: " << endl;
    cin >> choice;


    cout << "\t\tPlease enter the Employee's Information Below\n\n" << endl;
    cout << "Enter the employees Name: " << endl;
    cin >> inputName;
    One.setName(inputName);
    cout << "Enter the employees ID:     " << endl;
    cin >> ID;
    One.setEmp_ID(ID);
    cout << "Employee Gross Salary:        " << endl;
    cin >> Gross_Sal;
    One.setSalary(Gross_Sal);


    One.displayEmp();

    system("Pause");
    return 0;

}   

Я пытаюсь вывести информацию о сотрудниках, такую ​​как имя и т. Д. Просто пытаюсь сначала управлять частью имени.

1 Ответ

0 голосов
/ 29 марта 2019
If you're trying to set the employee's name, set the name to n, not the other way around.

    Employee(string *n,int , int )
    {
        *n = name; (This should be name = *n;) I believe.
        emp_ID = NULL;
        emp_GrossSal = NULL;
        //emp_Savings = (.1*emp_GrossSal);
    }

Что касается установки имени сотрудника, я бы сделал функцию установки, но, как есть, вы также можете заставить получатель возвращать ссылку вместо этого, и тогда это должно работать:

 Employee One;
    char name;
    cout << "\t\tPlease enter the Employee's Information Below\n\n" << 
        endl;
    cout << "Enter the employees Name: ";
    cin >> One.getName();  (*(One.getName()) if using pointer)

    One.displayEmp();

Edit:

void Employee::setName(const string& name_)
{
    name = name_;
}


Employee One;
char name;
cout << "\t\tPlease enter the Employee's Information Below\n\n" << endl;
cout << "Enter the employees Name: ";
string inputName;
cin >> inputName;
One.setName(inputName);

One.displayEmp();

Edit # 2:

//NOTE: Your issue here is the way your set salary and display functions work
    class Employee
    {
    private:
        string name;
        int emp_ID;
        int emp_GrossSal;
        //NOTE: This is never set
        double emp_Savings;

    public:

    double Employee::getSavings()
    {
        //NOTE: This is never called
        return (emp_GrossSal*(1/100));
    }
    void Employee::setSalary(int s)
    {
        //NOTE: Only the emp_GrossSal is set, emp_savings isn't
        emp_GrossSal = s;
    }
    void Employee::displayEmp()
    {
        cout << "Employee Gross Salary: " << emp_GrossSal << endl;
        //NOTE: When you print out savings, you use emp_Saving which was never set.
        cout << "Employee Savings: " << emp_Savings << endl;
    }

У вас есть два решения для этого, либо вам нужно установить экономию, как показано ниже:

void Employee::setSalary(int s)
{
    emp_GrossSal = s;
    emp_Savings = (s * 0.1);
}

Или вы можете полностью избавиться от переменной emp_Savings, поскольку она нигде не используется, и просто вызвать функцию получения сбережений вместо использования переменной-члена, как показано ниже:

void Employee::displayEmp()
{
    cout << "Employee Gross Salary: " << emp_GrossSal << endl;
    //NOTE: This function will actually just get the salary divided by 10
    //NOTE: You want to keep this a function in case you change percentage in the future
    cout << "Employee Savings: " << getSavings() << endl;
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...