Define the class Employee which has ID,name, DOB. Define the default constructor, member functions get_data() for taking the ID,name and DOB of the Employee, print() for displaying the data of Employee.Create an array of employee of size N and write the friend function sort() which sorts the array of employee according to their ID.
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
class Employee{
public:
int ID;
string name;
string DOB;
void get_data()
{
cout<<"Enter Employee ID : ";
cin>>ID;
cout<<"Enter Employee Name : ";
cin>>name;
cout<<"Enter Employee DOB : ";
cin>>DOB;
}
void print()
{
cout<<"Employee ID : "<<ID<<endl;
cout<<"Employee Name : "<<name<<endl;
cout<<"Employee DOB : "<<DOB<<endl;
}
friend void sort();
};
void sort(Employee *arr, int n)
{
for(int i=0; i<n; i++)
{
for(int j=0; j<n-i-1; j++)
{
if(arr[j].ID> arr[j+1].ID)
{
swap(arr[j+1], arr[j]);
}
}
}
}
int main()
{
cout<<"Enter number of Employees : ";
int n;
cin>>n;
Employee e[n];
cout<<endl;
for(int i=0; i<n; i++)
{
cout<<"Enter details of Employee"<<i+1<<endl;
e[i].get_data();
cout<<endl;
}
cout<<"Displaying Employee Details"<<endl;
cout<<"------------------------------"<<endl;
sort(e,n);
for(int i=0; i<n; i++)
{
e[i].print();
cout<<endl;
}
}
Comments
Leave a comment