Write a program define an EMPLOYEE class with EmpId, EName and ESalary, Declare an array of five EMPLOYEE objects. Using appropriate function find name of employee who is getting second highest salary.
#include <iostream>
#include <string>
using namespace std;
class Employee
{
public:
void setMpid (int mpid)
{
e_mpid = mpid;
}
int getMpid()
{
return e_mpid;
}
void setName(string name)
{
e_name = name;
}
string getName()
{
return e_name;
}
void setSalary(double salary)
{
e_salary = salary;
}
double getSalary()
{
return e_salary;
}
Employee() {};
Employee(int mpid, string name, double salary)
{
e_mpid = mpid;
e_name = name;
e_salary = salary;
}
private:
int e_mpid{ 0 };
string e_name;
double e_salary{ 0 };
};
string secondSalaryName(Employee* p, int n)
{
if (n < 2)
{
return "Incorrect number of participants entered";
}
int max = 0;
for (int i = 0; i < n; i++)
{
if (p[i].getSalary() > p[max].getSalary())
{
max = i;
}
}
int second =0;
if (max == 0)
{
second = 1;
}
for (int i = 0; i < n; i++)
{
if (p[i].getSalary() >= p[second].getSalary() && max != i)
{
second = i;
}
}
return p[second].getName();
}
int main()
{
// test example
Employee test [5];
test[0] = { 10234,"Bob",1200 };
test[1] = { 10235,"Foo",2148};
test[2] = { 97812,"Tomas",3100 };
test[3] = { 504789,"Gorge",3200 };
test[4] = { 102305,"Faina",2145};
cout << "Name of employee who is getting second highest salary:";
cout << secondSalaryName(test, 5)<<endl;
return 0;
}
Comments
Leave a comment