Write the class definition for a class named Employee. The class
should
include data members for an employee object's name and salary (the
salary will
be an integer). The class should contain two member functions: the
constructor
and a function that allows a program to assign values to the data
members. 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. (Hint: have the member functions simply
return the
contents of the appropriate data member).
Add another member function to the class. This function should
calculate an
employee object's new salary, based on a raise percentage, provided
by the
program (main function). Before calculating the raise, the member
function
should verify that the raise percentage is greater or equal to
zero. If the
raise percentage is less then zero, the member function should
display an error
message.
Write 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.
Do this in C++
Note
######
In case of any issue please comment
//####################### PGM START #########################################
#include<iostream>
#include<string>
using namespace std;
//employee class
class Employee{
private:
string name;
int salary;
public:
//constructor
Employee(){
}
//adding name and salary for
employee
void addEmp(string name,int
s){
this->salary=s;
this->name=name;
}
//funtion to get name of
employee
string getName(){
return
this->name;
}
//funciton to get salary of
employee
int getSalary(){
return
this->salary;
}
//funtion to find new salary beased
on rate increased
int newSalary(double r){
if(r<0){
cout<<"Error... rate less than 0\n";
return -1;
}
this->salary+=(0.01*r*this->salary);
return 1;
}
};
//main class
int main(){
string name;
double rate;
int flag;
//creating an array of 5 employee
Employee e1[5];
//adding values to 5 employee object using addEmp()
method
e1[0].addEmp("Emp1",1500);
e1[1].addEmp("Emp2",2300);
e1[2].addEmp("Emp3",4500);
e1[3].addEmp("Emp4",3400);
e1[4].addEmp("Emp5",4000);
//prinitng initial employee data
cout<<"Current employee data:\n";
for(int i=0;i<5;i++){
cout<<e1[i].getName()<<"
"<<e1[i].getSalary()<<"\n";
}
cout<<"Enter the rate by which salary is to be
increased: ";
cin>>rate;
//revising employee salary based on rate user
entered
for(int i=0;i<5;i++){
flag=e1[i].newSalary(rate);
if(flag==-1)
break;
}
if(flag==1){
//prinitng revised employee
data
cout<<"Revised employee
data:\n";
for(int i=0;i<5;i++){
cout<<e1[i].getName()<<"
"<<e1[i].getSalary()<<"\n";
}
}
return 0;
}
//############################### PGM END ##################################
OUTPUT
#########
Get Answers For Free
Most questions answered within 1 hours.