C++ examples for Class:Constructor
Set a data member to a value in constructor
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; int nextEmployeeId = 1000; class EmployeeId//from w w w . jav a 2 s. c om { 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; }