C++ 创建员工类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22903160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating an employee class
提问by Syntax_Error
I am attempting to write the following:
我正在尝试编写以下内容:
1) Write the class definition for a class named Employee with name and salary as employee objects. The class contains two member functions: the constructor and a function that allows a program to assign values to the data members.
2) Add two member functions to the Employee class. One member function should allow any program using an employee object to view the contents of the salary data member. The other member function should allow the program to view the contents of the employee name data member.
3) Add another member function to the Employeeclass. The member function should calculate an employee objects new salary, based on a raise percentage provided by the program using the object. Before calculating the raise, the member function should verify that the raise percentage is greater than or equal to zero. If the raise percentage is less than zero, the member function should display an error message.
4) Write a main function that will create an array of employee objects, assign values to the objects, display the names and current salaries for all objects, ask user for the raise percentage and then calculate and display new salaries for all objects.
1) 为名为 Employee 的类编写类定义,其中名称和薪水作为雇员对象。该类包含两个成员函数:构造函数和允许程序为数据成员赋值的函数。
2)在Employee类中添加两个成员函数。一个成员函数应该允许任何使用雇员对象的程序查看工资数据成员的内容。另一个成员函数应该允许程序查看员工姓名数据成员的内容。
3) 向Employee类添加另一个成员函数。成员函数应该根据使用该对象的程序提供的加薪百分比计算员工对象的新薪水。在计算加薪之前,成员函数应验证加薪百分比是否大于或等于零。如果提高百分比小于零,则成员函数应显示错误消息。
4) 编写一个 main 函数,该函数将创建一个员工对象数组,为对象赋值,显示所有对象的名称和当前工资,询问用户加薪百分比,然后计算并显示所有对象的新工资。
I have attempted this with the following code:
我已尝试使用以下代码进行此操作:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class EMPLOYEE
{
public:
EMPLOYEE();//
EMPLOYEE(string name, int salary);//
public:
string name;//name to be input
int salary;//salary to be input
public:
int enter_values();
int output_values();
int NEW_SALARY( int percentage_raise);
};
//default constructor
EMPLOYEE::EMPLOYEE()
{
name = "";
salary = 0;
}
//constructor with name/salary variables
EMPLOYEE::EMPLOYEE(string NAME, int SALARY)
{
name= NAME;
salary= SALARY;
}
//name and salary to be input...
int EMPLOYEE::enter_values()
{ cout<<"Enter name and salary: ";
cin>> name;
cin>>salary;
}
//output
int EMPLOYEE::output_values()
{ cout<<"Name: "<<name<<endl;
cout<<"Salary: "<<salary<<endl;
}
//
int EMPLOYEE::NEW_SALARY(int percentage_raise)
{
EMPLOYEE updated_salary;
if ( percentage_raise >= 0){salary= salary *(percentage_raise/100);
}
else if(percentage_raise< 0)
{ cout<<"Error Message"<<endl;
}
return percentage_raise;
}
int main()
{
EMPLOYEE employees[100];
EMPLOYEE.NEW_SALARY();
int percent= 0;
int i;
for(i =0 ;i<100 ; i++)
{ employees[i]=EMPLOYEE();
employees[i].enter_values();
employees[i].name;
employees[i].salary;
employees[i].output_values();
cout<<"How much should the salary be raised by?"<<endl;
cin>>percent;
cout<<EMPLOYEE.name<<"'s new salary is "<<percentage_raise<<endl;
}
}
However, I cannot access the parts I need to store the information into the array in the main function, nor can I apply the percentage raise when the program prompts the user. I'm pretty sure I have syntax errors which I am unaware of. I'm not asking for someone to do everything for me, but I would appreciate a steer in the right direction. I don't quite understand classes and how to call them into different parts of a program. Thank you for your time.
但是,我无法访问将信息存储到主函数中的数组所需的部分,也无法在程序提示用户时应用百分比提升。我很确定我有我不知道的语法错误。我不是要求有人为我做所有事情,但我会很感激朝着正确的方向前进。我不太了解类以及如何将它们调用到程序的不同部分。感谢您的时间。
回答by R Sahu
You have almost everything in good order.
你几乎一切都井井有条。
Things to fix:
需要解决的问题:
The line
if ( percentage_raise >= 0){salary= salary *(percentage_raise/100);
will set
salary
tozero
unlesspercentage_raise
is greater than100
. That's because the expression(percentage_raise/100)
will be an integer division and will evaluate tozero
, unlesspecentage_raise
is greater than100
.You can fix it with:
if ( percentage_raise >= 0) { int raise = (salary*percentage_raise)/100; salary += raise; }
The line
EMPLOYEE.NEW_SALARY();
is going to produce a compiler error since there is no object named
EMPLOYEE
.You can safely remove that line. It's not serving any purpose.
You are missing a call to set the percentage raise after you read the input. You need the line
employees[i].NEW_SALARY(percent);
immediately after you read
percent
.The following line is incorrect.
cout<<EMPLOYEE.name<<"'s new salary is "<<percentage_raise<<endl;
since there is no object named
EMPLOYEE
. You can replace it with:cout<<employees[i].name<<"'s new salary is "<<employees[i].salary<<endl;
线
if ( percentage_raise >= 0){salary= salary *(percentage_raise/100);
将设置
salary
为zero
除非percentage_raise
大于100
。那是因为表达式(percentage_raise/100)
将是一个整数除法,并且计算结果为zero
,除非pecentage_raise
大于100
。您可以使用以下方法修复它:
if ( percentage_raise >= 0) { int raise = (salary*percentage_raise)/100; salary += raise; }
线
EMPLOYEE.NEW_SALARY();
将产生编译器错误,因为没有名为
EMPLOYEE
.您可以安全地删除该行。它没有任何目的。
阅读输入后,您错过了设置加薪百分比的电话。你需要线
employees[i].NEW_SALARY(percent);
阅读后立即
percent
。以下行是不正确的。
cout<<EMPLOYEE.name<<"'s new salary is "<<percentage_raise<<endl;
因为没有名为 的对象
EMPLOYEE
。您可以将其替换为:cout<<employees[i].name<<"'s new salary is "<<employees[i].salary<<endl;
回答by ppl
class Employee
{
public:
Employee();
int salary;
};
Employee::Employee() { salary = 10; }
int main()
{
Employee joe;
std::cout << "Joe Salary: " << joe.salary << std::endl;
joe.salary = 15;
std::cout << "Joe New Salary: " << joe.salary << std::endl;
}
Usually, you will want your data members to be private and use an accessor method to provide the values of the data members which, again, should be private.
通常,您希望您的数据成员是私有的,并使用访问器方法来提供数据成员的值,这些数据成员也应该是私有的。