C++ Class Static Member Functions
#include <cstdio> #include <cstdlib> #include <iostream> using namespace std; class Employee//from w w w.jav a2s . co m { public: Employee(const char* pN = "no name") : sName(pN) { noOfEmployees++; } ~Employee() { noOfEmployees--; } const string& name() { return sName; } static int number() { return noOfEmployees; } protected: string sName; static int noOfEmployees; }; int Employee::noOfEmployees = 0; int main(int argcs, char* pArgs[]) { Employee s1("Tom"); Employee* pS2 = new Employee("Mary"); cout << "Created " << s1.name() << " and " << pS2->name() << endl; cout << "Number of employees is " << s1.number() << endl; cout << "Deleting " << pS2->name() << endl; delete pS2; cout << "Number of employees is " << Employee::number() << endl; return 0; }