C++ Constructor construct data member with an initializer
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; class EmployeeId{ public:/* w ww.j a va 2 s . c o m*/ EmployeeId(int id) : value(id){ cout << "id = " << value << endl; } protected: int value; }; int nextEmployeeId = 1000; class Employee{ public: Employee(const char *pName, int ssId): name(pName), id(ssId){ cout << "constructing employee " << pName << endl; } Employee(const char *pName): name(pName){ cout << "constructing employee " << pName << endl; } protected: string name; EmployeeId id = nextEmployeeId++; }; int main(int argcs, char* pArgs[]) { Employee s1("tom", 1234); Employee s2("abc"); return 0; }