C++ examples for Class:static member
Declaring Static Member Functions, two ways to call a static member function
#include <string> #include <cstdio> #include <cstdlib> #include <iostream> using namespace std; class Employee/* w w w .j av a2 s. com*/ { 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; }