Write an Employee class which maintains the employees name and salary. Then write an overloaded member functions to sort the employee names and salary respectively and display the same.
Answers
Answer:
gzgs hehhd uje. suh ehheue b jsjshjeys. jshs. shhshshe. ysyeh. jsjs
Program in C++:
#include<iostream>
using namespace std;
class Employee
{
public:
void sort(int salary[], string name[], int n)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-i-1; j++)
{
if(salary[j] > salary[j+1])
{
int temp1 = salary[j];
salary[j] = salary[j+1];
salary[j+1] = temp1;
string temp2 = name[j];
name[j] = name[j+1];
name[j+1] = temp2;
}
}
}
cout<<"Name\tSalary\n";
for(int i = 0; i < n; i++)
{
cout<<name[i]<<"\t"<<salary[i]<<endl;
}
}
void sort(string name[], int salary[], int n)
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n-i-1; j++)
{
if(name[j].compare(name[j+1]) > 0)
{
string temp2 = name[j];
name[j] = name[j+1];
name[j+1] = temp2;
int temp1 = salary[j];
salary[j] = salary[j+1];
salary[j+1] = temp1;
}
}
}
cout<<"Name\tSalary\n";
for(int i = 0; i < n; i++)
{
cout<<name[i]<<"\t"<<salary[i]<<endl;
}
}
};
int main()
{
int n;
cout<<"Enter number of Employee : ";
cin>>n;
string name[n];
int salary[n];
for(int i = 0; i < n; i++)
{
cout<<"Enter name : ";
cin>>name[i];
cout<<"Enter salary : ";
cin>>salary[i];
}
Employee e;
cout<<"\nSort according to name : \n";
e.sort(name, salary, n);
cout<<"\nSort according to salary : \n";
e.sort(salary, name, n);
return 0;
}
Output:
Enter number of Employee : 5
Enter name : Nirali
Enter salary : 12000
Enter name : Poorvi
Enter salary : 15000
Enter name : Anu
Enter salary : 16000
Enter name : Zeba
Enter salary : 1000
Enter name : Happy
Enter salary : 15000
Sort according to name :
Name Salary
Anu 16000
Happy 15000
Nirali 12000
Poorvi 15000
Zeba 1000
Sort according to salary :
Name Salary
Zeba 1000
Nirali 12000
Happy 15000
Poorvi 15000
Anu 16000