C++ Programming (using keyword static)

In C++, when modifying a data member in a class declaration, the static keyword specifies that one copy of the member is shared by all the instances of the class.

When modifying a member function in a class declaration, the static keyword specifies that the function accesses only static members.

The following example illustrates the usage of the keyword static:

class SavingsAccount {
public:
static void setInterest( float newValue ) // Member function that
{ currentRate = newValue; } // accesses only static members
private:
char name[30];
float total;
static float currentRate; // One copy of this member is shared among all instances
};

// Static data members must be initialized at file scope, even if private.
float SavingsAccount::currentRate = 0.00154;