Answer to Question #194349 in C++ for Navya kala

Question #194349

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.


1
Expert's answer
2021-05-17T05:57:02-0400


#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;
	}
}





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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS