In C++
Employee Class
Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions.
In main create an array of 100 Employee objects using the default constructor.
The program will repeatedly execute four menu items selected by the user, in main:
1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored, up to 100).
2) in a second function, display the employee data from the array.
3) in main have the user enter an employee ID number and name, and while processing each object in the array call the "Match" member function described below; if the ID and name are found in the current object then display in main all of the associated object's employee data; if not found display in main an error message with the entered ID and name.
4) end the program
The Employee class should be defined as follows:
Private member data:
-name (a string)
-idNumber (an int)
-department (a string)
-position (a string)
The Employee class should have the following Public member functions:
-a default constructor
-a constructor which accepts the employee name and ID number as arguments and assigns them to the appropriate member variables
-a constructor which accepts the employee name, ID number, department, and position as arguments and assigns them to the appropriate member variables
Also write the appropriate accessor (getter) and mutator (setter) functions for each of the member variables.
The employee ID number will be entered by the user in a member function named Enter_ID and returned via its return type. Enter_ID will call "private" member function Validate to validate the ID, which will be returned to Enter_ID via reference variable (the ID must be greater than zero).
Add a "Match" member function to the Employee class. The “Match” function will accept into its parameter list an ID number and name. The function will match the two parameter values to those in the current object and return "true" if the values match, i.e., the employee is found, or "false" if not.
Provide four screen prints:
-execute menu option #1 (have the user enter the data for the four employees shown below)
-execute menu option #3 (have the user enter ID Number 39119 and Mark Jones)
-execute menu option #2
-execute menu option #3 (have the user enter ID Number 53179 and Susan Meyers)
Employee data for user to enter (name; id; department; position):
Susan Meyers; 47899; Accounting; Vice President
Mark Jones; 39119; IT; Programmer
Joy Rogers; 81774; Manufacturing; Engineer
Humpty Dumpty; 00001; Executive; CEO
#include<iostream>
#include<string>
using namespace std;
class Employee
{
private:
string name,dept,position;
int id;
bool validate(int i)
{
if(i<=0)
return
false;
else return
true;
}
public:
Employee()
{
this->name="ABC";
this->dept="XYZ";
this->position="NONE";
this->id=0;
}
Employee(string name,int id)
{
this->name=name;
this->dept="XYZ";
this->position="NONE";
this->id=id;
}
Employee(string name,string
department,string pos,int id)
{
this->name=name;
this->dept=department;
this->position=pos;
this->id=id;
}
void setName(string name)
{
this->name=name;
}
string getName()
{
return
this->name;
}
void setDept(string dept)
{
this->dept=dept;
}
string getDept()
{
return
this->dept;
}
void setPosition(string pos)
{
this->position=pos;
}
string getPosition()
{
return
this->position;
}
void setId(int id)
{
this->id=id;
}
int getId()
{
return
this->id;
}
bool Enter_ID()
{
int i;
cout<<"Enter the id of employee: ";
cin>>i;
if(validate(i))
{
setId(i);
return true;
}
else return
false;
}
bool Match(int i,string n)
{
if(this->id!=i || this->name.compare(n)!=0)
{
return false;
}
else
{
cout<<"\nEmployee found with
details:-\n";
this->display();
return true;
}
}
void display()
{
cout<<"\nID: "<<this->getId();
cout<<"\nName: "<<this->getName();
cout<<"\nDepartment: "<<this->getDept();
cout<<"\nPosition: "<<this->getPosition();
cout<<"\n";
}
};
int main()
{
Employee e[100];
int n=0,ch;
string na,d,p="";
do
{
cout<<"1) Enter employee
details";
cout<<"\n2) Display all
emplyees";
cout<<"\n3) Search for an
employee";
cout<<"\n4) exit the
program";
cout<<"\nEnter your choice:
";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the name of the employee:
";
cin.ignore();
getline(cin, na);
e[n].setName(na);
reenter:
if(e[n].Enter_ID())
{
cout<<"Enter the
department: ";
cin.ignore();
getline(cin,d);
e[n].setDept(d);
cout<<"Enter the
position of the employee: ";
getline(cin,p);
e[n].setPosition(p);
n++;
}
else
{
cout<<"\nThe id must be
greater the 0.\n";
goto
reenter;
}
break;
case 2:
for(int i=0;i<n;i++)
e[i].display();
break;
case 3:
int id,f;
f=0;
bool a;
cout<<"\nEnter the id of the employee to
be searched: ";
cin>>id;
cout<<"Enter the name of the employee to
be searched: ";
cin.ignore();
getline(cin,na);
for(int i=0;i<n;i++)
{
if(e[i].Match(id,na))
{
f=1;
goto
out;
}
}
out:
if(f==0)
cout<<"\nThe employee does not
exist";
break;
case 4:
cout<<"Exiting...";
break;
}
cout<<"\n";
}while(ch!=4);
return 0;
}
Output:-
Get Answers For Free
Most questions answered within 1 hours.