Create a class called Employee that contains the Name and Employee number (type long).
Include a member function called getData() to get data from the user for insertion into the object,
another function called putData() to display the data. The name should have no embedded blanks.
Write a main() program to exercise this class. It should create array of type Employee and then
invite the user to input data for up to 10 employees. Finally, it should print out the data for all the
employees. (Use setw, setiosflags to format the output).
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Employee {
private:
string name;
long number;
public:
void getData();
void putData();
};
void Employee::getData() {
cout << "Enter a name of an employee: ";
cin >> name;
cout << "Enter the emplyee number: ";
cin >> number;
}
void Employee::putData() {
cout << setw(20) << setiosflags(cout.left) << name << ":"
<< setw(10) << setiosflags(cout.right) << number;
}
int main() {
Employee employees[10];
int n=0;
for (n=0; n<10; n++) {
cout << "Data to the " << n+1 << "th employee" << endl;
employees[n].getData();
char ans;
cout << "Enter another data (y/n)? ";
cin >> ans;
if (ans != 'y' && ans != 'Y')
break;
}
if (n!=10)
n++;
cout << setw(20) << setiosflags(cout.left) << "Name" << ":"
<< setw(10) << setiosflags(cout.right) << "Number" << endl;
cout << "--------------------:----------" << endl;
for (int i=0; i<n; i++) {
employees[i].putData();
cout << endl;
}
cout << endl;
}
Comments
Leave a comment