c++ - Creating an employee class -


i attempting write following:

1) write class definition class named employee name , salary employee objects. class contains 2 member functions: constructor , function allows program assign values data members.

2) add 2 member functions employee class. 1 member function should allow program using employee object view contents of salary data member. other member function should allow program view contents of employee name data member.

3) add member function employeeclass. member function should calculate employee objects new salary, based on raise percentage provided program using object. before calculating raise, member function should verify raise percentage greater or equal zero. if raise percentage less zero, member function should display error message.

4) write main function create array of employee objects, assign values objects, display names , current salaries objects, ask user raise percentage , calculate , display new salaries objects.

i have attempted following code:

#include <iostream> #include <cstdlib> #include <string>  using namespace std;    class employee { public:     employee();//     employee(string name, int salary);//   public:   string name;//name input   int salary;//salary input  public: int enter_values(); int output_values(); int new_salary( int percentage_raise);    };   //default constructor employee::employee() {     name = "";     salary = 0; }  //constructor name/salary variables employee::employee(string name, int salary) {     name= name;     salary= salary; }  //name , salary input... int employee::enter_values() { cout<<"enter name , 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 should salary raised by?"<<endl;          cin>>percent;         cout<<employee.name<<"'s new salary "<<percentage_raise<<endl;    }         } 

however, cannot access parts need store information array in main function, nor can apply percentage raise when program prompts user. i'm pretty sure have syntax errors unaware of. i'm not asking me, appreciate steer in right direction. don't quite understand classes , how call them different parts of program. thank time.

you have in order.

things fix:

  1. the line

    if ( percentage_raise >= 0){salary= salary *(percentage_raise/100); 

    will set salary zero unless percentage_raise greater 100. that's because expression (percentage_raise/100) integer division , evaluate zero, unless pecentage_raise greater 100.

    you can fix with:

    if ( percentage_raise >= 0) {    int raise = (salary*percentage_raise)/100;    salary += raise; } 
  2. the line

       employee.new_salary(); 

    is going produce compiler error since there no object named employee.

    you can safely remove line. it's not serving purpose.

  3. you missing call set percentage raise after read input. need line

    employees[i].new_salary(percent); 

    immediately after read percent.

  4. the following line incorrect.

    cout<<employee.name<<"'s new salary "<<percentage_raise<<endl; 

    since there no object named employee. can replace with:

    cout<<employees[i].name<<"'s new salary "<<employees[i].salary<<endl; 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -