Static Member Functions - C++ Class

C++ examples for Class:static member

Introduction

Static member functions exist not in an object but in the scope of the class.

Demo Code

#include <iostream> 
 
class Robot /*from   w  ww  .  j a v a  2  s. com*/
{ 
public: 
    Robot(int newAge = 1):age(newAge){ howManyRobots++; } 
    virtual ~Robot() { howManyRobots--; } 
    virtual int gGetAge() { return age; } 
    virtual void setAge(int newAge) { age = newAge; } 
    static int getHowMany() { return howManyRobots; } 
private: 
    int age; 
    static int howManyRobots; 
}; 
 
int Robot::howManyRobots = 0; 
 
void countRobots(); 
 
int main() 
{ 
    const int maxRobots = 5; 
    Robot *gestalt[maxRobots]; 
    int i; 
    for (i = 0; i < maxRobots; i++) 
    { 
           gestalt[i] = new Robot(i); 
           countRobots(); 
    } 
 
    for (i = 0; i < maxRobots; i++) 
    { 
           delete gestalt[i]; 
           countRobots(); 
    } 
    return 0; 
} 
 
void countRobots() 
{ 
    std::cout << "There are " << Robot::getHowMany() 
            << " robots.\n"; 
}

Result


Related Tutorials