对于班级作业,我必须编写班级定义。它被称为 Employee 类,对于我的第一个 c++ 类来说是非常基础的东西。
我的问题出在第一个 forloop 中,当我尝试根据新百分比调整薪水时。之后类中的变量不会改变。我不知道有什么问题了。
代码是:
#include <iostream>
#include <string>
using namespace std;
class Employee
{
private:
int emplSalary;
string employeeName;
public:
Employee();
Employee(string name,int salary);
string getName();
int getSalary();
void newSalary(int percent);
void input();
void output();
};
Employee::Employee()
{
emplSalary=0;
employeeName="";
}
Employee::Employee(string name,int sal)
{
employeeName=name;
emplSalary =sal;
}
string Employee::getName()
{
return employeeName;
}
int Employee::getSalary()
{
return emplSalary;
}
void Employee::newSalary(int percent)
{
emplSalary= emplSalary *(1+(percent/100));
cout<<"I calculated"<<endl;
/*
if(percent < 0)
{
cout<<"Invalid Percentage";
cout<<"I calculated"<<endl;
}
else
{
emplSalary= emplSalary *(1+(percent/100));
cout<<"I calculated"<<endl;
}
*/
}
void Employee::input()
{
cout << "Enter Name: ";
cin>> employeeName;
cout<<"\n";
cout<<"Enter Salary: " ;
cin>>emplSalary;
cout<<"\n";
}
void Employee::output()
{
cout << "Name: " << employeeName <<" : "<< "Salary: " << emplSalary << endl;
cout<<"\n";
}
int main()
{
const int NUMBER_EMPLOYEE =1;
Employee employees[NUMBER_EMPLOYEE];
int percent;
cout<<"Welcome to Employee program. Enter Name and Salary when prompted."<<endl;
cout<<"\n";
cout<<"\n";
for (int i=0; i<NUMBER_EMPLOYEE; i++)
{
employees[i]=Employee();
employees[i].input();
cout<<"What percentage to raise the salary: ";
cin>>percent;
employees[i].newSalary(percent);
}
for (int i=0; i<NUMBER_EMPLOYEE; i++)
{
employees[i].output();
}
return EXIT_SUCCESS;
}
输出是:
Welcome to Employee program. Enter Name and Salary when prompted.
Enter Name:
Enter Salary:
What percentage to raise the salary: I calculated
Name: : Salary: 0