Problem 3 Classes 10 points Create a class called Employee. The class should hav
ID: 3912867 • Letter: P
Question
Problem 3 Classes 10 points Create a class called Employee. The class should have the following members: firstName: string .lastName: string . pay: int Create a dynamically allocated array of n employee variables where n is a number entered by the user. Validate the input until a positive integer is entered. Create a function that expects the array as input. The function populates the array with employee objects entered by a user by looping through the array and letting the user input the data for each employee. Create a second function that expects a pointer to the array as input and prints the elements in the array. Remember that arrays are passed by reference, that is, any modifications inside the function will be reflected in the caller's array. Call both functions from the main Number of employees: 2 Employee 1 First Name: Allen Last Name: Harper Pay: 1000 Employee 2 First Name: John Last Name: Doe Pay: 500 Example: Employees: Name: Allen Harper (1000) Name: John Doe (500)Explanation / Answer
#include<bits/stdc++.h>
using namespace std;
class Employee{
string first_name;
string last_name;
int pay;
public:
Employee(){
first_name="";
last_name="";
pay=-1;
}
Employee(string first,string last,int p){
first_name=first;
last_name=last;
pay=p;
}
void print_(){
cout<<first_name<<" "<<last_name<<" "<<"("<<pay<<") ";
}
};
void create_objects(Employee *arr,int n){
string first_name,last_name;
int payment;
for(int i=0;i<n;i++){
cout<<"Employee "<<i+1<<" : "<<" ";
cout<<"Enter First name : ";
cin>>first_name;
cout<<"Enter Last name : ";
cin>>last_name;
cout<<"Enter pay amount : ";
cin>>payment;
*(arr + i )=Employee(first_name,last_name,payment);
cout<<" ";
}
}
void print_objects(Employee *arr,int n){
cout<<"Employee : ";
for(int i=0;i<n;i++){
cout<<"Name : ";
(arr+i)->print_();
}
}
int main(){
int n,i;
cout<<"enter count of employee : ";
cin>>n;
if(n>0){
Employee *arr=new Employee[n];
create_objects(arr,n);
print_objects(arr,n);
}
else
cout<<"Not a valid count ";
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.