C++ Constructor Set a data member

Description

C++ Constructor Set a data member

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int nextEmployeeId = 1000;
class EmployeeId//  w w  w  .  ja va 2 s . c  o m
{
  public:
    EmployeeId(){
        value = nextEmployeeId++;
        cout << "take next employee id " << value << endl;
    }
    EmployeeId(int id)
    {
        value = id;
        cout << "assign employee id " << value << endl;
    }
  protected:
    int value;
};


class Employee
{
  public:
    Employee(const char *pName, int ssId): name(pName), id(ssId)
    {
        cout << "constructing employee " << pName << endl;
    }
  protected:
    string name;
    EmployeeId id;
};

int main(int argcs, char* pArgs[])
{
    Employee s("Jack", 1234);
    cout << "This message from main" << endl;

    return 0;
}



PreviousNext

Related