Creating an employee class
Solution 1
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;
Solution 2
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.
Syntax_Error
Updated on August 10, 2020Comments
-
Syntax_Error over 3 years
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.
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.