Employee object uses a string as data - C++ Class

C++ examples for Class:Class Creation

Description

Employee object uses a string as data

Demo Code

#include <iostream>
#include <string>
using namespace std;
class Employee// w  w  w  .j a  va 2 s.c  o m
{
   private:
   string name;
   long number;
   public:
   void getdata()          //get data from user
   {
      cout << "\nEnter name: ";  cin >> name;
      cout << "Enter number: "; cin >> number;
   }
   void putdata()          //display data
   {
      cout << "\n   Name: " << name;
      cout << "\n   Number: " << number;
   }
};
int main()
{
   Employee emparr[100];      //an array of Employees
   int n = 0;                 //how many Employees
   char ch;                   //user response
   do {                       //get data from user
       cout << "\nEnter data for Employee number " << n+1;
       emparr[n++].getdata();
       cout << "Enter another (y/n)? "; cin >> ch;
    } while( ch != 'n' );
    for(int j=0; j<n; j++)     //display data in array
    {
       cout << "\nEmployee number " << j+1;
       emparr[j].putdata();
    }
    cout << endl;
    return 0;
}

Result


Related Tutorials