Question #248604

Create a class called employee that contains a name (an object of class string) and an
employee number (type long). Include a member function called getdata() to get data
from the user for insertion into the object, and another function called putdata() to
display the data. Assume the name has no embedded blanks.
Write a main() program to exercise this class. It should create an array of type employee,
and then invite the user to input data for up to 100 employees. Finally, it should print out
the data for all the employees.

Expert's answer

#include<iostream>


using namespace std;
class employee{
	private:
		string name;
		long number;
	public:
		void getdata(){
			cout<<"Enter the name of the employee\n";
			cin.ignore();
			string n;
		getline(cin, n);
		
		
		name =n;
		
			cout<<"Enter the number of the employee\n";
			long num;
			cin>>num;
			number = num;
		}
		
		void putdata(){
			cout<<"Name:  "<<name<<endl;
			cout<<"Number:  "<<number<<endl;
		}
		
		
		
};
int main(){
	employee em[100];
		cout<<"Enter details of employees"<<endl;
		
	for(int i=0; i<100; i++){
		cout<<"Employee "<<(i+1)<<":"<<endl;
		em[i].getdata();
	}
	cout<<"Employees' Details as follows: \n";
	for(int i=0; i<100; i++){
		cout<<"Employee "<<(i+1)<<endl;
		em[i].putdata();
	}
	
	
}
LATEST TUTORIALS
APPROVED BY CLIENTS