C++ Class Static member function calling

Description

C++ Class Static member function calling

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

class Employee/*w w w  .  ja  v a2s  .  c om*/
{
  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;
}



PreviousNext

Related