Question #178399

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.


Expert's answer

#include <iostream>
#include <string>

using namespace std;

class Employee{
private:
	string name;
	int age;
	string designation;
	double salary;
public:
	//Constructor
	Employee(){}
	Employee(string n,int a,string d,double s){
		this->name=n;
		this->age=a;
		this->designation=d;
		this->salary=s;
	}
	~Employee(){}
	double getSalary(){
		return salary;
	}
	void display(){
		cout<<"Employee name: "<<name<<"\n";
		cout<<"Employee age: "<<age<<"\n";
		cout<<"Employee designation: "<<designation<<"\n";
		cout<<"Employee salary: "<<salary<<endl<<"\n";
	}
};


int main (){
	string employeeName;
	int employeeAge;
	string employeeDesignation;
	double employeeSalary;
	Employee employees[5];
	for(int i=0;i<5;i++){
		cout<<"Enter the employee name: ";
		getline(cin,employeeName);
		cout<<"Enter the employee age: ";
		cin>>employeeAge;
		cin.ignore();
		cout<<"Enter the employee designation: ";
		getline(cin,employeeDesignation);
		cout<<"Enter the employee salary: ";
		cin>>employeeSalary;
		employees[i]=Employee(employeeName,employeeAge,employeeDesignation,employeeSalary);
		cin.ignore();
		cout<<"\n";
	}
	cout<<"\n\n";
	for(int i=0;i<5;i++){
		employees[i].display();
	}
	//Find the highest salaried employee
	Employee maxSalaryEmployee=employees[0];
	for(int i=1;i<5;i++){
		if(employees[i].getSalary()>maxSalaryEmployee.getSalary()){
			maxSalaryEmployee=employees[i];
		}
	}
	cout<<"\nThe highest salaried employee in professor grade: \n";
	maxSalaryEmployee.display();
	cin>>employeeSalary;
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS