Create a class called employee with the following details as variables within it.
1. Name of the employee
2. Age
3. Designation
4. Salary
Get the details of 5 employees and initialize it using Constructor. Release resources using destructor. print the name, designation and salary of employees, Find the highest salaried employee in professor grade and display their details.
#include <iostream>
#include <string>
using namespace std;
class employee{
private:
string name;
int age;
string designation;
float salary;
public:
//Constructor
employee(){}
employee(string n,int a,string d,float s){
name=n;
age=a;
designation=d;
salary=s;
}
//destructor
~employee(){}
float getSalary(){
return salary;
}
//Display information about the employee
void displayInformationAboutEmployee(){
cout<<"Name: "<<name<<endl;
cout<<"Age: "<<age<<endl;
cout<<"Salary: "<<designation<<endl;
cout<<"Name: "<<salary<<endl<<endl;
}
};
//The start point of the program
int main (){
string name;
int age;
string designation;
float salary;
employee allEmployees[5];
//Get the details of 5 employees and initialize it using Constructor.
for(int i=0;i<5;i++){
cout<<"Enter the employee name: ";
getline(cin,name);
cout<<"Enter the employee age: ";
cin>>age;
cout<<"Enter the employee designation: ";
getline(cin,designation);
getline(cin,designation);
cout<<"Enter the employee salary: ";
cin>>salary;
allEmployees[i]=employee(name,age,designation,salary);
getline(cin,name);
cout<<endl;
}
cout<<endl<<endl;
for(int i=0;i<5;i++){
allEmployees[i].displayInformationAboutEmployee();
}
//Find the highest salaried employee
employee highestEmp=allEmployees[0];
for(int i=1;i<5;i++){
if(highestEmp.getSalary()<allEmployees[i].getSalary()){
highestEmp=allEmployees[i];
}
}
cout<<"\nThe highest salaried employee: \n";
highestEmp.displayInformationAboutEmployee();
//delay
system("pause");
return 0;
}
Comments
Leave a comment